# -*- coding: utf-8 -*- """Vermisst das Tabellenlayout des BVA-Fragenkatalogs. Erkundungsskript (kein Teil der Produktivpipeline): ermittelt Spaltengrenzen, Zeilentrenner, Checkbox-Geometrie und Kapitelkopfzeilen, damit der eigentliche Parser auf verifizierten Koordinaten aufsetzen kann. """ import io import sys from collections import Counter import fitz sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8") PDF = r"C:\Antigravity\Waffensachkundeprüfung\Fragenkatalog_sachkunde_mitAntworten.pdf" doc = fitz.open(PDF) print(f"Seiten: {doc.page_count} Metadaten: {doc.metadata.get('title')!r} / {doc.metadata.get('author')!r}") page = doc[5] # erste Fragenseite print(f"\nSeitengröße: {page.rect}") drawings = page.get_drawings() print(f"Vektorobjekte auf Seite 6: {len(drawings)}") vert_lines = Counter() horiz_lines = Counter() rects = [] for d in drawings: for item in d["items"]: if item[0] == "l": # Linie p1, p2 = item[1], item[2] if abs(p1.x - p2.x) < 0.6: vert_lines[round(p1.x)] += 1 elif abs(p1.y - p2.y) < 0.6: horiz_lines[round(p1.y)] += 1 elif item[0] == "re": # Rechteck r = item[1] rects.append(r) if r.width < 1.5 and r.height > 3: vert_lines[round(r.x0)] += 1 elif r.height < 1.5 and r.width > 3: horiz_lines[round(r.y0)] += 1 print(f"\nVertikale Trenner (x -> Anzahl): {sorted(vert_lines.items())}") print(f"Horizontale Trenner (y): {sorted(horiz_lines.items())[:40]}") # Checkbox-Kandidaten: annähernd quadratische Rechtecke boxes = [r for r in rects if 6 < r.width < 20 and 6 < r.height < 20 and abs(r.width - r.height) < 4] print(f"\nCheckbox-Kandidaten: {len(boxes)}") for b in boxes[:6]: print(f" x0={b.x0:.1f} y0={b.y0:.1f} w={b.width:.1f} h={b.height:.1f}") # Textblöcke mit Spaltenzuordnung print("\n--- Textzeilen Seite 6 mit x-Positionen ---") d = page.get_text("dict") for block in d["blocks"]: if block["type"] != 0: continue for line in block["lines"]: txt = "".join(s["text"] for s in line["spans"]).strip() if not txt: continue x0 = line["bbox"][0] y0 = line["bbox"][1] flags = {s["flags"] for s in line["spans"]} fonts = {s["font"] for s in line["spans"]} print(f" x={x0:6.1f} y={y0:6.1f} {txt[:62]!r} fonts={fonts} flags={flags}")