Comparing Two PDF Contract Versions Word by Word with Python

PDFs have no built-in change tracking, so comparing two versions of a contract means computing the difference between the files. The article weighs three approaches: pixel-level comparison of rendered pages, line-by-line comparison of extracted text, and word-level comparison. It tests all three on a fictional design-services contract that returns with five changes, including a payment deadline moved from 30 to 60 days and altered revision and ownership terms. A reformatted copy containing identical words was also tested to see which method is robust to layout changes. Word-level diffing, implemented in about 120 lines of Python using PyMuPDF and difflib, detected the same five changes in both files, while pixel and line diffs were thrown off by reflowed text. The method also writes the changes back into the PDF as comments.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.

Discussion (0)
Log in to join the discussion and vote.
Log in