This repository has been archived by the owner on Nov 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcommandLine.py
executable file
·147 lines (129 loc) · 5.05 KB
/
commandLine.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# Relevant modules
import PyPDF2 as pyp
import pikepdf as pike
from pikepdf import *
import pyttsx3 as pyt
import os
from os import *
from os.path import isfile, join
mypath = "./"
onlyfiles = [f for f in listdir(mypath) if isfile(join(mypath, f))]
pdf_files = [file for file in onlyfiles if ".pdf" == file[(len(file) - 4):(len(file))]]
no_files = len(pdf_files)
print(f"PDF Files detected : {no_files} ")
print(f"Files: {pdf_files}")
def readAloud(file_name, text, voice):
engine = pyt.init()
rate = engine.getProperty('rate')
volume = engine.getProperty('volume')
engine.setProperty('volume', 0.8)
engine.setProperty('rate', 125)
voices = engine.getProperty('voices')
if voice == '-f':
engine.setProperty('voice', voices[1].id)
elif voice == "-m":
engine.setProperty('voice', voices[0].id)
if ".pdf" in file_name:
file_name = file_name.replace('.pdf', "").strip()
print("Generating Audiobook.....")
engine.save_to_file(text, file_name + ".mp3")
engine.runAndWait()
def extract_text(file_name, pdf_begin, pdf_end, voice):
if no_files > 0:
print("Initializing....... ")
if file_name in pdf_files:
if '.pdf' in file_name:
print("Opening file ..... ")
doc = pike.open(file_name)
else:
pass
if doc:
print("Decrypting.... ")
doc.save("convertable.pdf")
pread = pyp.PdfFileReader("convertable.pdf")
if pread:
if pdf_end == "end" or pdf_end == "END":
pdf_end = pread.getNumPages()
else:
pdf_end = int(pdf_end)
if pdf_begin == "start" or pdf_begin == "START":
pdf_begin = 1
else:
pdf_begin = int(pdf_begin)
for y in range(pdf_begin, pdf_end):
page = pread.getPage(y)
text = page.extractText()
return text
if pdf_begin == pdf_end:
page = pread.getPage(pdf_begin)
text = page.extractText()
return text
else:
print("[Error: Could not read text]")
else:
print("[Error: Could not decrypt file]")
else:
print(f"[Error: File not found '{file_name}']")
else:
print("No pdf files detected")
# Contribute to the below function
# To be revisited
def extractAll(pdf_files, voice):
if no_files > 0:
for file in pdf_files:
print(f"opening {file} .....")
doc = pike.open(file)
if doc:
print(f"Decrypting {file} .....")
doc.save(file + "_convertable.pdf")
docread = pyp.PdfFileReader(file + "_convertable.pdf")
if docread:
# Get number of pages
no_pages = docread.getNumPages()
for i in range(no_pages):
page = docread.getPage(i)
text = page.extractText()
return text
readAloud(file, text, voice)
cmd_temp = "aud .conv -f main.pdf 4:6"
while True:
try:
cmd = input("~$ ")
if "aud .conv" in cmd:
for f in pdf_files:
if f in cmd:
if "-f" in cmd:
voice = "-f"
pos_f = cmd.index("-f")
elif "-m" in cmd:
voice = "-m"
pos_f = cmd.index("-m")
else:
print("Voice option invalid")
if ".pdf" in cmd and ':' in cmd:
pos = cmd.index(".pdf")
col = cmd.index(":")
wordInts = ["end", "END", "start", "START"]
pdf_begin = cmd[(pos + 4):(col)].strip()
pdf_end = cmd[col + 1:]
file_name = (cmd[(pos_f + 2):(pos + 4)].strip())
readAloud(file_name, extract_text(file_name, pdf_begin, pdf_end, voice), voice)
else:
print("Invalid syntax , Type aud -help")
if cmd == "aud -help":
help = """
Convert pdf file to an audiobook : [aud .conv -voiceOption filename.pdf startPage:EndPage ]
For instance : aud .conv -f filename.pdf 35:38
Voices: (male and female)
How to use male voice: [aud .conv -m mydocument.pdf startPage:EndPage ]
How to use female voice: [aud .conv -f filename.pdf startPage:EndPage ]
"""
print(help.strip())
except:
print("Oops, An unexpected Error occured")
finally:
print("Exiting....")
conv = [a for a in onlyfiles if 'convertable' in a]
for p in conv:
os.remove(p)
print("Done .")