How to make an Audiobook with Python

How to make an Audiobook with Python

Do you like listening to Audiobooks? Yes!! Now you don't need to buy the subscription if you have a pdf of the book. In this article, you will learn about how to make an audiobook from pdf using python. So, without wasting time let's start. 




If you like the post, share this article on your social media platforms and coding communities so that more people can reach out to the content. Now, let's start to make an audiobook with Python.



1) Installing required libraries

For creating an audiobook with Python, we require two libraries. One that will read the text from the pdf and one that will convert the text to speech.

1. PyPDF2 - A python library build as a PDF toolkit is capable extract text from documents, split and merge documents, cropping pages and many more.

We will be using this library to extract text from pdf. So, open your command prompt or terminal and write the following command and hit enter. This library will be installed in your computer within few seconds.

pip install pyPDF2

2. pyttsx3 - This is one of the best text to speech conversion library available in Python. Unlike other libraries, it works offline and is compatible with both Python2 and Python3.

To install this library, open your command prompt or terminal and run the following command.

pip install pyttsx3

Also Read - QR Code Generator with Python

2) Let's code

We are done with the installation part and now let's code. So, create a python file and start to code.

First thing to do is to import our installed libraries in the program. For this write the following command.

import pyttsx3
import PyPDF2

Now as we have to read the text from the file, let's open the pdf file in read mode. The name of my pdf file is "demo.pdf" and is present in the same directory where my python file is present. Note here "rb" stands for reading mode.

pdf_Reader = PyPDF2.PdfFileReader(open("demo.pdf",'rb'))

Now, let's initialize the pyttsx3 module by using init method.

engine = pyttsx3.init()

Now, the next step is to loop through each page of pdf file and extract the text from the pdf using extractText() method and store it in text variable. Before that let's print a message "Playing the Audiobook". Next, we will call the say method to convert the text into speech and then we will call runAndWait at the end.

print("Playing the Audiobook!!!")
for page_number in range(pdf_Reader.numPages):
    text = pdf_Reader.getPage(page_number).extractText()
    engine.say(text)
    engine.runAndWait()

At last we will stop the engine.

engine.stop()

Now, run the program.

BOOM!! You will hear some audio like reading the pdf file and your pdf file is converted to an audiobook. You can listen to it anytime by just running the python file. 

Here's the complete combined source code for the project - How to make an Audiobook with Python.

Source Code

import pyttsx3
import PyPDF2
pdf_Reader = PyPDF2.PdfFileReader(open("demo.pdf",'rb'))
engine = pyttsx3.init()

for page_number in range(pdf_Reader.numPages):
    text = pdf_Reader.getPage(page_number).extractText()
    engine.say(text)
    engine.runAndWait()
engine.stop()


I hope you liked this article on how to make an audiobook with Python. In case of any doubt feel free to ask in comments below. Also, your suggestions and appreciations are welcomed in comments. Share the article in your coding groups so that more people can get in touch with the content.

Enjoy Coding!!!

Also Read 





For any doubts feel free to ask in comments below.
Stay Connected to Us for more such Courses and Projects.

Post a Comment

For any doubts feel free to ask in comments below.
Stay Connected to Us for more such Courses and Projects.

Post a Comment (0)

Previous Post Next Post