-
Notifications
You must be signed in to change notification settings - Fork 4
/
Simple_Pdf_To_Audio_Book_Converter.py
42 lines (37 loc) · 1.33 KB
/
Simple_Pdf_To_Audio_Book_Converter.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
# Simple PDF To Audio Book Converter in Python3
# Author: Sriharsha Kotcharlakot
# Date: Thu, 19 Mar 2020
import subprocess
def install(name):
subprocess.call(['pip3', 'install', name])
try:
from PyPDF4 import PdfFileReader, PdfFileWriter
except:
install("PyPDF4")
from PyPDF4 import PdfFileReader, PdfFileWriter
try:
from gtts import gTTS
except:
install("gtts")
from gtts import gTTS
def extract_text(pdf_path):
pdf_writer = PdfFileWriter()
pdf_reader = PdfFileReader(pdf_path, 'rb')
file_writer = open('Intermediate_Text_Output_For_Audio_Conversion.txt', 'w')
i = 0
for i in range(pdf_reader.getNumPages()):
page = pdf_reader.getPage(i)
content = page.extractText()
file_writer.write(content)
file_writer.close()
print('Please be patient to endure several empty minutes! And be sure to have a stable internet connection.')
file_reader = open('Intermediate_Text_Output_For_Audio_Conversion.txt', 'r')
content = file_reader.read()
language = 'en'
gtts_obj = gTTS(text = str(content), lang = language, slow=False)
gtts_obj.save("Audio_Book.mp3")
print('Audio book has been generated and saved as Audio_Book.mp3')
print("!! Welcome to Simple PDF To Audio Book Converter !!")
print("Enter path to the required PDF:")
path = input()
extract_text(path)