Convert PDF file to AUDIO using Python
Packages used:
pyttsx3: Python library for text to speech. It will help the machine to speak to us.
pyPDF2: It will help to the text from the PDF
Install both the libraries :
>>pip install pyttsx3
>>pip install pyPDF2
After installing the above packages let’s import them and get started with the task:
Code:
| |
import pyttsx3 | |
import pyPDF2 | |
file = 'C:/Users/<user_name>/Desktop/Book.pdf' | |
#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 | |
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() | |
print(text) | |
speaker = pyttsx3.init() | |
speaker.say(text) | |
speaker.runAndWait() |
No comments
If you have any doubts, Please let me know