|

How To Read Text From A PDF Aloud With Python (Text To Speech)

I was just wondering if there was a way to read pdf files out loud using a text to speech engine with python.

I found a simple script here:

https://www.geeksforgeeks.org/convert-pdf-file-text-to-audio-speech-using-python/

I tried it out, and it works!

You only need to install 2 libraries:

  • pdfReader
  • pyttsx3

And then run this simple script:

# importing the modules
import PyPDF2
import pyttsx3

# path of the PDF file
path = open('file.pdf', 'rb')

# creating a PdfFileReader object
pdfReader = PyPDF2.PdfReader(path)

# the page with which you want to start
# this will read the page of 25th page.
from_page = pdfReader.pages[1]

# extracting the text from the PDF
text = from_page.extract_text()

# reading the text
speak = pyttsx3.init()
speak.say(text)
speak.runAndWait()

Again, if you want to see the full code, go to the source article here on the GeeksForGeeks blog.

If you learn faster from audio like me, this can help you speed through the dense content that’s buried within text in a PDF which is harder and slower for me to process.