From 89fe8d318a96ac38803d2c1bf68538f4ff8c9fca Mon Sep 17 00:00:00 2001 From: godax84 Date: Mon, 3 Feb 2025 03:23:08 -0800 Subject: [PATCH] Carregar ficheiros para "/" --- main.py | 81 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 main.py diff --git a/main.py b/main.py new file mode 100644 index 0000000..81d51c7 --- /dev/null +++ b/main.py @@ -0,0 +1,81 @@ +import tkinter as tk +from tkinter import filedialog, ttk, messagebox +from PyPDF2 import PdfReader, PdfWriter +import os + +class PDFPasswordRemover: + def __init__(self, root): + self.root = root + self.root.title("PDF Password Remover") + self.root.geometry("600x200") + + # File Selection Frame + file_frame = ttk.LabelFrame(root, text="PDF File Selection", padding=10) + file_frame.pack(fill=tk.X, padx=5, pady=5) + + self.file_path = tk.StringVar() + ttk.Entry(file_frame, textvariable=self.file_path, width=60).pack(side=tk.LEFT, padx=5) + ttk.Button(file_frame, text="Browse", command=self.browse_file).pack(side=tk.LEFT) + + # Password Frame + pass_frame = ttk.LabelFrame(root, text="Password", padding=10) + pass_frame.pack(fill=tk.X, padx=5, pady=5) + + self.password = tk.StringVar() + ttk.Entry(pass_frame, textvariable=self.password, show="*", width=30).pack(side=tk.LEFT, padx=5) + + # Buttons + button_frame = ttk.Frame(root, padding=10) + button_frame.pack(fill=tk.X, padx=5) + + ttk.Button(button_frame, text="Remove Password", command=self.remove_password).pack(side=tk.LEFT, padx=5) + ttk.Button(button_frame, text="Exit", command=root.quit).pack(side=tk.RIGHT) + + def browse_file(self): + filename = filedialog.askopenfilename( + title="Select PDF file", + filetypes=[("PDF files", "*.pdf"), ("All files", "*.*")] + ) + if filename: + self.file_path.set(filename) + + def remove_password(self): + if not self.file_path.get(): + messagebox.showerror("Error", "Please select a PDF file") + return + + try: + # Read PDF + reader = PdfReader(self.file_path.get()) + + # Check if PDF is encrypted + if reader.is_encrypted: + # Try to decrypt with provided password + reader.decrypt(self.password.get()) + + # Create writer + writer = PdfWriter() + + # Add pages to writer + for page in reader.pages: + writer.add_page(page) + + # Get output filename + output_file = os.path.splitext(self.file_path.get())[0] + "_unlocked.pdf" + + # Save decrypted PDF + with open(output_file, "wb") as output_pdf: + writer.write(output_pdf) + + messagebox.showinfo("Success", f"Password removed successfully!\nSaved as: {output_file}") + + except Exception as e: + messagebox.showerror("Error", f"Error: {str(e)}") + +def main(): + root = tk.Tk() + app = PDFPasswordRemover(root) + root.mainloop() + +if __name__ == "__main__": + main() \ No newline at end of file