create python pdf protector

Assalam u alaikum friends, in this tutorial we will create a python project in which we will create a PDF Protector with GUI which makes it easy for a user to easily protect a pdf with password. 

So without wasting time let's Start >> But if you wants to watch video then below is the video in which you will learn how to create it. 



Idea:

We have to create a window where we will display a heading and below this heading we will create a Entry box where user will se the selected pdf and after that we will create a button for browsing pdf file and then we will create a separate section where we will create three entry boxes for password, owner name and new pdf name and then in last we will create a button for applying password on pdf.

GUI Setup

First of all we will import tkinter, tkinter filedialogue and pikepdf module which will help us to encrypt pdf and then we will create a function named Screen where we will create our complete GUI. 

Also Read: Create Result Checking System with JavaScript

Here is the complete code of screen function. 


from tkinter import *
from tkinter import filedialog
import pikepdf




def screen():
    global pdf, password, owner, new_pdf
    root = Tk()
    root.title('PDF Protector')
    root.config(bg='gray21')
    root.geometry('400x500')

    heading = Label(root, text='Welcome to PDF Protector', font=('verdana 15 bold'), bg='gray21', fg="white")
    heading.place(x=50, y=20, height=50)

    pdf = StringVar()
    password = StringVar()
    owner = StringVar()
    new_pdf = StringVar()

    pdf_path = Entry(root,  font=('verdana 13'), textvariable=pdf)
    pdf_path.place(x=90, y=70, height=40, width=200)

    browse_button = Button(root, text='Get PDF', font=('verdana 13'), command=browsePDF )
    browse_button.place(x=130, y=120, width=100 )


    label1 = Label(root, text='Enter Password', font=('verdana 13'), bg='gray21', fg="white")
    label1.place(x=70, y=170, height=30)
    entry1 = Entry(root,  font=('verdana 13'), textvariable=password)
    entry1.place(x=70, y=200, height=30, width=200)

    label1 = Label(root, text='Enter Owner name', font=('verdana 13'), bg='gray21', fg="white")
    label1.place(x=70, y=240, height=30)
    entry1 = Entry(root,  font=('verdana 13'), textvariable=owner)
    entry1.place(x=70, y=280, height=30, width=200)

    label1 = Label(root, text='Enter new pdf name', font=('verdana 13'), bg='gray21', fg="white")
    label1.place(x=70, y=320, height=30)
    entry1 = Entry(root,  font=('verdana 13'), textvariable=new_pdf)
    entry1.place(x=70, y=360, height=30, width=200)

    secure_button = Button(root, text='Protect it', font=('verdana 13'), command=secure )
    secure_button.place(x=130, y=400, width=100 )

    root.mainloop()
screen()

In above code we have designed our GUI and we have created four variables "pdf, password, owner, new_pdf" and we have set the values of entry boxes in these variables.

We have made these variables global because we will be using these variables outside this function.

Browse PDF

After completing GUI we will move to create new function that will help us to get the PDF from user's device. In this function we will allow user to select pdf file and then we will set the path to "pdf" variable. So that user will be able to see the selected pdf's path and name.

Also Read:Create Definition generator with JavaScript

Below is the code of this function:


 def browsePDF():
    file_path = filedialog.askopenfilename(filetypes=[("PDF Files", "*.pdf")])
    pdf.set(file_path) 


Encryption Function

After browsing pdf we will create a function that will encrypt our pdf this function will run when user will click on "Protect PDF" button in our GUI. In this function we will get the values of global variables "pdf, password, owner, newpdf" and then we will store these values in new variables and then we will use pikepdf modules' syntax and in which we will set our pdf's value as pdf path and then we will add other values as written in following code.

Also Read: Top 10 Python Projects you must create

Here is the code of this function:


def secure():
    path = pdf.get()
    passwordnew = password.get()
    ownernew = owner.get()
    newname = new_pdf.get()
    
    sample_pdf = pikepdf.Pdf.open(path)
    no_extr = pikepdf.Permissions(extract=False)
    sample_pdf.save(f"E://{newname}.pdf", encryption=pikepdf.Encryption(user=passwordnew, owner=ownernew, allow=no_extr))


Note: Place these functions before the screen function in python file.

Congrats! You did it. 

Now there is a task for you >> Now you have to show the success message to user when user's pdf is protected successfully and remember paste your code snippet in comment here or on YouTube video. 

Thanks For Reading and and Congrats you on Learning Something New Today! 

If  you have not followed us on Facebook or on Whatsapp Channel then please follow us and make sure to Subscribe our Channel.