Designing an Audiobook using Python

In this tutorial, we will learn how to Design an Audiobook using Python. In this project, we have used Python libraries( Pyttsx3 and PyPDF2) and used them to create an audiobook and saved the audio for later use.

Let’s Import some Libraries that we have to use here.

import Pyttsx3
import PyPDF2

library Pyttsx3 for text to speech and PyPDF2 for the pdf to text

Now put a PDF in our directory so we can open our PDF in Python.

Book = open('Stock.pdf', 'rb')
pdfReader = PyPDF2.PdfFileReader(Book)
pages = pdfReader.numPages
# print(pages) to chcek number of pages

Now let’s move on to the next step

Defining Engine so that our Speak function will use the voice we have in our system

speaker = pyttsx3.init()
for num in range(0, pages):
    page = pdfReader.getPage(num) # if we want to listen a specific page then insert the page number here
    text = page.extractText()
    # print(text)  to check if out pdf is ohk & our ertract text is actually extracting text
    speaker.say(text) # to listen to text now 
    speaker.runandwait()

and if you want to change the audio form male to female or vice-versa( use the code below if you need so)

#voices = speaker.getProperty('voices')  # getting details of current voice
#speaker.setProperty('voice', voices[0].id)  # changing index, changes voices. 0 for male
#speaker.setProperty('voice', voices[1].id)  # changing index, changes voices. 1 for female

Now to save our Audiobook

speaker.save_to_file(text,' Audiobook')

we have saved the Audio in our directory as Audiobook we can listen to this in any media player I did try this in VLC  it worked fine for me.

Check out my other works:

Leave a Reply

Your email address will not be published. Required fields are marked *