forked from kishanrajput23/Hacktoberfest_2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
audiobook.py
33 lines (26 loc) · 970 Bytes
/
audiobook.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import PyPDF2
import pyttsx3
# taking user input to get the pdf to be read(Make sure the pdf is available on the current directory)
pdf_name = input("Enter the name of the pdf to be listened:- ")
pdf_name = pdf_name + '.pdf'
book = open(pdf_name, 'rb')
pdfReader = PyPDF2.PdfFileReader(book)
pages = pdfReader.numPages
speaker = pyttsx3.init()
# set the voice rate
speaker.setProperty('rate', 200)
# set volume
volume = speaker.getProperty('volume')
speaker.setProperty('volume', 1.0)
# set the voice property voices[0] is male and voices[1] is female
v = input("Voice you need : male or female:- ")
x = v.lower()
dict = {'male': 0, 'female': 1}
voices = speaker.getProperty('voices')
speaker.setProperty('voice', voices[dict[x]].id)
# It skips the first page since numbering starts from 0.
for num in range(11, pages):
page = pdfReader.getPage(num)
text = page.extractText()
speaker.say(text)
speaker.runAndWait()