|
6 | 6 | logger = logging.getLogger("PyPDF2") |
7 | 7 | logger.setLevel(logging.INFO) |
8 | 8 |
|
9 | | - |
10 | 9 | speed_dict = { |
11 | 10 | "slow": 100, |
12 | 11 | "normal": 150, |
13 | 12 | "fast": 200} |
14 | 13 |
|
15 | 14 |
|
16 | | -def speak_text(engine, text): |
| 15 | +def speak_text(engine, text, print=False): |
| 16 | + if print: |
| 17 | + print(text) |
17 | 18 | engine.say(text) |
18 | 19 | engine.runAndWait() |
19 | 20 |
|
@@ -43,7 +44,29 @@ def create_json_book(self, pdf_file_path, password=None): |
43 | 44 | text = pageObj.extractText() |
44 | 45 | book_dict[num] = text |
45 | 46 | return book_dict, pages |
| 47 | + |
| 48 | + def save_audio(self, pdf_file_path, password=None): |
| 49 | + if not os.path.exists(pdf_file_path): |
| 50 | + raise FileNotFoundError("File not found!") |
| 51 | + |
| 52 | + if not pdf_file_path.endswith(".pdf"): |
| 53 | + raise ValueError("File must be a pdf!") |
46 | 54 |
|
| 55 | + with open(pdf_file_path, "rb") as fp: |
| 56 | + basename = os.path.basename(pdf_file_path).split(".")[0] |
| 57 | + os.makedirs(basename, exist_ok=True) |
| 58 | + logging.info('Saving audio files in folder: {}'.format(basename)) |
| 59 | + pdfReader = PyPDF2.PdfFileReader(fp) |
| 60 | + if pdfReader.isEncrypted: |
| 61 | + logging.info("File is encrypted, trying to decrypt...") |
| 62 | + pdfReader.decrypt(password) |
| 63 | + pages = pdfReader.numPages |
| 64 | + for num in range(0, pages): |
| 65 | + pageObj = pdfReader.getPage(num) |
| 66 | + text = pageObj.extractText() |
| 67 | + self.engine.save_to_file(text, os.path.join(basename, basename + "_" + (str(num) + ".mp3"))) |
| 68 | + self.engine.runAndWait() |
| 69 | + |
47 | 70 | def read_book(self, pdf_file_path, password=None): |
48 | 71 | if not os.path.exists(pdf_file_path): |
49 | 72 | raise FileNotFoundError("File not found!") |
|
0 commit comments