Convert PDF File Text to Audio Speech using Python and Google gTTS API

Introduction

We are going to show you how to extract text from PDF file and convert them into audio speech using Google gTTS API.

gTTS (Google Text-to-Speech), a Python library and CLI tool to interface with Google Translate’s text-to-speech API. Write spoken mp3 data to a file, a file-like object (bytestring) for further audio manipulation, or stdout.

Prerequisites

Python 3.8.3, gTTS 2.1.1 (pip install gTTS)

Convert PDF Text to Audio Speech

Now we will extract the text from PDF file and convert it to audio speech using Google gTTS API.

We import the required modules.Next we open and read the PDF file.

Extract lines of text from PDF file and put one by one into a list.

Next we convert list of text lines into a string of text.

Finally we convert string of text into English audio speech at fast speed.

#Importing Libraries
#Importing Google Text to Speech library
from gtts import gTTS

#Importing PDF reader PyPDF2
import PyPDF2

#Open file Path
pdf_File = open('simple.pdf', 'rb') 

#Create PDF Reader Object
pdf_Reader = PyPDF2.PdfFileReader(pdf_File)
count = pdf_Reader.numPages # counts number of pages in pdf
textList = []

#Extracting text data from each page of the pdf file
for i in range(count):
   try:
    page = pdf_Reader.getPage(i)    
    textList.append(page.extractText())
   except:
       pass

#Converting multiline text to single line text
textString = " ".join(textList)

print(textString)

#Set language to english (en)
language = 'en'

#Call GTTS
myAudio = gTTS(text=textString, lang=language, slow=False)

#Save as mp3 file
myAudio.save("Audio.mp3")

Testing the Program

Running the above script you will get a file called Audio.mp3 saved under the current directory. You can play the mp3 file to listen to the speech and compare it with your PDF text.

You can find the sample pdf file and output mp3 file in the source code.

Source Code

Download

Thanks for reading.

Leave a Comment