Building a PDF Locker GUI Application

Subscribe to my newsletter and never miss my upcoming articles

Listen to this article

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,

  • PyPDF2 - Which helps us to extract information, merging documents and encrypting documents etc... Simply run pip install PyPDF2 to install this module.
  • Tkinter - To create GUI application and it's the only framework built into the Python standard library.

Alt Text

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

Code to encrypt files

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)

Code to build GUI App

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

If you like my content, please consider supporting me

Hope it's useful

A ❤️ would be Awesome 😊

No Comments Yet