Build your own Free Audio Book from PDF using Python
In this tutorial, we are going to make an Audiobook from PDF using python. As we know we have to pay a lot of money to buy an audiobook subscription online, most of us cant able to afford that. so we can use this method to build an audiobook and listen to it.
we are using 2 dependencies to make it work. PyPDF2 and pyttsx3. The PyPDF2 dependencies help us to get the contents from the PDF file and the pyttsx3 will read that for us. simple.
let’s start writing code, for that You need Python and the PyCharm IDE on your pc (or you can use any of your IDE)
Download the dependencies first
pip install pypdf2
pip install pyttsx3
Now open your code editor and write this script. and copy your PDF file to the same folder as u have this file saved.
import PyPDF2 import pyttsx3 pdfobj = open('demo.pdf','rb') #replace demo with your file #pdf file object. pdfreader = PyPDF2.PdfFileReader(pdfobj) text = "" #read all the page of pdf. for pagenumber in range(pdfreader.numPages): page = pdfreader.getPage(pagenumber) #get page. text += page.extractText() #extract the text. pdfobj.close() #close the file. print(text) engine = pyttsx3.init() #init the text to speech library. engine.say(text) #this function speak the text. engine.runAndWait()
Run the code in your terminal. and this python guy will tell you what’s inside your book.
Enjoy your audio book.