Reading books is a good habit but listening to the books makes the process all the more convenient. An audiobook comes in place of traditional books as we can listen to them whenever and wherever we want easily. How useful would it be, if we could code an audiobook maker, which converts an ebook pdf to audiobooks and reads it for us?
In this tutorial, we will construct an audiobook maker, which reads out a book for us, using Python.
Reading eBooks as Audiobooks using Python
Let’s get into the process of making a book reader or a Python script that converts PDF ebooks into audiobooks!
1. Install Required Modules
To start with, we need to install the required modules that would ease out our work considerably for coding an audiobook maker. We install the pyttsx3 and PyPDF3 modules using the pip package manager.
pyttsx3 is a text-to-speech conversion library in Python while PyPDF3 is a library to read and edit PDF files in Python.
pip install pyttsx3 pip install PyPDF3
2. Import the PDF Reader and TTS Modules
In a python file, we begin with importing the required modules.
import PyPDF3 import pyttsx3
Now we initialize the pyttsx3 engine object to read.
engine = pyttsx3.init()
3. Open and Read PDF
Now as we have initialized our speech engine, we need to open the PDF to read the contents of it. We pass in the name of the pdf to the open method, as shown:
You are required to pass the name along with the location of the PDF if it’s not in the same directory as the python script.
book = open('sample.pdf', 'rb')
To read the pdf contents line by line and as per the speech, we use the PdffileReader method of the PyPDF3 module as shown:
We then extract the text from the object of the pdf reader, using the extractText method.
pdfRead= PyPDF3.PdfFileReader(book) #to start the reading from 1st page in the pdf page = pdfRead.getPage(0) #to extract text to read text = page.extractText()
4. Speak the PDF
As we have opened, read the pdf content we now need to feed into this data to our speech engine of the pyttsx3 library
#takes in message to read or text engine.say(text) engine.runAndWait()
Upon Execution of the script, the code starts to read the passed PDF. Final Code is given as:
import PyPDF3 import pyttsx3 engine = pyttsx3.init() book = open('sample.pdf', 'rb') pdfRead= PyPDF3.PdfFileReader(book) #to start the reading from 1st page in the pdf page = pdfRead.getPage(0) #to extract text to read text = page.extractText() #takes in message to read or text engine.say(text) engine.runAndWait()
5. Changing the Speech
The pyttsx3 library provides us various types of changes to the speech, such as:
Settings to change the Rate of Speech
rate = engine.getProperty('rate') # gets the current rate of speech engine.setProperty('rate', 125) # sets up new rate of speech (passed in as 125 to change to 1.25x or 150 to make it to 1.5x)
Settings to change the Voice of Speech
voices = engine.getProperty('voices') # gets the current voice type #engine.setProperty('voice', voices.id) #changing index, changes voices. 0 for male engine.setProperty('voice', voices.id) #changing index, changes voices. 1 for female
Settings to change the Volume of Speech
volume = engine.getProperty('volume') #gets the current volume (min=0 and max=1) engine.setProperty('volume',1.0) # setting up volume level between 0 and 1
We can also save the complete audiobook file – meaning complete pdf (book) in terms of Speech in an audio file (type .mp3), using the following code:
That’s it for the tutorial on how to code an audiobook maker in Python. We hope you enjoyed this short tutorial on converting a PDF to an audiobook. Go ahead and play around with the script to make it even more intuitive and automated!