summaryrefslogtreecommitdiff
path: root/skills/pptx/scripts/slide_text.py
blob: c583efabc4fc3e5d3b4d569fe9e5ca77d8dc5249 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
"""Print each slide of a .pptx, .pptm, .potx or .ppsx as markdown under `<!-- Slide number: N -->`.

Title, text, tables, chart data, picture alt text and speaker notes; shapes top to bottom.

Usage: python slide_text.py deck.pptx
"""

import os
import re
import sys
import zipfile

from pptx.package import Package


def _position(shape):
    try:
        return (shape.top or 0, shape.left or 0)
    except Exception:
        return (0, 0)


def _one_line(text) -> str:
    return " ".join(str(text).split())


def _table(rows) -> list[str]:
    lines = ["| " + " | ".join(_one_line(c).replace("|", "\\|") for c in r) + " |" for r in rows]
    if lines:
        lines.insert(1, "|" + " --- |" * len(rows[0]))
    return lines


def _chart(chart) -> list[str]:
    heading = "### Chart"
    try:
        if chart.has_title and chart.chart_title.has_text_frame:
            heading += ": " + _one_line(chart.chart_title.text_frame.text)
        categories = [c.label for c in chart.plots[0].categories]
        series = [(s.name, list(s.values)) for s in chart.series]
    except Exception as e:
        return [
            heading,
            f"[chart data not readable ({type(e).__name__}): render the slide to see it]",
        ]
    rows = [["Category", *(name for name, _ in series)]]
    for i, category in enumerate(categories):
        rows.append([category, *(v[i] if i < len(v) else "" for _, v in series)])
    return [heading, "", *_table(rows)]


def _shape(shape) -> list[str]:
    tag = shape._element.tag
    if tag.endswith("}grpSp"):
        return _shapes(shape.shapes)
    if tag.endswith("}pic"):
        alt = _one_line(shape._element._nvXxPr.cNvPr.get("descr", ""))
        return [f"![{alt}]({shape.name})"]
    if getattr(shape, "has_table", False):
        return ["", *_table([[c.text for c in r.cells] for r in shape.table.rows]), ""]
    if getattr(shape, "has_chart", False):
        return ["", *_chart(shape.chart), ""]
    if shape.has_text_frame and shape.text_frame.text.strip():
        text = shape.text_frame.text.replace("\v", "\n")
        is_title = shape.is_placeholder and shape.placeholder_format.idx == 0
        return ["# " + text.lstrip() if is_title else text]
    return []


def _members(shapes) -> list:
    try:
        return list(shapes)
    except Exception:
        pass
    members = []
    for i in range(len(shapes)):
        try:
            members.append(shapes[i])
        except Exception as e:
            members.append(e)
    return members


def _shapes(shapes) -> list[str]:
    lines = []
    for shape in sorted(_members(shapes), key=_position):
        try:
            if isinstance(shape, Exception):
                raise shape
            lines += _shape(shape)
        except Exception as e:
            lines.append(f"[shape not readable ({type(e).__name__}): render the slide to see it]")
    return lines


def slide_text(slide, number: int) -> str:
    lines = [f"<!-- Slide number: {number} -->", *_shapes(slide.shapes)]
    try:
        notes = slide.notes_slide.notes_text_frame if slide.has_notes_slide else None
        if notes is not None and notes.text.strip():
            lines += ["", "### Notes:", notes.text.replace("\v", "\n")]
    except Exception as e:
        lines += ["", f"[notes not readable ({type(e).__name__})]"]
    return re.sub(r"\n{3,}", "\n\n", "\n".join(lines).strip())


def main() -> None:
    if len(sys.argv) != 2:
        sys.exit("Usage: python slide_text.py deck.pptx")
    path = sys.argv[1]
    if not os.path.isfile(path):
        sys.exit(f"Error: {path} {'is not a file' if os.path.exists(path) else 'does not exist'}")
    if not zipfile.is_zipfile(path):
        sys.exit(
            f"Error: {path} is corrupt, password-protected, or an old .ppt (not a zip archive)"
        )
    try:
        deck = getattr(Package.open(path).main_document_part, "presentation", None)
    except Exception as e:
        sys.exit(f"Error: cannot read {path}: {e}")
    if deck is None:
        sys.exit(f"Error: {path} is not a .pptx, .pptm, .potx or .ppsx file")
    sys.stdout.reconfigure(errors="replace")
    try:
        blocks = [slide_text(s, n) for n, s in enumerate(deck.slides, 1)]
        print("\n\n".join(blocks) or "[no slides; layouts and masters are not printed]", flush=True)
    except BrokenPipeError:
        os.dup2(os.open(os.devnull, os.O_WRONLY), sys.stdout.fileno())


if __name__ == "__main__":
    main()