Convert the text of PDF file into speech



Hello guys this is Satyam again back with another awesome blog and in this blog I am going to show you how you can convert the text of a PDF file into speech using the pyttsx3 module in Python and to handle PDF files you also need to import the PyPDF2 module in Python as they are not the part of standard library in Python.So you have to install these modules using pip package manager.

pip install pyttsx3

And for installing PyPDF2

pip install PyPDF2


So guys we will be using some methods as PdfFileReader,get_page and extract text from the PyPDF2 module and we will also be using functions such as say and runAndWait from the pyttsx3 module.Now let's see the code below.

# 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)

 

from_page = pdfReader.getPage(2)

  
# extracting the text from the PDF

text = from_page.extractText()

  
# reading the text

speak = pyttsx3.init()
speak.say(text)
speak.runAndWait()




Thanks for Reading

No comments: