Branchwell

Skillfarm / skills · skillfarm.ai / converted / pptx / scripts / office / helpers / pptx_slide.py

1"""Find the slide-XML defects known or predicted to make PowerPoint refuse or repair the file.
2
3Schema errors are picked by a denylist over lxml's messages, so an unrecognised
4error class is a miss rather than a false alarm. Animations that name a shape
5the slide no longer has are found by reading the part.
6"""
7
8
9from __future__ import annotations
10
11import re
12
13SLIDE_PART_RE = re.compile(
14 r"ppt/(slides|slideLayouts|slideMasters|notesSlides|notesMasters|handoutMasters)"
15 r"/[^/]+\.xml"
16)
17
18FATAL_SLIDE_ERRORS: tuple[tuple[re.Pattern[str], str], ...] = (
19 (
20 re.compile(r"\}tableStyleId': This element is not expected"),
21 "two <a:tableStyleId> in one <a:tblPr> (the schema allows one)",
22 ),
23 (
24 re.compile(r"\}srgbClr', attribute 'val'"),
25 "a colour that is not six hex digits",
26 ),
27 (
28 re.compile(r"\}txBody': Missing child element"),
29 "a <p:txBody> with no children",
30 ),
31 (
32 re.compile(r"\}miter', attribute 'lim'"),
33 'a line join with lim="NaN"',
34 ),
35 (
36 re.compile(r"\}uLnTx': This element is not expected"),
37 "<a:uLnTx> in a position the schema forbids",
38 ),
39 (
40 re.compile(r"\}overrideClrMapping': This element is not expected"),
41 "<p:overrideClrMapping> in a position the schema forbids",
42 ),
43 (
44 re.compile(r"\}nvGrpSpPr': Missing child element"),
45 "a <p:nvGrpSpPr> with no children",
46 ),
47 (
48 re.compile(r"\}(?:off|ext|chOff|chExt)', attribute '\w+': '-?(?:Infinity|NaN)'"),
49 "a position or size that is not a number (check the x, y, w, h you worked out)",
50 ),
51 (
52 re.compile(r"\}prstDash', attribute 'val'"),
53 "an unknown line dash style (for line.dashType try dash, sysDash or solid)",
54 ),
55)
56
57
58def is_schema_verdict(error: str) -> bool:
59 return error.startswith("Element ")
60
61
62_PML = "{http://schemas.openxmlformats.org/presentationml/2006/main}"
63_MC = "{http://schemas.openxmlformats.org/markup-compatibility/2006}"
64
65_NAMES_A_SHAPE = frozenset({f"{_PML}spTgt", f"{_PML}bldP", f"{_PML}bldGraphic"})
66
67
68def _shape_id(value: str | None) -> int | str | None:
69 text = (value or "").strip()
70 if text.isascii() and text.isdigit() and len(text) <= 18:
71 return int(text)
72 return text or None
73
74
75def stranded_animation_targets(root) -> list[str]:
76 timing = root.find(f"{_PML}timing")
77 if timing is None:
78 timing = root.find(f"{_MC}AlternateContent/{_MC}Choice/{_PML}timing")
79 if timing is None:
80 return []
81
82 on_slide = {_shape_id(e.get("id")) for e in root.iter("{*}cNvPr")}
83 named = {_shape_id(e.get("spid")) for e in timing.iter(*_NAMES_A_SHAPE)} - {None}
84 stranded = named - on_slide
85 return [
86 f"<p:timing> animates shape id {spid if isinstance(spid, int) else repr(spid[:16])}, which is gone, so PowerPoint will "
87 f"offer to repair the deck. Fix: delete the slide's whole <p:timing> element "
88 f"(the slide loses its animations), or put the shape back"
89 for spid in sorted(stranded, key=str)
90 ]
91
92
93def fatal_slide_errors(errors: set[str]) -> list[str]:
94 out = []
95 for error in sorted(errors):
96 for pattern, meaning in FATAL_SLIDE_ERRORS:
97 if pattern.search(error):
98 out.append(f"{meaning}: {error}")
99 break
100 return out