Convert PDF to Audio file

Posted by Afsal on 17-Nov-2023

Hi pythonistas!

Today we are going to teach an interesting topic which is very useful to everyone. If you are too lazy to read the PDF books you can convert to audio file and listen. In our last posts we have already learned how to read text from PDF using pyPDF2  and how to convert  text to audio using pyttsx3. Please go through these posts for more details. Let us dive into the code.


import pyttsx3
from PyPDF2 import PdfReader

text = ''
reader = PdfReader("sample.pdf")
pages = reader.pages
for page in pages:
    text += page.extract_text()

engine = pyttsx3.init()
engine.save_to_file(text, 'test.mp3')

Here we are reading text from the sample.pdf files and feeding it to pyttsx3 save_to_file method. Once the code is executed a test.mp3 is created with content same as the PDF file.

Hope you have learned something from this post.  Please share your valuable suggestions with