In this tutorial, We will learn how to convert pdf to an audiobook using the python program. For this tutorial, you’ll need to have basic understandings of python and python packages. Let’s move on to the coding part.
pip install pyttsx3 pip install PyPDF2
Now copy one of the pdf and paste on the code section like this:
Let’s import the packages:
import pyttsx3 import PyPDF2
Simple program just to convert text to speech
import pyttsx3 engine = pyttsx3.init() engine.say("This tutorial is awesome") engine.say("Wodan.xyz is really good website to learn new things") engine.runAndWait()
This above code will convert the text “This tutorial is awesome”, “Wodan.xyz is a really good website to learn new things” to the speech.
To read Nepali pdf we need to install the Hindi language on our windows. For that you need to click start then follow the following steps:
- Select the Start button, then select Settings > Time & Language > Region & Language.
- Select Add a language and choose Hindi from the list. (Because the Nepali language doesn’t have text to speech capabilities.
- After the new language has been installed (this may take a few moments), select it in the Region & Language list, and then select Options.
- Under Language options > Speech, select Download.
- Restart your computer. The Text-to-Speech voices will be installed when your machine turns back on.
Now let’s print all the voices available:
# Print all available voices import pyttsx3 engine = pyttsx3.init() voices = engine.getProperty('voices') for voice in voices: print("Voice:") print(" - ID: %s" % voice.id) print(" - Name: %s" % voice.name) print(" - Languages: %s" % voice.languages) print(" - Gender: %s" % voice.gender) print(" - Age: %s" % voice.age)
Now you will be able to see the Hindi language on the available voices. Copy the ID of Hindi’s voice.
# Voice IDs pulled from engine.getProperty('voices') # These will be system specific hn_voice_id = "" #here write down voice ID of Hindi engine.setProperty('voice', hn_voice_id)
Now lets open the pdf file :
book = open('BPKoirala2032BS_DoshiChasma.pdf', 'rb') pages = pdfReader.numPages
To read all the pages of pdf we need to create a for loop:
for num in range(1, pages): page = pdfReader.getPage(num) #gets the page number text = page.extractText() #extracts the text from that page speaker.say(text) speaker.runAndWait()
With this code, you can convert Nepali pdf to Nepali speech.
import pyttsx3 import PyPDF2 book = open('BPKoirala2032BS_DoshiChasma.pdf', 'rb') pdfReader = PyPDF2.PdfFileReader(book) pages = pdfReader.numPages engine = pyttsx3.init() for num in range(1, pages): page = pdfReader.getPage(num) text = page.extractText() hn_voice_id = "" engine.setProperty('voice', hn_voice_id) engine.say(text) engine.runAndWait()