Python has a lot of modules that can be used for a variety of purposes. In this tutorial, We are Going to Build our own Audiobook with just a Few simple Lines of Python code. To create an audiobook in this tutorial, we'll use two python libraries (pyttsx3, PyPDF2).
Let's Get started
Step - 1 Installing pyttsx3 Module
To install this module Just type your terminal
pip install PyPDF2
Let's Look what is the use of this module. This module can extract pdf document information Like "Title", "Author name" etc. You can see more from Here
After successfully installing this Module Let's install our second module PyPDF2
To do this you can simply type
pip install pyttsx3
Let's Look what is the use of this module.
pyttsx3 is a Python text-to-speech conversion library. It operates offline, unlike other libraries, and is compatible with Python 2 and 3.
Now it's time to code our Audiobook
This library is being used to divide a pdf file into pages, read the text on each page, and send the text to the next step.
The first step is to import our package to the code editor we can do this by following the command
import pyttsx3, PyPDF2
Now we want to convert our file to a reading format and save it as a book. The story is the name of my PDF file. And rb stands for reading mode. We can see our code here 👇
book = open('story.pdf', 'rb')
Now we can call PyPDF2.pdfFileReader on the book and we can store this in a variable called book reader Like this 👇
bookreader = PyPDF2.PdfFileReader(book)
Now let's calculate our pages in pdf by num pages method we can do this by declaring a new variable named total_pages Like this 👇
total_pages = bookreader.numPages
Now we can use the init method to start pyttsx3.
speak = pyttsx3.init()
Let's take the text from our page and extract it using extract text and store it on variable text
text = page.extractText()
Now we can look to our full code 👇
import pyttsx3, PyPDF2
book = open('oop.pdf', 'rb')
bookreader = PyPDF2.PdfFileReader(book)
total_pages = bookreader.numPages
speak = pyttsx3.init()
for num in range(7, total_pages):
page = bookreader.getPage(num)
text = page.extractText()
speak.say(text)
speak.runAndWait()
Now we are Done with our Audio Book
Best Baby Photography Perth
ReplyDelete