MAKE AN AUDIOBOOK FROM PDF FILE USING PYTHON
Before getting into this, you need to take a look at how to convert text to speech using python and click here to check out.
Program:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# pip install pyttsx3 | |
import pyttsx3 | |
# pip install PyPDF2 | |
import PyPDF2 | |
# Creating object for pyttsx3 | |
engine = pyttsx3.init() | |
# To open the PDF file | |
book = open("PDF file location", "rb") | |
# create PDF File Reader object | |
pdfreader = PyPDF2.PdfFileReader(book) | |
# to get the no. of pages | |
pages = pdfreader.numPages | |
print(f"No of Pages: {pages}") # prints no. of pages | |
# iterating each page and extracting text from the PDF | |
text = '' | |
for i in range(0, pages): | |
# Getting a page one by one | |
page = pdfreader.getPage(i) | |
# Extracting text and appending to the text variable | |
extract_text = page.extractText() | |
text += extract_text | |
# Converting & storing text to speech in mp3 file | |
engine.save_to_file(text, "filename.mp3") | |
engine.runAndWait() |
Comments