Open In App

Convert PDF File Text to Audio Speech using Python

Last Updated : 21 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Let us see how to read a PDF that is converting a textual PDF file into audio.

Packages Used:

  • pyttsx3: It is a Python library for Text to Speech. It has many functions which will help the machine to communicate with us. It will help the machine to speak to us
  • PyPDF2: It will help to the text from the PDF. A Pure-Python library built as a PDF toolkit. It is capable of extracting document information, splitting documents page by page, merging documents page by page etc.

Both these modules need to be installed

pip install pyttsx3
pip install PyPDF2

You also need to know about the open() function which will help us to open the PDF in read mode. Knowledge about the OOPS Concept is also recommended.

Approach:

  • Import the PyPDF2 and pyttx3 modules.
  • Open the PDF file.
  • Use PdfFileReader() to read the PDF. We just have to give the path of the PDF as the argument.
  • Use the getPage() method to select the page to be read.
  • Extract the text from the page using extractText().
  • Instantiate a pyttx3 object.
  • Use the say() and runwait() methods to speak out the text.

Now here the code for it

Python3




# importing the modules
import PyPDF2
import pyttsx3
  
# path of the PDF file
path = open('file.pdf', 'rb')
  
# creating a PdfFileReader object
pdfReader = PyPDF2.PdfFileReader(path)
  
# the page with which you want to start
# this will read the page of 25th page.
from_page = pdfReader.getPage(24)
  
# extracting the text from the PDF
text = from_page.extractText()
  
# reading the text
speak = pyttsx3.init()
speak.say(text)
speak.runAndWait()


Output:


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads