"""Compare extractable PDF text locally. Requires Python and PyMuPDF.

Usage: python compare_text.py old.pdf new.pdf > local-diff.txt
This reads files only; it has no OCR, graphic comparison, or network calls.
CC0: HowCurio practice utility.
"""
import sys
import difflib
import pymupdf

def text_lines(filename):
    with pymupdf.open(filename) as doc:
        if doc.needs_pass:
            raise ValueError('Password-protected file: open an authorized readable copy first.')
        return [line for page in doc for line in page.get_text(sort=True).splitlines() if line.strip()]

if __name__ == '__main__':
    if len(sys.argv) != 3:
        raise SystemExit('Usage: python compare_text.py old.pdf new.pdf > local-diff.txt')
    changes = list(difflib.unified_diff(text_lines(sys.argv[1]), text_lines(sys.argv[2]),
                                     fromfile=sys.argv[1], tofile=sys.argv[2], lineterm=''))
    print('\n'.join(changes) if changes else 'No extracted-text differences found. Graphics and other PDF objects were not compared.')
