81 lines
3.3 KiB
Python
81 lines
3.3 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
probe_checkbox.py - Untersucht, was pdfplumber an Grafik/Annotationen auf der
|
|
Info-Seite sieht, speziell rund um die YES/NO-Checkbox.
|
|
|
|
Aufruf:
|
|
python3 probe_checkbox.py --pdf ~/data/S/"Sturgill, Garett 040126.pdf"
|
|
"""
|
|
import argparse
|
|
import pdfplumber
|
|
|
|
ANCHOR_INFO = "BUYER INFORMATION SHEET"
|
|
|
|
ap = argparse.ArgumentParser()
|
|
ap.add_argument("--pdf", required=True)
|
|
args = ap.parse_args()
|
|
|
|
with pdfplumber.open(args.pdf) as pdf:
|
|
# Info-Seite per Anker finden
|
|
info_page = None
|
|
for i, page in enumerate(pdf.pages):
|
|
txt = page.extract_text() or ""
|
|
if ANCHOR_INFO in txt:
|
|
info_page = page
|
|
print(f"Info-Seite gefunden: Seite {i+1}\n")
|
|
break
|
|
if info_page is None:
|
|
print("Keine Info-Seite gefunden!")
|
|
raise SystemExit(1)
|
|
|
|
# Finde die Y-Position der Checkbox-Zeile ("INTEREST?" ... "YES" ... "NO")
|
|
words = info_page.extract_words()
|
|
yes_word = no_word = interest_word = None
|
|
for w in words:
|
|
t = w["text"].upper().strip(".:?")
|
|
if t == "YES" and yes_word is None:
|
|
yes_word = w
|
|
elif t == "NO" and no_word is None:
|
|
no_word = w
|
|
elif "INTEREST" in t and interest_word is None:
|
|
interest_word = w
|
|
|
|
print("=== Position der Schluesselwoerter ===")
|
|
for label, w in [("INTEREST", interest_word), ("YES", yes_word), ("NO", no_word)]:
|
|
if w:
|
|
print(f" {label:10} x0={w['x0']:.0f} x1={w['x1']:.0f} top={w['top']:.0f} bottom={w['bottom']:.0f}")
|
|
else:
|
|
print(f" {label:10} NICHT GEFUNDEN")
|
|
|
|
if not (yes_word and no_word):
|
|
print("\nYES/NO nicht beide gefunden - Analyse eingeschraenkt.")
|
|
|
|
# Grafik-Elemente in der Naehe der YES/NO-Zeile untersuchen
|
|
print(f"\n=== Grafik-Elemente (rects/lines/curves) auf der Info-Seite ===")
|
|
print(f" rects: {len(info_page.rects)}")
|
|
print(f" lines: {len(info_page.lines)}")
|
|
print(f" curves: {len(info_page.curves)}")
|
|
|
|
# Die Checkbox-Zeile hat ein bestimmtes 'top'. Zeige alle Rects/Lines in
|
|
# diesem Y-Bereich (kleine Quadrate = Checkboxen, Haekchen = curves/lines).
|
|
if yes_word:
|
|
y_lo = yes_word["top"] - 5
|
|
y_hi = yes_word["bottom"] + 5
|
|
print(f"\n=== Elemente im Y-Bereich der YES/NO-Zeile (top {y_lo:.0f}..{y_hi:.0f}) ===")
|
|
print(" --- Rechtecke (moegliche Checkboxen) ---")
|
|
for r in info_page.rects:
|
|
if y_lo <= r["top"] <= y_hi or y_lo <= r["bottom"] <= y_hi:
|
|
w_ = r["x1"]-r["x0"]; h_ = r["bottom"]-r["top"]
|
|
print(f" x0={r['x0']:.0f} top={r['top']:.0f} groesse={w_:.0f}x{h_:.0f}")
|
|
print(" --- Linien/Kurven (moegliche Haekchen) ---")
|
|
for el in info_page.lines + info_page.curves:
|
|
if y_lo <= el["top"] <= y_hi or y_lo <= el["bottom"] <= y_hi:
|
|
print(f" typ x0={el['x0']:.0f} x1={el['x1']:.0f} top={el['top']:.0f} bottom={el['bottom']:.0f}")
|
|
|
|
# Annotationen (Formularfelder / Widgets)?
|
|
print(f"\n=== Annotationen (Formular-Widgets) ===")
|
|
annots = info_page.annots or []
|
|
print(f" Anzahl: {len(annots)}")
|
|
for a in annots[:15]:
|
|
print(f" {a.get('data',{}).get('Subtype','?')} @ top={a.get('top',0):.0f} "
|
|
f"x0={a.get('x0',0):.0f} {str(a.get('data',{}).get('AS',''))[:30]}") |