Skip to content

Instantly share code, notes, and snippets.

@viniciusCamargo
Last active August 5, 2023 23:26
Show Gist options
  • Save viniciusCamargo/acaafd6830222b3064e9cbec988ca3c7 to your computer and use it in GitHub Desktop.
Save viniciusCamargo/acaafd6830222b3064e9cbec988ca3c7 to your computer and use it in GitHub Desktop.
pdf redact
import tkinter as tk
from tkinter import filedialog
from tkinter import messagebox
import os
import shutil
import fitz
rect_coordinates = (230, 827, 370, 840)
def redact_pdf_area(input_pdf, output_pdf):
# Load the PDF file
pdf_document = fitz.open(input_pdf)
for page in pdf_document:
rect = fitz.Rect(rect_coordinates)
# Add a redaction annotation to the specified area
page.add_redact_annot(rect, fill=(1, 1, 1))
# Apply the redactions on the page
page.apply_redactions()
if os.path.exists(output_pdf):
# Save the modified PDF to a temporary file
temp_output_pdf = output_pdf + ".temp"
pdf_document.save(temp_output_pdf)
pdf_document.close()
# Rename the temporary file to the original filename
os.remove(output_pdf)
shutil.move(temp_output_pdf, output_pdf)
else:
pdf_document.save(output_pdf)
pdf_document.close()
selected_files = []
def update_selected_files_list():
selected_files_listbox.delete(0, tk.END)
for file_path in selected_files:
file_name = os.path.basename(file_path)
selected_files_listbox.insert(tk.END, file_name)
def open_file_dialog():
global selected_files
file_paths = filedialog.askopenfilenames(filetypes=[("Arquivos PDF", "*.pdf")], multiple=True)
if file_paths:
selected_files = list(file_paths)
# print("Selected Files:")
# for file_path in selected_files:
# print(file_path)
# Check if selected_files is empty and disable the folder_button accordingly
folder_button.config(state="normal" if selected_files else "disabled")
update_selected_files_list()
def open_folder_dialog():
global selected_files
folder_path = filedialog.askdirectory()
if folder_path:
# print("Selected Folder:", folder_path)
try:
# Check if the folder contains files with the same names as the selected files
for file_path in selected_files:
file_name = os.path.basename(file_path)
new_file_path = os.path.join(folder_path, file_name)
if os.path.exists(new_file_path):
response = messagebox.askyesno("Substituir esse troço?", f"Já tem um arquivo com esse nome nessa pasta, seu jegue. '{file_name}'. Vai querer substituir? Aí tu vai perder o original, se liga.")
if response == True:
# Replace the file
# print(f"Replacing file '{file_name}' in the folder.")
# print(file_path)
redact_pdf_area(file_path, new_file_path)
# # Cancel replacing the file
# print(f"File '{file_name}' will not be replaced in the folder.")
else:
# print(file_name)
redact_pdf_area(file_path, new_file_path)
# Display a pop-up message for success
messagebox.showinfo("deu boa", "tá tudo certo")
except Exception as e:
# Display a pop-up message for failure
print(e)
messagebox.showerror("deu ruim", "me chama no whats")
# Create the main window
root = tk.Tk()
root.title("app pro ruan não ser preso por violação de direitos autorais")
# Set the minimum width and height for the window
root.minsize(500, 300)
# Set the maximum width and height for the window
root.maxsize(800, 500)
# Create a button to open the file dialog
file_button = tk.Button(root, text="escolhe os pdf", command=open_file_dialog)
file_button.pack(pady=10)
# Create a Label for the listbox
label = tk.Label(root, text="esses são os pdf que tu selecionou")
label.pack()
# Create a Listbox to display the selected file names
selected_files_listbox = tk.Listbox(root, width=60)
selected_files_listbox.pack(pady=10)
# Create a button to open the folder dialog
folder_button = tk.Button(root, text="escolhe a pasta onde tu quer salvar os pdf", command=open_folder_dialog, state="disabled")
folder_button.pack(pady=10)
# Start the main event loop
root.mainloop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment