Hi there,
I want to start off by saying I have 0 experience with coding and am more asking for to see if this would be possible.
I want to create an app that would compare one or many child PDFs to a master PDF, scan through each child PDF highlighting any differences in each child PDF and then save the output of the processed child PDFs with the differences visibly highlighted.
I've seen the ability to compare and output the differences via text but Highlighting the changes directly in each PDF would suit my needs better.
This is what I've got so far, this opens a basic GUI that lets you select a master and 1 child but doesn't seem to do anything when I hit the compare button.
All the PDFs I'm needing to compare are 1 page however I could have 20 versions of the same page, 1 being the master and the other 19 being "child" PDFs
An example of the parent PDF
![[Image: HOd83vV.th.png]](https://iili.io/HOd83vV.th.png)
and an example of the child PDF
I want to start off by saying I have 0 experience with coding and am more asking for to see if this would be possible.
I want to create an app that would compare one or many child PDFs to a master PDF, scan through each child PDF highlighting any differences in each child PDF and then save the output of the processed child PDFs with the differences visibly highlighted.
I've seen the ability to compare and output the differences via text but Highlighting the changes directly in each PDF would suit my needs better.
This is what I've got so far, this opens a basic GUI that lets you select a master and 1 child but doesn't seem to do anything when I hit the compare button.
import tkinter as tk
from tkinter import filedialog, messagebox
import fitz
class PDFCompare:
def __init__(self, master):
self.master = master
master.title("PDF Compare")
self.master_file = None
self.child_file = None
self.result_file = None
self.master_label = tk.Label(master, text="Master PDF:")
self.master_label.grid(row=0, column=0, sticky="w")
self.master_button = tk.Button(master, text="Select", command=self.select_master_pdf)
self.master_button.grid(row=0, column=1, sticky="w")
self.child_label = tk.Label(master, text="Child PDF:")
self.child_label.grid(row=1, column=0, sticky="w")
self.child_button = tk.Button(master, text="Select", command=self.select_child_pdf)
self.child_button.grid(row=1, column=1, sticky="w")
self.compare_button = tk.Button(master, text="Compare", command=self.compare_pdfs)
self.compare_button.grid(row=2, column=0, sticky="w")
def select_master_pdf(self):
self.master_file = filedialog.askopenfilename(title="Select Master PDF", filetypes=[("PDF Files", "*.pdf")])
def select_child_pdf(self):
self.child_file = filedialog.askopenfilename(title="Select Child PDF", filetypes=[("PDF Files", "*.pdf")])
def compare_pdfs(self):
if self.master_file is None or self.child_file is None:
messagebox.showerror("Error", "Please select both master and child PDFs.")
return
try:
master_doc = fitz.open(self.master_file)
child_doc = fitz.open(self.child_file)
except:
messagebox.showerror("Error", "Failed to open PDF files.")
return
result_doc = fitz.open()
for parent_page in master_doc:
child_page = child_doc[int(parent_page.number) - 1]
result = parent_page.compare(child_page)
if result:
diff_rects = result[0].rects
for rect in diff_rects:
highlight = result_doc.add_highlight_annot(rect)
highlight.update()
else:
result_doc.insert_pdf(parent_page)
if not result_doc:
messagebox.showwarning("Warning", "No differences found.")
return
output_file = filedialog.asksaveasfilename(title="Save Output PDF", filetypes=[("PDF Files", "*.pdf")])
if not output_file.endswith(".pdf"):
output_file += ".pdf"
try:
result_doc.save(output_file)
messagebox.showinfo("Success", "Comparison complete. Results saved to {}".format(output_file))
except:
messagebox.showerror("Error", "Failed to save output file.")
root = tk.Tk()
app = PDFCompare(root)
root.mainloop()Some additional info:All the PDFs I'm needing to compare are 1 page however I could have 20 versions of the same page, 1 being the master and the other 19 being "child" PDFs
An example of the parent PDF
![[Image: HOd83vV.th.png]](https://iili.io/HOd83vV.th.png)
and an example of the child PDF
