|
| 1 | +#dependencies |
| 2 | +# for python 3 |
| 3 | +from tkinter import * |
| 4 | +from tkinter import filedialog |
| 5 | +import PyPDF2 |
| 6 | +import pyttsx3 |
| 7 | + |
| 8 | +# for python 2 uncomment the following and comment the above one |
| 9 | +# from Tkinter import * |
| 10 | +# import tkFileDialog #change tkFileDialog in line 17 |
| 11 | +# import PyPDF2 |
| 12 | +# import pyttsx3 |
| 13 | + |
| 14 | + |
| 15 | +def extract_text(): |
| 16 | + '''To extract text from chosen PDF file''' |
| 17 | + file = filedialog.askopenfile(parent=root, mode='rb', title='Choose a PDF file') |
| 18 | + if file != None: |
| 19 | + pdfReader = PyPDF2.PdfFileReader(file) |
| 20 | + global mytext |
| 21 | + mytext = "" |
| 22 | + for pageNum in range(pdfReader.numPages): |
| 23 | + pageObj = pdfReader.getPage(pageNum) |
| 24 | + mytext += pageObj.extractText() |
| 25 | + file.close() |
| 26 | + |
| 27 | + |
| 28 | +def stop_speaking(): |
| 29 | + '''To stop the TTS engine on Stop click button''' |
| 30 | + engine.stop() |
| 31 | + |
| 32 | + |
| 33 | +def speak_text(): |
| 34 | + '''Function to invoke TTS engine to speak the pdf text''' |
| 35 | + global rate |
| 36 | + global male |
| 37 | + global female |
| 38 | + rate = int(rate.get()) #getting value from rate entry widget |
| 39 | + engine.setProperty('rate', rate) #setting inputted rate |
| 40 | + male = int(male.get()) |
| 41 | + female = int(female.get()) |
| 42 | + all_voices = engine.getProperty('voices') #extracting all voices from engine |
| 43 | + maleVoice = all_voices[0].id #for male voice |
| 44 | + femaleVoice = all_voices[1].id #for female voice |
| 45 | + if (male == 0 and female == 0) or (male == 1 and female == 1): #default voice-> male |
| 46 | + engine.setProperty('voice', maleVoice) |
| 47 | + elif male == 0 and female == 1: |
| 48 | + engine.setProperty('voice', femaleVoice) |
| 49 | + else: |
| 50 | + engine.setProperty('voice', maleVoice) |
| 51 | + |
| 52 | + engine.say(mytext) |
| 53 | + engine.runAndWait() |
| 54 | + |
| 55 | + |
| 56 | + |
| 57 | +def Application(root): |
| 58 | + '''Whole gui app''' |
| 59 | + root.geometry('{}x{}'.format(600, 500)) #for fixed size window |
| 60 | + root.resizable(width=False, height=False) |
| 61 | + root.title("PDF Orateur") |
| 62 | + root.configure(background="#e0ffff") |
| 63 | + global rate, male, female |
| 64 | + |
| 65 | + frame1 = Frame(root, width=500, height=200, bg="#8a2be2") #frame 1 for project name and desc. |
| 66 | + frame2 = Frame(root, width=500, height=450, bg="#e0ffff") #frame 2 for rest part |
| 67 | + frame1.pack(side="top", fill="both") |
| 68 | + frame2.pack(side="top", fill="y") |
| 69 | + |
| 70 | + #frame 1 widgets |
| 71 | + name1 = Label(frame1, text="PDF Orateur", fg="white", bg="#8a2be2", font="Arial 28 bold") |
| 72 | + name1.pack() |
| 73 | + name2 = Label(frame1, text="A simple PDF Audio Reader for you!", fg="white", |
| 74 | + bg="#8a2be2", font="Calibri 15") |
| 75 | + name2.pack() |
| 76 | + |
| 77 | + #frame 2 widgets |
| 78 | + btn = Button(frame2, text='Select PDF file', command=extract_text, activeforeground="red", |
| 79 | + padx="70", pady="10", fg="white", bg="black", font="Arial 12") |
| 80 | + btn.grid(row=0, pady=20, columnspan=2) |
| 81 | + |
| 82 | + rate_text = Label(frame2, text="Enter Rate of Speech:", fg="black",bg="#e0ffff", |
| 83 | + font="Arial 12") |
| 84 | + rate_text.grid(row=1, column=0, pady=15, padx=0, sticky=E) |
| 85 | + rate = Entry(frame2, text="200", fg="black",bg="white", font="Arial 12") |
| 86 | + rate.grid(row=1, column=1, padx=30, pady=15,sticky=W) |
| 87 | + # rate.focus_set() |
| 88 | + |
| 89 | + voice_text = Label(frame2, text="Voice:", fg="black",bg="#e0ffff", font="Arial 12") |
| 90 | + voice_text.grid(row=2, column=0, pady=15, padx=0, sticky=E) |
| 91 | + male = IntVar() #to store male voice option value |
| 92 | + maleOpt = Checkbutton(frame2,text="Male",bg="#e0ffff", |
| 93 | + variable=male, |
| 94 | + onvalue=1, |
| 95 | + offvalue=0) |
| 96 | + maleOpt.grid(row=2, column=1, pady=0, padx=30, sticky=W) |
| 97 | + female = IntVar() #to store female voice option value |
| 98 | + femaleOpt = Checkbutton(frame2,text="Female",bg="#e0ffff", |
| 99 | + variable=female, |
| 100 | + onvalue=1, |
| 101 | + offvalue=0) |
| 102 | + femaleOpt.grid(row=3, column=1, pady=0, padx=30, sticky=W) |
| 103 | + |
| 104 | + submitBtn = Button(frame2, text='Play PDF file', command=speak_text, activeforeground="red", |
| 105 | + padx="60", pady="10", fg="white", bg="black", font="Arial 12") |
| 106 | + submitBtn.grid(row=4,column=0, pady=65) |
| 107 | + |
| 108 | + #stop button |
| 109 | + stopBtn = Button(frame2, text='Stop playing', command=stop_speaking,activeforeground="red", |
| 110 | + padx="60", pady="10", fg="white", bg="black", font="Arial 12") |
| 111 | + stopBtn.grid(row=4, column=1, pady=65) |
| 112 | + |
| 113 | + |
| 114 | + |
| 115 | + |
| 116 | + |
| 117 | +if __name__ == "__main__": |
| 118 | + mytext, rate, male, female = "", 100, 0, 0 #global vars |
| 119 | + engine = pyttsx3.init() |
| 120 | + root = Tk() |
| 121 | + Application(root) |
| 122 | + root.mainloop() |
| 123 | + |
0 commit comments