Skip to content

Commit 58476ec

Browse files
Create AudioToPdfConverter.py
1 parent d370bb0 commit 58476ec

File tree

1 file changed

+116
-0
lines changed

1 file changed

+116
-0
lines changed

AudioToPdfConverter.py

+116
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
from tkinter import *
2+
import tkinter.messagebox as mb
3+
from path import Path
4+
from PyPDF4.pdf import PdfFileReader as PDFreader, PdfFileWriter as PDFwriter
5+
import pyttsx3
6+
from speech_recognition import Recognizer, AudioFile
7+
from pydub import AudioSegment
8+
import os
9+
10+
11+
# Initializing the GUI window
12+
class Window(Tk):
13+
def __init__(self):
14+
super(Window, self).__init__()
15+
self.title("PDF to Audio and Audio to PDF converter")
16+
self.geometry('400x250')
17+
self.resizable(0, 0)
18+
self.config(bg='Black')
19+
20+
Label(self, text='PDF to Audio and Audio to PDF converter',
21+
wraplength=400, bg='Black',
22+
font=("Comic Sans MS", 15)).place(x=0, y=0)
23+
24+
Button(self, text="Convert PDF to Audio",
25+
font=("Comic Sans MS", 15), bg='cyan',
26+
command=self.pdf_to_audio, width=25).place(x=40, y=80)
27+
28+
Button(self, text="Convert Audio to PDF",
29+
font=("Comic Sans MS", 15), bg='cyan',
30+
command=self.audio_to_pdf, width=25).place(x=40, y=150)
31+
32+
def pdf_to_audio(self):
33+
pta = Toplevel(self)
34+
pta.title('Convert PDF to Audio')
35+
pta.geometry('500x300')
36+
pta.resizable(0, 0)
37+
pta.config(bg='cyan')
38+
Label(pta, text='Convert PDF to Audio', font=('Comic Sans MS', 15), bg='cyan').place(relx=0.3, y=0)
39+
Label(pta, text='Enter the PDF file location (with extension): ', bg='cyan', font=("Verdana", 11)).place(
40+
x=10, y=60)
41+
filename = Entry(pta, width=32, font=('Verdana', 11))
42+
filename.place(x=10, y=90)
43+
Label(pta, text='Enter the page to read from the PDF (only one can be read): ', bg='cyan',
44+
font=("Verdana", 11)).place(x=10, y=140)
45+
page = Entry(pta, width=15, font=('Verdana', 11))
46+
page.place(x=10, y=170)
47+
Button(pta, text='Speak the text', font=('Gill Sans MT', 12), bg='Snow', width=20,
48+
command=lambda: self.speak_text(filename.get(), page.get())).place(x=150, y=240)
49+
50+
def audio_to_pdf(self):
51+
atp = Toplevel(self)
52+
atp.title('Convert Audio to PDF')
53+
atp.geometry('675x300')
54+
atp.resizable(0, 0)
55+
atp.config(bg='cyan')
56+
Label(atp, text='Convert Audio to PDF', font=("Comic Sans MS", 15), bg='cyan').place(relx=0.36, y=0)
57+
Label(atp, text='Enter the Audio File location that you want to read [in .wav or .mp3 extensions only]:',
58+
bg='cyan', font=('Verdana', 11)).place(x=20, y=60)
59+
audiofile = Entry(atp, width=58, font=('Verdana', 11))
60+
audiofile.place(x=20, y=90)
61+
Label(atp, text='Enter the PDF File location that you want to save the text in (with extension):',
62+
bg='cyan', font=('Verdana', 11)).place(x=20, y=140)
63+
pdffile = Entry(atp, width=58, font=('Verdana', 11))
64+
pdffile.place(x=20, y=170)
65+
Button(atp, text='Create PDF', bg='Snow', font=('Gill Sans MT', 12), width=20,
66+
command=lambda: self.speech_recognition(audiofile.get(), pdffile.get())).place(x=247, y=230)
67+
68+
@staticmethod
69+
def speak_text(filename, page):
70+
if not filename or not page:
71+
mb.showerror('Missing field!', 'Please check your responses,'
72+
'because one of the fields is missing')
73+
return
74+
reader = PDFreader(filename)
75+
engine = pyttsx3.init()
76+
with Path(filename).open('rb'):
77+
page_to_read = reader.getPage(int(page) - 1)
78+
text = page_to_read.extractText()
79+
engine.say(text)
80+
engine.runAndWait()
81+
82+
@staticmethod
83+
def write_text(filename, text):
84+
writer = PDFwriter()
85+
writer.addBlankPage(72, 72)
86+
pdf_path = Path(filename)
87+
with pdf_path.open('ab') as output_file:
88+
writer.write(output_file)
89+
output_file.write(text)
90+
91+
def speech_recognition(self, audio, pdf):
92+
if not audio or not pdf:
93+
mb.showerror('Missing field!', 'Please check your responses, '
94+
'because one of the fields is missing')
95+
return
96+
audio_file_name = os.path.basename(audio).split('.')[0]
97+
audio_file_extension = os.path.basename(audio).split('.')[1]
98+
if audio_file_extension != 'wav' and audio_file_extension != 'mp3':
99+
mb.showerror('Error!', 'The format of the audio file should '
100+
'only be either "wav" and "mp3"!')
101+
if audio_file_extension == 'mp3':
102+
audio_file = AudioSegment.from_file(Path(audio), format='mp3')
103+
audio_file.export(f'{audio_file_name}.wav', format='wav')
104+
source_file = f'{audio_file_name}.wav'
105+
r = Recognizer()
106+
with AudioFile(source_file) as source:
107+
r.pause_threshold = 5
108+
speech = r.record(source)
109+
text = r.recognize_google(speech)
110+
self.write_text(pdf, text)
111+
112+
113+
# Finalizing the GUI window
114+
app = Window()
115+
app.update()
116+
app.mainloop()

0 commit comments

Comments
 (0)