sec-parser / mistral_pdf_ocr_overlay.py
sefd-anonymous's picture
Upload SEC parser release files
62787e2 verified
#!/usr/bin/env python3
from __future__ import annotations
import argparse
import json
from pathlib import Path
import table_ocr_backends
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(
description=(
"Run OCR on a PDF page, then overlay born-digital PDF cell bboxes "
"and recovered bold/italic/underline formatting onto the returned table HTML."
)
)
parser.add_argument("--pdf", required=True, help="Path to the source PDF.")
parser.add_argument("--page", required=True, type=int, help="1-based PDF page number.")
parser.add_argument(
"--model-id",
default=None,
help="Optional OCR model id override. Defaults to the configured/default PDF page OCR model.",
)
parser.add_argument(
"--input-html",
default=None,
help="Optional path to an existing OCR HTML fragment. When provided, the script skips OCR and only applies the PDF-native overlay.",
)
parser.add_argument(
"--style-overlay-mode",
default="auto",
choices=["none", "attrs_only", "formatting_only", "auto", "aggressive"],
help=(
"Formatting overlay behavior. "
"`formatting_only` preserves OCR text and only injects semantic bold/italic/underline tags. "
"`auto` safely swaps in native styled cell HTML when the text match is strong. "
"`aggressive` prefers native styled cell HTML whenever a cell matches."
),
)
parser.add_argument("--page-render-zoom", type=float, default=None, help="Optional PDF render zoom before OCR.")
parser.add_argument("--output-html", default=None, help="Optional path to write the final annotated HTML.")
parser.add_argument(
"--output-raw-html",
default=None,
help=(
"Optional path to write the original OCR HTML before PDF-native overlay. "
"If omitted and --output-html is set, defaults to a sibling '*.raw.html' file."
),
)
parser.add_argument("--output-json", default=None, help="Optional path to write the full JSON payload.")
return parser.parse_args()
def main() -> None:
args = parse_args()
if args.input_html:
input_html_path = Path(args.input_html).resolve()
input_html = input_html_path.read_text(encoding="utf-8")
payload = table_ocr_backends.overlay_pdf_page_html_with_native_cells(
input_html,
pdf_path=args.pdf,
page_number=max(1, int(args.page)),
effective_model_id=args.model_id or "existing-html+pdf-overlay",
style_overlay_mode=args.style_overlay_mode,
)
else:
payload = table_ocr_backends.transcribe_pdf_page_to_payload(
args.pdf,
page_number=max(1, int(args.page)),
model_id=args.model_id,
page_render_zoom=args.page_render_zoom,
overlay_pdf_cells=True,
style_overlay_mode=args.style_overlay_mode,
)
output_html = str(payload.get("html") or "")
raw_html = str(payload.get("raw_html") or output_html)
output_html_path = Path(args.output_html).resolve() if args.output_html else None
output_raw_html_path = None
if args.output_raw_html:
output_raw_html_path = Path(args.output_raw_html).resolve()
elif output_html_path is not None:
output_raw_html_path = output_html_path.with_name(f"{output_html_path.stem}.raw.html")
if args.output_html:
assert output_html_path is not None
output_html_path.parent.mkdir(parents=True, exist_ok=True)
output_html_path.write_text(output_html, encoding="utf-8")
if output_raw_html_path is not None:
output_raw_html_path.parent.mkdir(parents=True, exist_ok=True)
output_raw_html_path.write_text(raw_html, encoding="utf-8")
if args.output_json:
output_json_path = Path(args.output_json).resolve()
output_json_path.parent.mkdir(parents=True, exist_ok=True)
output_json_path.write_text(json.dumps(payload, indent=2, sort_keys=True) + "\n", encoding="utf-8")
if not args.output_html and not args.output_json:
print(output_html)
return
summary = {
"pdf": str(Path(args.pdf).resolve()),
"page": int(args.page),
"model_id": payload.get("effective_model_id"),
"overlay_applied": bool(payload.get("overlay_applied")),
"overlay_changed_html": output_html != raw_html,
"style_overlay_mode": payload.get("style_overlay_mode"),
"timings_ms": payload.get("timings_ms"),
"output_html": str(output_html_path) if output_html_path is not None else None,
"output_raw_html": str(output_raw_html_path) if output_raw_html_path is not None else None,
"output_json": str(Path(args.output_json).resolve()) if args.output_json else None,
}
print(json.dumps(summary, indent=2, sort_keys=True))
if __name__ == "__main__":
main()