Sunday, 28 June 2020

Conversion of text in a pdf file to speech(mp3) file using Python

Some of the things which i have started, and that may be useful to somebody whoever interested in these type of below mentioned idea:

I have an idea about how to convert a text in pdf to speech(mp3) file, that may be helpful to the blind people. And also who are seriously in other works and want to read the pdf file, simply by mentioning their file and can run in a Python script.

The below code can give you an illustration of how i converted the pdf text to mp3 file, Just one thing you can mention, file name(with filepath) you have to mention, no need to worry about installation of any libraries. Just simply copy the code and paste in any .py file and run in your command prompt or pycharm or any IDE for python.

Code:
-------
import os

try:
import PyPDF2
except ImportError:
print("Installing Required modules")
os.system('python -m pip install PyPDF2')

try:
import gtts
except ImportError:
print("Installing Required modules")
os.system('python -m pip install gtts')

import PyPDF2
import gtts
from gtts import gTTS
pdfobj=open('pdf file path','rb')# mention the pdf file in .pdf format
pdfReader = PyPDF2.PdfFileReader(pdfobj)
print(pdfReader.numPages)
text3=''
for t1 in range(1,pdfReader.numPages):
text1=pdfReader.getPage(t1)
text2=text1.extractText()
text3=text3+'\n'+text2
pdfobj.close()
obj=gTTS(text=text3,lang='en',slow=False)
#mention the filename with .mp3 format to save in your local disk
obj.save("mp3file.mp3")
-> Continue coding, All the Best

No comments:

Post a Comment