From Reading to Listening with Python (Text to Speech)😎...

From Reading to Listening with Python (Text to Speech)😎...
Hello Friends ......
Greetings for a day...
Welcome to Iyashaswini's Tech-Blog.
In today's era no one is having enough time to read every document peacefully and if have time, lots of lack of interest has been generated to read lengthy articles in one go.
So, after thinking alot on this issue, as I wanted each one to go through various articles which may help shaping individual's brain and knowledge and to overcome this issue, I found new interesting way for people like me ......
A platform which allows you to listen to your favorite articles/ebooks without reading it and Putting your headphones ON... and here you go....

Yes you heard it right......

A convinient way to Listen to your long and lengthy articles, ebooks, reports etc at just one button click.
For this, the most convinient way is using Python language with only few lines of code which is easy and affordable for maintainance as well as Light weight source for Listening any source of written material very easily.

Following are the steps to follow in order to build your own application for Text to Voice Conversion:

Step 1 Installing Requirements.
Step 2 Importing Libraries.
Step 3 Writing small code to test whether it is working for 1 line text.
Step 4 Extracting Pdf.
Step 5 Making Python Read.
Step 6 Complete Code.
Now let us deep dive into each step one by one:

Step 1: Installing Requirements

You have to fire below commands in terminal window/panel available in VSCode:

pip install pyttsx3
This means, I’m telling pip to install a package called 'pyttsx3' (Python text to speech) which is a text to speech conversion library in Python.
pip install PyPDF2
This means, I’m telling pip to install a package called 'PyPDF2'
which is a python library used for performing major tasks on PDF files, such as Extracting the document-specific information, Merging the PDF files, Splitting the pages of a PDF file, Adding watermarks to a file, Encrypting and Decrypting the PDF files, etc.

Step 2: Importing Libraries

import pyttsx3
import PyPDF2

Step 3: Writing small code to test whether it is working for 1 line text

import pyttsx3
speak = pyttsx3.init()
speak.say('Hello Welcome to Iyashaswini's Blog')
speak.runAndWait()
Basically, the text quoted as ‘Hello Welcome to Iyashaswini's Blog’ is the actual text that it will read. This is just simple as that.

Step 4: Extracting Pdf

Here, our task is to get a pdf file and extract the number of pages from it.

After that, we’ll look into how to make Python read the text or content from that Pdf.
So in this step, you need a pdf that you want python to read for you.
If it is an ebook then convert it into pdf format.
Here, I’m using 1.pdf for an example.
Now as I have a pdf file, you need to do a few more work:
import PyPDF2
book = open('1.pdf', 'rb')
pdfReader = PyPDF2.PdfFileReader(book)
pages = pdfReader.numPages
print(pages)
You just have to put the above part of the code in our step 3 code after import statement which will look something like below:
import pyttsx3
import PyPDF2
book = open('1.pdf', 'rb')
pdfReader = PyPDF2.PdfFileReader(book)
pages = pdfReader.numPages
print(pages)
speaker = pyttsx3.init()

speaker.say('Hey I can speak')
speaker.runAndWait()
If you run the above code, you can see the number of pages in the console,

Which will be exactly the same as that was in your pdf.

Step 5: Making Python Read

Moving ahead to our next step we will now look into how to make it read the desired page from our pdf.
Till now we have got a PDF Reader and a speaker that can speak anything given to it.
Now to initialize this speaker and speak a single page from our pdf we will take a variable ‘page‘:
speaker = pyttsx3.init()
page = pdfReader.getPage()
In the empty braces'()', you can always put the page number you want it to listen, and code will do the needful for you.
Remember if you want to read page 5 we need to put 4 because in the programming index the number starts from 0.
And to extract text from the variable we need another variable:
speaker = pyttsx3.init()
page = pdfReader.getPage(4)
text = page.extractText()
After getting the text extracted you have to add this ‘text‘ replacing the previous ‘Hey I can speak‘ into the speaker:
speaker = pyttsx3.init()
page = pdfReader.getPage(4)
text = page.extractText()
speaker.say(text)
but it will work for only desired page no.
If we want to do it for entire PDF then we just have to put for loop for that till length of our PDF.
for i in range(0,pages):

    speaker = pyttsx3.init()
    page = pdfReader.getPage(i)
    text = page.extractText()
    speaker.say(text)
    speaker.runAndWait()
That’s it!
Our Audio book is ready ..................

Step 6: Complete Code

import pyttsx3
import PyPDF2
book = open('1.pdf', 'rb')
pdfReader = PyPDF2.PdfFileReader(book)
pages = pdfReader.numPages
print(pages)
for i in range(0,pages):

    speaker = pyttsx3.init()
    page = pdfReader.getPage(i)
    text = page.extractText()
    speaker.say(text)
    speaker.runAndWait()

Please follow below link for detailed code: https://github.com/suryawanshiyashaswini/pdfreader