In this blog let’s practice our new coding tutorial about creating an audiobook generator using Python with a few lines of simple code. We are using the PyPDF2 module in Python to create an audiobook generator. So let’s write our code
Code for Audiobook generator using Python with PyPDF2 module
#Importing Libraries
#Importing Google Text to Speech library
from gtts import gTTS
#Importing PDF reader PyPDF2
import PyPDF2
#Open file Path
pdf_File = open('read.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")