Welcome folks today in this blog post we will be building a pdf to ms word docx
converter gui desktop app in tkinter using pypdf2
library in python. All the full source code of the application is given below.
Get Started
First of all install the below library using the pip
command as shown below
pip install pypdf2
In order to get started you need to make an app.py
file and copy paste the following code
app.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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
from tkinter import * from tkinter import filedialog from tkinter.filedialog import askopenfilename,asksaveasfile from PyPDF2 import PdfFileReader #=================open file method====================== def openFile(): file = askopenfilename(defaultextension=".pdf", filetypes=[("Pdf files","*.pdf")]) if file == "": file = None else: fileEntry.delete(0,END) fileEntry.config(fg="blue") fileEntry.insert(0,file) def convert(): try: pdf = fileEntry.get() pdfFile = open(pdf, 'rb') # creating a pdf reader object pdfReader = PdfFileReader(pdfFile) # creating a page object pageObj = pdfReader.getPage(0) # extracting text from page extractedText= pageObj.extractText() readPdf.delete(1.0,END) readPdf.insert(INSERT,extractedText) # closing the pdf file object pdfFile.close() except FileNotFoundError: fileEntry.delete(0,END) fileEntry.config(fg="red") fileEntry.insert(0,"Please select a pdf file first") except: pass def save2word(): text = str(readPdf.get(1.0,END)) wordfile = asksaveasfile(mode='w',defaultextension=".doc", filetypes=[("word file","*.doc"), ("text file","*.txt"), ("Python file","*.py")]) if wordfile is None: return wordfile.write(text) wordfile.close() print("saved") fileEntry.delete(0,END) fileEntry.insert(0,"pdf Extracted and Saved...") #=================== Front End Design root = Tk() root.geometry("600x350") root.config(bg="light blue") root.title("PDF Converter [designed by:CID]") root.resizable(0,0) try: root.wm_iconbitmap("pdf2.ico") except: print('icon file is not available') pass file= "" defaultText = "\n\n\n\n\t\t Your extracted text will apear here.\n \t\t you can modify that text too." #==============App Name==============================================================>> appName = Label(root,text="PDF to WORD Converter ",font=('arial',20,'bold'), bg="light blue",fg='maroon') appName.place(x=150,y=5) #Select pdf file labelFile = Label(root,text="Select Pdf File",font=('arial',12,'bold')) labelFile.place(x=30,y=50) fileEntry = Entry(root,font=('calibri',12),width=40) fileEntry.pack(ipadx=200,pady=50,padx=150) #===========button to access openFile method================================= openFileButton = Button(root,text=" Open ",font=('arial',12,'bold'),width=30, bg="sky blue",fg='green',command=openFile) openFileButton.place(x=150,y=80) #===========button to access convert method================================= convert2Text = Button(root,text="Read",font=('arial',8,'bold'), bg="RED",fg='WHITE',width=20,command=convert) convert2Text.place(x=250,y=120) #======================= Text Box to read pdf file and modify ===================>> readPdf = Text(root,font=('calibri',12),fg='light green',bg='black',width=60,height=30,bd=10) readPdf.pack(padx=20,ipadx=20,pady=20,ipady=20) readPdf.insert(INSERT,defaultText) #===============================Button to access save2word method================= save2Word = Button(root,text="Save to Word File",font=('arial',10,'bold'), bg="RED",fg='WHITE',command=save2word) save2Word.place(x=255,y=320) #===================halt window=============================>> if __name__ == "__main__": root.mainloop() |
Now run the python script by typing the below command as shown below
python app.py