Skip to content

Commit 58b5955

Browse files
Merge pull request #13 from codePerfectPlus/save_audiobook
featute: audiobook can be saved now
2 parents a146a46 + f010fe0 commit 58b5955

File tree

2 files changed

+41
-5
lines changed

2 files changed

+41
-5
lines changed

README.md

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,10 @@ pip install audiobook
3131

3232
```python
3333
from audiobook import AudioBook
34-
ab = AudioBook("file_path")
35-
ab.text_to_speech()
34+
ab = AudioBook() # argument: Speech-Speed="slow/normal/fast"
35+
36+
ab.save_audio(file_path, password=None) # save audio file
37+
ab.read_book(file_path, password=None) # listen to the book
3638
```
3739

3840
## Usages
@@ -69,7 +71,18 @@ sudo apt update && sudo apt install espeak ffmpeg libespeak1
6971

7072
## Project status
7173

72-
- Alpha
74+
## V1.0.0
75+
76+
- [x] Save Audio Book locally
77+
- [x] Listen to the book
78+
- [x] Speech-speed control
79+
- [x] Read password protected PDF
80+
- [x] Create json file for the book
81+
82+
## Upcoming Features
83+
84+
- [ ] Support more extensions
85+
7386

7487
## Author
7588

audiobook/main.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@
66
logger = logging.getLogger("PyPDF2")
77
logger.setLevel(logging.INFO)
88

9-
109
speed_dict = {
1110
"slow": 100,
1211
"normal": 150,
1312
"fast": 200}
1413

1514

16-
def speak_text(engine, text):
15+
def speak_text(engine, text, print=False):
16+
if print:
17+
print(text)
1718
engine.say(text)
1819
engine.runAndWait()
1920

@@ -43,7 +44,29 @@ def create_json_book(self, pdf_file_path, password=None):
4344
text = pageObj.extractText()
4445
book_dict[num] = text
4546
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!")
4654

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+
4770
def read_book(self, pdf_file_path, password=None):
4871
if not os.path.exists(pdf_file_path):
4972
raise FileNotFoundError("File not found!")

0 commit comments

Comments
 (0)