PDF Locker using Python Tkinter

pdf locker using python tkinter

Hello coders we create this project with Python Tkinter 

  •   In this project we create a PDF locking  programm.

Libraries used-

  >>Tkinter(for GUI)

  >>PYPDF2(for locking pdf)

Step to create GUI-

  • Install Tkinter using pip install
  • Import Tkinter
  • Create the main window
  • Import png button

Algorithm-

  • Create window and entry box for Password
  •  Import png over buttons
  • Use PyPDF2 to lock the pdf in function
  • Use tkinter filedialog to ask to save file
  • Create button and add command    
#importing Libraries
from tkinter import *
import os
from tkinter.filedialog import askopenfilename,asksaveasfilename
from PyPDF2 import PdfFileWriter,PdfFileReader

 

root = Tk()
root.title("PDF LOCKER")
root.minsize(350,300)

def openfile():
    global file
    print(E1.get())
    file=askopenfilename(defaultextension='.pdf')

    if file=='':
        file=None
    else:
        f=open(file,'rb')
        
        f.close()
        

def lock():
    location=os.path.normpath(file)
    pdfwriter = PdfFileWriter()
    pdf = PdfFileReader(str(location))
    for page_num in range(pdf.numPages):
        pdfwriter.addPage(pdf.getPage(page_num))
        pdfwriter.encrypt(E1.get())
        File=asksaveasfilename(initialfile="Untitled.pdf",defaultextension=".pdf")
        with open(File,'wb') as f:
            pdfwriter.write(f)

#importing image
press_png = PhotoImage(file='pdf.png')
lock_png = PhotoImage(file='lock.png')


E1 = Entry(root,font=('Nirmala UI',20),bd=1)
E1.place(x=1,y=30,height=50,width=350)
E1.insert(0,"Enter Password here")

def click(event):
    if E1.get()=="Enter Password here":
        E1.delete(0,END)
        E1.insert(0,'')

E1.bind('<FocusIn>',click)


file_btn  = Button(root,image=press_png,borderwidth=0,command=openfile)
file_btn.place(x=10,y=150)
lock_btn  = Button(root,image=lock_png,borderwidth=0,command=lock)
lock_btn.place(x=230,y=150)

label_1 = Label(root,text='choose your file',font=('Nirmala UI',10))
label_1.place(x=10,y=250)
label_2 = Label(root,text='Press to Lock',font=('Nirmala UI',10))
label_2.place(x=230,y=250)


Output:

Here if you have any doubt feel free to connect us +91-7982108418

Write a comment