Using The Python Programming Language
Have you ever wanted to take a PDF file that you were reading and simply give it to some program and have that program read the contents of the PDF file to you ? Well, if you are like me and you wanted a program like this, then you are in luck because in this article, I will attempt to convert a PDF file to an audio book using the python programming language.
So, exactly what is a PDF file ? Well PDF stands for Portable Document Format and it is a file format developed by Adobe. It was used to present documents (text & images) that could not be modified but still easily shared and printed.
Now that you know a little about PDF files, let’s talk about audio books. An audio book is a “talking book”. Many times it is a recording of a book, for this article it will be the text from a document being read out loud.
If you prefer not to read this article and would like a video representation of it, you can check out the YouTube Video below. It goes through everything in this article with a little more detail, and will help make it easy for you to start programming . Or you can use both as supplementary materials for learning !
Okay so, with all of that being said let’s start programming !
First, you will need to install the packages: pyttsx3, PyPDF2 & pdfplumber.
pip install pyttsx3 pdfplumber PyPDF2
Once you have those packages installed, you can begin writing the program.
The first thing that I like to do is put a description in comments about the program before writing any logic for the code.
#Description: This program converts a pdf to a audio book.
Next, import the libraries that will be used throughout the program.
Next get the file location and name.
file = 'C:/Users/<user_name>/Desktop/The_Masked_Programmer.pdf'
From the above code snippet you can see the file name is “The_Masked_Programmer.pdf” and it is located on the users desktop.
Now, we want to get the number of pages in the PDF file.
#Creating a PDF File Object
pdfFileObj = open(file, 'rb')
# creating a pdf reader object
pdfReader = PyPDF2.PdfFileReader(pdfFileObj)
#Get the number of pages
pages = pdfReader.numPages
Create a pdfplumber object and loop through the number of pages to extract the text and then convert that text to speech. I will print the text to the screen just to see the text that should be converted to speech.
#Create a pdfplumber object and loop through the pages
with pdfplumber.open(file) as pdf:
#Loop through the number of pages
for i in range(0, pages):
page = pdf.pages[i]
text = page.extract_text()
speaker = pyttsx3.init()
That’s it ! You are done. Hopefully with some improvements, this can be more useful to you all !
If you are interested in reading more on Python one of the fastest growing programming languages that many companies and computer science departments use, then I recommend you check out the book Learning Python written by Mark Lutz’s.
Thanks for reading this article I hope its helpful to you all! If you enjoyed this article and found it helpful please leave some claps to show your appreciation. Keep up the learning, and if you like machine learning, mathematics, computer science, programming or algorithm analysis, please visit and subscribe to my YouTube channels (randerson112358 & compsci112358 ).