Geek Culture
Published in

Geek Culture

Make Your PDF an AudioBook Using Python Programming

Converting PDF into AudioBook using Google Text to Speech and Python

Converting Pdf to an audio file using Python

Hello, Python Enthusiasts..!!

Python is an easy programming language to learn. But doing projects brings more clarity to your knowledge. Projects can be of many types like real-world problem-solving projects, a project from which you can earn money, and many more. If you want to achieve something big in your programming carer, then at some point, you have to start doing projects. In the beginning, start with small projects.

In this article, we are going to build a Python project. The project is about the conversion of a pdf file to an audio file. Yes, you can convert your favorite book into an audiobook and listen at your bedtime. Let’s start the project.

If you are looking for a video tutorial of this project then it’s here:

PDF → AudioBook

My recommendation is to start this project in a virtual environment because, in the virtual environment, the newly installed version of the python library won’t affect the existing version of the same python library in the system. And it’s a good habit to build a python-based project. But it’s not mandatory, you can continue without a virtual environment too. But you have to install the following python libraries for this project:

pip install gtts
pip install tk
pip install PyPDF2

If you don’t know, how to start a python project in a virtual environment then you can read this article or watch a video:

Code With Explanation

Make a file, file.py

from gtts import gTTS #google text to speech
import PyPDF2
from tkinter import filedialog
location = filedialog.askopenfilename()
full_text =""
with open (location, 'rb') as book:
try:
reader = PyPDF2.PdfFileReader(book)
total_pages = reader.numPages #for total number of pages
print('Location of the file is: '+location)
print('\n')
print('Total number of pages are: '+ str(total_pages))
try:
for page in range(total_pages):
next_page = reader.getPage(page)
content = next_page.extractText()
full_text += content

speech = gTTS(text = full_text)
speech.save("audio.mp3")

except:
print("Task failed Successfully")

except:
print("\n")
print('Invalid pdf')
print("\n")
  • Import the libraries
  • Specify variables for location and pdf text.
  • Read the number of pages
  • Print the location of Pdf file
  • Print the number of pages of the pdf file.
  • Use a for loop for reading the pdf file page by page and storing the text as it is in the full_text variable. If you want to listen to the audio from a specific page then you can change the indexing of the range of the for loop. For example, I want to listen to the audio from the 42nd page of the pdf file, Then I have to change the range of for loop something like this:
for page in range(41, total_pages):

You are all well aware of the concept of indexing in python.

  • Use Google text to speech to convert the pdf file to audio.
  • Save the audio file in the same directory.

Now run the file using the command:

python file.py

Select a small pdf file, let the conversion complete. Now check out your project directory. You will see a new file, “audio.mp3”. Play that file.

BINGO.

That’s it for this article.

If this article sounds informative to you then make sure to follow and clap. Share it with your geek community.

Thanks for reading.

Hello, My Name is Rohit Kumar Thakur. I am open to freelancing. I build react native projects and currently working on Python Django. Feel free to contact me (freelance.rohit7@gmail.com)

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store