Allow the Books Speak to You With Python
By: Shubham Pathania
Tags: Coding, Software Development, Python, Data Science, Programming

Step #1. Import the Python library

The library I was talking about is pyttsx3 (python text to speech version 3). It is a text-to-speech conversion library in Python. Unlike alternative libraries, it works offline and is compatible with both Python 2 and 3.

You can use any editor for creating this project. I prefer using Pycharm due to its user-friendly and other important features. You can use any editor and then import this library by executing the below-mentioned command:

pip install pyttsx3

pip is a package manager for Python. That means it’s a tool that allows you to install and manage additional libraries and dependencies that are not distributed as part of the standard library.

Here, we are telling the package manager to install the specific library to our project.

Step #2. Make the Code Talk

Once the library is installed, we can import it into our project.

import pyttsx3

Then it takes three lines of code to make the code speak.

speak = pyttsx3.init() speak.say('A.I. is going to take over the world') speak.runAndWait()

Here, we have initialized an instance of our imported library. We used the in-built method say in which we wrote the text that we want to convert into speech. Lastly, we call the runAndWait method for the execution.

Run the above code for turning your text into the speech.

Step #3. Create an Audiobook

There is a prerequisite before we move to create our own audiobook. We will need a pdf that can be converted into an audiobook. You can choose any pdf file. If you have a pdf for any book or novel, then you can use that one.

Once we have a pdf, then the next thing we need is a package to read pdf files. We will again go to our editor and install the package —

pip install PyPDF2

After the package installation, we can import the package in our code to read the pdf file.

import PyPDF2 book = open('filename.pdf','rb')

pdfReader = PyPDF2.PdfFileReader(book) page = pdfReader.getPage(1) text = page.extractText()

Here, we have imported the package to read pdf files in our code. Then we have created an object call book to open the given pdf file. The second argument ‘rb’ stands for — read as binary.

After that, we call the PdfFileReader method of the imported package and pass our pdf file information to it. Then we are calling the getPage method to extract the specific page information and extract the text by calling method extractText in the next step.

Once we get the text extracted then we can pass it to the method say that we created in step #1.

Final Code for audiobook

import pyttsx3

import PyPDF2 book = open('filename.pdf','rb')

pdfReader = PyPDF2.PdfFileReader(book) pages = pdfReader.numPages()

speak = pyttsx3.init() for num in range(0, pages-1)

pages = pdfReader.getPage(num)

text = page.extractText()

speak.say(text)

speak.runAndWait()

That’s it. In eleven lines of code, we created our own custom audiobook.

Next time, instead of sitting in front of a computer going through some daunting pdf, just convert it into an audiobook and lie down while listening to it.

There are several other methods present in library pyttsx3 for customizations like changing the voice of the reader, controlling volume, and even we can save the audiobook as a .mp3 file in our system. I’ll leave those things up to you for exploring the library further.