Zurück

BEK Protect PDF by Bekim Jahmurataj

main.py


    from tkinter import *
    from tkinter import filedialog
    from tkinter import messagebox
    from PyPDF2 import PdfFileWriter, PdfFileReader
    import os
    
    def browse():
        global filename
        filename=filedialog.askopenfilename(initialdir=os.getcwd(),
                                            title="Bitte wählen sie die zu Schützende PDF Datei aus",
                                            filetypes=(('PDF Datei','*.pdf'),('Alle Datein','*.*')))
        entry1.insert(END,filename)
    
    def Protect():
        mainfile=source.get()
        protectfile=target.get()
        code=password.get()
        
        if mainfile=="" and protectfile=="" and password.get=="":
            messagebox.showerror("Fehler","Bitte Daten eingeben !")
        elif mainfile=="":
            messagebox.showerror("Fehler","Bitte PDF Namen eingeben !")
        elif protectfile=="":
            messagebox.showerror("Fehler","Bitte ziel PDF eingeben !")
        elif password.get()=="":
            messagebox.showerror('Fehler', "Bitte Password eingeben")
        else:
            try:
                out=PdfFileWriter()
                file=PdfFileReader(filename)
                num = file.numPages
                
                for idx in range(num):
                    page=file.getPage(idx)
                    out.addPage(page)
                    
                    #password
                    out.encrypt(code)
                    with open(protectfile, "wb") as f:
                        out.write(f)
                    source.set("")
                    target.set("")
                    password.set("")
                    
                    messagebox.showinfo("Info", "Erfolgreich Gesichert !")
                    
            except:
                messagebox.showerror("Fehler","Fehlerhafte Eingabe!!")
                    
    
    root=Tk()
    root.title("BEK-Protect PDF by Bekim Jahmurataj")
    root.geometry("600x430+300+100")
    root.resizable(False,False)
    
    #icon
    image_icon=PhotoImage(file="img/icon.png")
    root.iconphoto(False,image_icon)
    
    #main
    Top_image=PhotoImage(file="img/top-image.png")
    Label(root,image=Top_image).pack()
    
    frame=Frame(root,width=580,height=290,bd=5,relief=GROOVE)
    frame.place(x=10,y=130)
    
    ###########
    source=StringVar()
    Label(frame,text="Ziel PDF Datei", font="arial 10 bold", fg="#4c4542").place(x=30,y=50)
    entry1=Entry(frame, width=30,textvariable=source,font="arial 15", bd=1)
    entry1.place(x=150,y=40)
    
    Button_icon=PhotoImage(file="img/button-image.png")
    Button(frame,image=Button_icon,width=35,height=24,bg="#d3cdcd", command=browse).place(x=500,y=47)
    
    ###########
    target=StringVar()
    Label(frame,text="Dateinamen wählen:", font="arial 10 bold", fg="#4c4542").place(x=30,y=100)
    entry2=Entry(frame, width=30,textvariable=target,font="arial 15", bd=1)
    entry2.place(x=150,y=100)
    
    ###########
    password=StringVar()
    Label(frame,text="Password eingeben:", font="arial 10 bold", fg="#4c4542").place(x=15,y=150)
    entry3=Entry(frame, width=30,textvariable=password,font="arial 15", bd=1)
    entry3.place(x=150,y=150)
    
    button_icon=PhotoImage(file="img/button.png")
    Protect=Button(root,text="PDF Datei schützen",compound=LEFT, image=button_icon,width=230,height=50,bg="#bfb9b9",font="arial 14 bold",command=Protect)
    Protect.pack(side=BOTTOM,pady=40)
    
    
    
    root.mainloop()