How to build your own Audiobook in Python

Are you hate reading? Don’t worry, In this tutorial, we will build an audiobook in Python. You do not need to buy a subscription for an audiobook if you have a pdf format of the book. In this article, you will know how to develop a basic audiobook in just 12 lines of python code.

Audiobook in python
Audiobook in python

Audiobooks are great for people who want to read but don’t have time or for people who can’t read because of a disability. It is also great for learning languages because you can listen to them in your native language and then translate it into another language.

Install required libraries

Python has an ocean of libraries that serve various purposes. In this article, we will require two libraries (pyttsx3, PyPDF2) to develop an audiobook.

  • pyttsx3 (pip install pyttsx3)
  • PyPDF2 (pip install PyPDF2)
import pyttsx3
speaker = pyttsx3.init()
speaker.say('codewithberi.xyz')
speaker.runAndWait()

Now, your python code can speak anything you want. Now we want a pdf file which we want to listen.

Build your own Audiobook in 12 lines of Python Code

import pyttsx3
import PyPDF2

book = open('oop.pdf', 'rb') #Open any pdf file
pdfReader = PyPDF2.PdfFileReader(book)
pages = pdfReader.numPages
speaker = pyttsx3.init()
for num in range(pages):
    page = pdfReader.getPage(num)
    text = page.extractText()
    speaker.say(text)
    speaker.runAndWait()

I found one awesome YouTube video, and I think this will be helpful for you.

Conclusion:

In this article, we have covered the implementation of a basic audiobook, that can read the entire pdf book using 12 lines of python code. For better audio results you can also tune the voice, rate, and volume of the speaker library. Thanks for reading!

Leave a Comment