Generate PDF Locker GUI application with Python

To be safe, I need to carry some basic documents digitally, such as passports, health reports and other government IDs, and I don’t want other people to have my sensitive information. Therefore, I thought of encrypting these files with a password so that only I can see them.

Therefore, I started to build a GUI application to encrypt these files.

I used two main modules for this project,

  • PyPDF2- Help us extract information, merge documents and encrypt documents, etc. Just run pip install PyPDF2 to install this module.
  • Tkinter- Create GUI applications, it is the only framework built into the Python standard library.

 

Before building a GUI application, we will understand how easy it is to encrypt files using the PyPDF2 module

Encrypted file code

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 use Tkinter to build a GUI application (compared to other GUI frameworks, it has greater functionality)

Code for creating GUI application

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()

I still want to recommend the Python learning group I built myself : 721195303 , all of whom are learning Python. If you want to learn or are learning Python, you are welcome to join. Everyone is a software development party and share dry goods from time to time (only Python software development related), including a copy of the latest Python advanced materials and zero-based teaching compiled by myself in 2021. Welcome friends who are in advanced and interested in Python to join!
 

Guess you like

Origin blog.csdn.net/aaahtml/article/details/113093119