71 lines
2.4 KiB
Python
71 lines
2.4 KiB
Python
#!/usr/bin/env python3
|
|
"""Audit a .pptx presentation for out-of-bounds shapes, overlapping text boxes, and text walls."""
|
|
import json
|
|
import sys
|
|
|
|
|
|
def boxes_overlap(a: tuple[int, int, int, int], b: tuple[int, int, int, int]) -> bool:
|
|
ax1, ay1, ax2, ay2 = a
|
|
bx1, by1, bx2, by2 = b
|
|
ix = min(ax2, bx2) - max(ax1, bx1)
|
|
iy = min(ay2, by2) - max(ay1, by1)
|
|
if ix <= 0 or iy <= 0:
|
|
return False
|
|
smaller = min((ax2 - ax1) * (ay2 - ay1), (bx2 - bx1) * (by2 - by1))
|
|
return smaller > 0 and (ix * iy) / smaller > 0.15
|
|
|
|
|
|
def audit(path: str) -> dict:
|
|
from pptx import Presentation
|
|
|
|
prs = Presentation(path)
|
|
sw, sh = prs.slide_width, prs.slide_height
|
|
out_of_bounds = []
|
|
overlaps = []
|
|
dense_slides = []
|
|
|
|
for idx, slide in enumerate(prs.slides, start=1):
|
|
text_boxes = []
|
|
total_chars = 0
|
|
for s in slide.shapes:
|
|
x1, y1 = int(s.left), int(s.top)
|
|
x2, y2 = x1 + int(s.width), y1 + int(s.height)
|
|
tol = int(sw * 0.01)
|
|
if x1 < -tol or y1 < -tol or x2 > sw + tol or y2 > sh + tol:
|
|
out_of_bounds.append(f"Slide {idx}: '{s.name}' exceeds slide bounds")
|
|
if getattr(s, "has_text_frame", False):
|
|
txt = s.text_frame.text.strip()
|
|
if txt:
|
|
total_chars += len(txt)
|
|
text_boxes.append((s.name, (x1, y1, x2, y2)))
|
|
|
|
for i in range(len(text_boxes)):
|
|
for j in range(i + 1, len(text_boxes)):
|
|
if boxes_overlap(text_boxes[i][1], text_boxes[j][1]):
|
|
overlaps.append(f"Slide {idx}: '{text_boxes[i][0]}' overlaps '{text_boxes[j][0]}'")
|
|
|
|
if total_chars > 320:
|
|
dense_slides.append(f"Slide {idx}: {total_chars} chars (consider splitting or moving detail to speaker notes)")
|
|
|
|
return {
|
|
"file": path,
|
|
"slides": len(prs.slides),
|
|
"out_of_bounds": out_of_bounds,
|
|
"overlapping_text_boxes": overlaps,
|
|
"dense_slides": dense_slides,
|
|
"status": "error" if (out_of_bounds or overlaps) else ("warn" if dense_slides else "ok"),
|
|
}
|
|
|
|
|
|
def main() -> int:
|
|
if len(sys.argv) < 2:
|
|
print("Usage: audit_pptx.py <deck.pptx>", file=sys.stderr)
|
|
return 2
|
|
report = audit(sys.argv[1])
|
|
print(json.dumps(report, ensure_ascii=False, indent=2))
|
|
return 1 if report["status"] == "error" else 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|