# -*- coding: utf-8 -*- """Diagnose: exakte x-Ausdehnung von Antworttext und Ankreuzkästchen.""" 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) box_x0 = Counter() box_x1 = Counter() box_w = Counter() total_boxes = 0 for pno in range(5, doc.page_count): for d in doc[pno].get_drawings(): for item in d["items"]: if item[0] == "re": r = item[1] if 6 < r.width < 20 and abs(r.width - r.height) < 4 and r.x0 > 400: total_boxes += 1 box_x0[round(r.x0)] += 1 box_x1[round(r.x1)] += 1 box_w[round(r.width, 1)] += 1 print(f"Kästchen gesamt (x0>400): {total_boxes}") print(f" x0-Verteilung: {sorted(box_x0.items())}") print(f" x1-Verteilung: {sorted(box_x1.items())}") print(f" Breiten: {sorted(box_w.items())}") # Maximale Textausdehnung in der Antwortspalte, getrennt nach Seiten mit/ohne Kästchen max_x1_with = Counter() max_x1_without = Counter() for pno in range(5, doc.page_count): page = doc[pno] boxes = [] for d in page.get_drawings(): for item in d["items"]: if item[0] == "re": r = item[1] if 6 < r.width < 20 and abs(r.width - r.height) < 4 and r.x0 > 400: boxes.append(r) for x0, y0, x1, y1, text, *_ in page.get_text("words"): if y0 < 92 or x0 < 292 or not text.strip(): continue near_box = any(abs(b.y0 - y0) < 12 for b in boxes) (max_x1_with if near_box else max_x1_without)[round(x1 / 5) * 5] += 1 print(f"\nTextende x1 (gerundet auf 5) in Antwortspalte, Zeilen MIT Kästchen:") print(f" {sorted(max_x1_with.items())[-8:]}") print(f"Textende x1, Zeilen OHNE Kästchen (Freitext):") print(f" {sorted(max_x1_without.items())[-8:]}") # Kollidiert Text jemals mit einem Kästchen? collisions = 0 examples = [] for pno in range(5, doc.page_count): page = doc[pno] boxes = [] for d in page.get_drawings(): for item in d["items"]: if item[0] == "re": r = item[1] if 6 < r.width < 20 and abs(r.width - r.height) < 4 and r.x0 > 400: boxes.append(r) if not boxes: continue for x0, y0, x1, y1, text, *_ in page.get_text("words"): if y0 < 92 or not text.strip(): continue for b in boxes: if x0 < b.x1 and x1 > b.x0 and y0 < b.y1 and y1 > b.y0: collisions += 1 if len(examples) < 5: examples.append((pno + 1, text, round(x0), round(b.x0))) print(f"\nText-Kästchen-Überlappungen: {collisions} Beispiele: {examples}") # Wie viele Fragen hätten mit Schwelle 495 statt 505 ein Kästchen? for threshold in (495, 500, 505): n = sum(1 for pno in range(5, doc.page_count) for d in doc[pno].get_drawings() for item in d["items"] if item[0] == "re" and 6 < item[1].width < 20 and abs(item[1].width - item[1].height) < 4 and item[1].x0 > threshold) print(f"Kästchen bei Schwelle x0>{threshold}: {n}")