"""PPTXの各スライドが「画像だけ」かを判定する。標準ライブラリのみ。
使い方: python3 check_pptx.py deck.pptx
"""
import re, sys, zipfile

def slides(z):
    names = [n for n in z.namelist() if re.fullmatch(r"ppt/slides/slide\d+\.xml", n)]
    return sorted(names, key=lambda n: int(re.search(r"(\d+)", n.split("/")[-1]).group(1)))

def main(path):
    z = zipfile.ZipFile(path)
    pres = z.read("ppt/presentation.xml").decode("utf8")
    cx, cy = map(int, re.search(r'<p:sldSz cx="(\d+)" cy="(\d+)"', pres).groups())
    print(f"{path}  ({len(slides(z))} slides)")
    all_image_only = True
    for n in slides(z):
        x = z.read(n).decode("utf8")
        pics = len(re.findall(r"<p:pic>", x))
        shapes = len(re.findall(r"<p:sp>", x))
        tables = len(re.findall(r"<a:tbl>", x))
        charts = len(re.findall(r"graphicData[^>]+chart", x))
        texts = [t for t in re.findall(r"<a:t>([^<]*)</a:t>", x) if t.strip()]
        big = False
        for ext in re.findall(r'<p:pic>.*?<a:ext cx="(\d+)" cy="(\d+)"', x, flags=re.S):
            if int(ext[0]) * int(ext[1]) >= 0.9 * cx * cy:
                big = True
        image_only = big and not texts and not tables and not charts
        all_image_only &= image_only
        verdict = "画像だけ" if image_only else ("文字あり" if texts else "判定不能")
        print(f"  {n.split('/')[-1]:12s} pic={pics} text={len(texts)} table={tables} chart={charts} 全面画像={'y' if big else 'n'} -> {verdict}")
    notes = [n for n in z.namelist() if n.startswith("ppt/notesSlides/") and n.endswith(".xml")]
    print(f"  ノート: {len(notes)}件")
    print("  結論:", "全スライドが画像だけ。文字は直接直せない" if all_image_only else "文字として持つスライドがある")

main(sys.argv[1])
