Create your own audiobook using python

Updated: Sep 24


In Python, there is a module 'PYTTSX3' that is used to convert your text into a speech. This library also works in offline mode and is comfortable for both versions of Python (Python2 and Python 3).

To implement this library, First of all, you must have to install this library in your python terminal:


>>pip install pyttsx3

After installing the library now it's time to read the data from our file.

To extract the data from PDF we will use a third-party tool called PyPDF2. PyPDF2 is an unadulterated python PDF toolbox beginning from the pyPdf project.

To install PyPDF2, run the following command from the command line:


   >> pip install PyPDF2

Now our necessary libraries are installed now it's time to make our audiobook and the python code will be:

import pyttsx3
import PyPDF2
book=open('SWOC Analysis.pdf','rb')
#replace the file name with your pdf file name
pdfReader=PyPDF2.PdfFileReader(book)
#Here, we create an object of PdfFileReader class of PyPDF2 module and pass the pdf file object & get a pdf reader object.
pages=pdfReader.numPages
#numPages property gives the number of pages in the pdf file
p1=input("Enetr your name: ")
name=('hi',p1,'I am your audiobook')
print("Number of pages in this pdf are: ",pages)
speaker=pyttsx3.init()
#pyttsx3. init() factory function is used to get a reference to a pyttsx3.
speaker.say(name)
p=('total number of pages in this pdf are',pages)
speaker.say(p)
for i in range(pages):
    page=pdfReader.getPage(i)
    #Retrieves a page by number from this PDF file
    text=page.extractText()
    #extractText() is used to extract text from the pdf page.
    page_no=i+1
    tell=('Page number is',page_no)
    speaker.say(tell)
    speaker.say(text)
    speaker.runAndWait()
    # This function will make the speech audible in the system, if you don't write this command then the speech will not be audible to you

I hope you like this

Thanks

Amandeep Singh


30 views0 comments

Recent Posts

See All