-
Notifications
You must be signed in to change notification settings - Fork 1
/
audiobook_gen.py
63 lines (46 loc) · 1.67 KB
/
audiobook_gen.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# PDF to audio and audio file converter
import pyttsx3
from PyPDF2 import PdfReader
from pathlib import Path
def read_text(pdf):
# initialising empty content string and reader object
reader = PdfReader(pdf)
content = ""
for page in reader.pages:
# extracting the text from the pdf
text_ = page.extract_text()
content = content + text_
return content
def convert_to_mp3(content, filename):
# a new engine is being created
engine = pyttsx3.init()
# changing the voice of the audio
voices = engine.getProperty("voices")
if len(voices) == 0:
print("Unable to find any convertable voice on your system.")
quit(1)
voice = voices[1] if len(voices) > 1 else voices[0]
engine.setProperty("voice", voice.id)
#changing volume
volume = engine.getProperty('volume')
print ("initial volume: %s" %volume ) #print initial volume level
engine.setProperty('volume', 1.0)
# creating the audio(mp3) file of the pdf content
engine.save_to_file(content, f"./{filename}.mp3")
engine.runAndWait()
if __name__ == "__main__":
try:
path = Path(input("Enter the path to the pdf file: "))
if not path.exists():
print("The path to the file you gave is incorrect/doesn't exist.")
quit(1)
name = input(
"What would you like to name your audiobook (exclude extensions): "
).strip()
print("Reading file...")
content = read_text(path)
print("Converting to audio...")
convert_to_mp3(content, name)
print("Audiobook created.")
except Exception as e:
print(f"An error occured: {e}.")