Audio Book using python
we can convert any pdf to an audiobook using python.
Only two Python libraries are required.
1. pyttsx3
2. PyPDF2
and you need to import your pdf document under this project folder too.
let's start
💻step 1:
import both libraries using import keyword,
import pyttsx3
import PyPDF2
💻step 2:
we need to open our pdf document using the open function.
open syntax
book = open(“filename”, “mode”)
where book is the variable to add the file object. filename is your pdf file name.
book = open('event.pdf', 'rb')
💻step 3:
call pdf reader function to read your pdf document.
pdfReader = PyPDF2.PdfFileReader(book)
💻step 4:
Get pdf pages details for example 200 page pdf or 1000 page pdf document
print(page) command is just optional.
page = pdfReader.numPages
print(page)//optional
💻step 5:
initialize speaker and save it into speaker variable
speaker = pyttsx3.init()
💻step 6:
define which page you gonna read, using the get page function.
here I am going to read page 1.
page = pdfReader.getPage(1)
💻step 7:
just extract the Text from your selected page and store it any variable, in this case, I am using txt variable.
txt = page.extractText()
💻step 8:
speaker.say(txt)
full coding 👇👇👇 simply copy and paste ..... enjoy 🙋🙋🙋🙋
import pyttsx3
import PyPDF2
book = open('event.pdf', 'rb')
pdfReader = PyPDF2.PdfFileReader(book)
page = pdfReader.numPages
print(page)
speaker = pyttsx3.init()
page = pdfReader.getPage(1)
txt = page.extractText()
speaker.say(txt)
speaker.runAndWait()
waiting for your feedback......
Comments
Post a Comment