Files
ocrmypdf/misc/pdf_text_diff.py
T

58 lines
1.4 KiB
Python
Raw Normal View History

2025-01-01 18:03:15 -08:00
# SPDX-FileCopyrightText: 2025 James R. Barlow
# SPDX-License-Identifier: MPL-2.0
"""Compare text in PDFs."""
from __future__ import annotations
from pathlib import Path
2025-01-01 18:03:15 -08:00
from subprocess import run
from tempfile import NamedTemporaryFile
from typing import Annotated
import cyclopts
2025-01-01 18:03:15 -08:00
app = cyclopts.App()
2025-01-01 18:03:15 -08:00
@app.default
2025-01-01 18:03:15 -08:00
def main(
pdf1: Annotated[Path, cyclopts.Parameter()],
pdf2: Annotated[Path, cyclopts.Parameter()],
*,
engine: Annotated[str, cyclopts.Parameter()] = 'pdftotext',
2025-01-01 18:03:15 -08:00
):
"""Compare text in PDFs."""
with open(pdf1, 'rb') as f1, open(pdf2, 'rb') as f2:
text1 = run(
['pdftotext', '-layout', '-', '-'],
stdin=f1,
capture_output=True,
check=True,
)
text2 = run(
['pdftotext', '-layout', '-', '-'],
stdin=f2,
capture_output=True,
check=True,
)
with NamedTemporaryFile() as t1, NamedTemporaryFile() as t2:
t1.write(text1.stdout)
t1.flush()
t2.write(text2.stdout)
t2.flush()
2025-01-01 18:03:15 -08:00
diff = run(
['diff', '--color=always', '--side-by-side', t1.name, t2.name],
2025-01-01 18:03:15 -08:00
capture_output=True,
)
run(['less', '-R'], input=diff.stdout, check=True)
if text1.stdout.strip() != text2.stdout.strip():
return 1
return 0
if __name__ == '__main__':
app()