Building a PDF Locker GUI Application
Tags: python, tutorial, tkinter
Published: 2021-01-23 23:26:06

{% spotify spotify:episode:23wpmgMmdy1rYHwudjez0u?si=3fC8uoKkSuOBzyPebrZfBA %}

I'm a **Indian** and I recently moved to the **USA** to pursue my Master's degree at **UT Arlington**. So I need to carry some essential documents digitally like passport, health reports and other Government id's to be on the safer side and I don't want others to have my sensitive information. So I thought of encrypting these files with a password so that only I can see. 

So I started building a GUI Application which encrypts these files.

Well, I'm using two major modules for this project,
<ul>
<li>
<b>PyPDF2</b> - Which helps us to extract information, merging documents and encrypting documents etc...

Simply run <b>pip install PyPDF2</b> to install this module.
</li>
<li>
<b>Tkinter</b> - To create GUI application and it's the only framework built into the Python standard library.
</li>
</ul>

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/5a6jkqa95by19lh9nzuu.png)


Before building a GUI Application, we will see how easy it is to encrypt files using PyPDF2 module

<b>Code to encrypt files</b>

```python
import PyPDF2

#Locate pdf file inside PdfFileReader funtion
pdf_reader = PyPDF2.PdfFileReader(pdf_file)
pdf_writer = PyPDF2.PdfFileWriter()
for page_num in range(pdf_reader.numPages):
    pdf_writer.addPage(pdf_reader.getPage(page_num))
#encrypt method encrypts files with given password
pdf_writer.encrypt("password")
            
#create a pdf file and make it in wb mode           
result_pdf = open('Lockedfile.pdf','wb')  
pdf_writer.write(result_pdf)
#Close the file
result_pdf.close()

```


Now we will build a GUI Application using Tkinter (has a bigger community which compared to other GUI frameworks)

<b>Code to build GUI App</b>
``` python
import tkinter as tk
from tkinter import messagebox
import PyPDF2
from PIL import Image,ImageTk
from tkinter.filedialog import askopenfile

root = tk.Tk()
root.title("PDF Locker")

canvas =  tk.Canvas(root,width=600,height=300)
canvas.grid(columnspan=3)

#logo
logo = Image.open('/Users/sunilaleti/Desktop/logo.png')
logo = ImageTk.PhotoImage(logo)
logo_label = tk.Label(image=logo)
logo_label.image=logo
logo_label.grid(column=1,row=0)

#instructions
instructions=tk.Label(root,text="Enter a password and select a pdf to encrypt\n")
instructions.grid(columnspan=3,column=0,row=1)

#Creating a input field for password 
password=tk.Entry(root,show="*",width=15)
password.grid(column=1,row=2)

def open_file():
    pdf_file=askopenfile(parent=root,mode="rb",title="choose a file",filetypes=[("PDF Files"," *.pdf")])
    FileName=file.name.split(".")[0]
    if pdf_file is not None:
        pdf_reader = PyPDF2.PdfFileReader(pdf_file)
        pdf_writer = PyPDF2.PdfFileWriter()
        for page_num in range(pdf_reader.numPages):
            pdf_writer.addPage(pdf_reader.getPage(page_num))
        pdf_writer.encrypt(password.get())
        encryptedFile=FileName+"_Encrypted.pdf"
        result_pdf = open(encryptedFile,'wb')  
            
        pdf_writer.write(result_pdf)
        result_pdf.close()
        #To clear input field 
        password.delete(0, 'end')
        #Message box to show success message
        messagebox.showinfo("Success","File encrypted successfully")
    else:
        messagebox.showerror("Failed","Unable to encrypt file")
        


#Creating "Browse file" button using tk.Button
browse_btn=tk.Button(root,text="Browse file",command=lambda:open_file(),width="15",height="2")
browse_btn.grid(column=1,row=4)

canvas=tk.Canvas(root,width=600,height=250)
canvas.grid(columnspan=3)

root.mainloop()
```

The file will be encrypted and you have to enter the password to access it
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/jltse5whkuumlb208cze.png)


{% youtube Md61aBTZOWQ %}

<a href="https://www.buymeacoffee.com/sunilaleti" target="_blank"><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1g0v3nxthxjw79prdacu.png"></a>



Hope it's useful

A :heart: would be Awesome :blush:

Thanks to all readers/viewers who helped me to cross 85K+ views