Build your own Audio book from any PDF with use of Python..๐
Python Tutorial Blog
Hello People,
In this Blog article, we will learn how convert any PDF to an Audio book with just few lines of Python code. It is a fun project to try. it is easy to understand.
Check out the Repository in python. Drop a star if you find it useful !!!
Video Tutorial:
You can watch my YouTube video Tutorial to see a working tutorial for better understanding and a step by step Guide to write a code.
Module Used in Code:
All you need is two python libraries:
โ pyttsx3
โ PyPDF2
That's all you are ready to make this cool program.
If you wish to know more about it, you can refer to pyttsx3 and PyPDF2 Documentation.
Code time:
# import PyPDF2 and pyttsx3 library
import PyPDF2
import pyttsx3
# Open PDF
# where mode='rb' is used for open the file in binary format for reading.
pdf_file = open('demo.pdf', 'rb')
# PDF file reader
pdf_read = PyPDF2.PdfFileReader(pdf_file)
# gives number of Pages in PDF file
num_pages = pdf_read.numPages
engine = pyttsx3.init()
print('Read PDF')
# loop for read out all pages one by one
for n in range(0, num_pages):
# Retrieves a page by number from this PDF file.
page = pdf_read.getPage(n)
# Extract text from page.
text = page.extractText()
x = n + 1
print(f"Reading page {x}/{num_pages}.")
# read text from page.
engine.say(text)
engine.runAndWait()
Thank you.
See you in my next Blog article, Take care!!
Dhruvin Gajjar.
TheCreators
Resources:
ย