TsukiNi22/ai-utils / skills/ai/presentation
presentation
VerifiedMake a presentation from a plan or an idea - a time (given, or estimated and confirmed) sets the number of slides; the format (PDF, one-file animated HTML, PPTX, several or all) and the style (several proposals for the subject, "White blueprint" by default, or copied from a PPTX / PDF / HTML the user gives) are asked through interactive-questions. One deck.json gives every format plus speaker notes. Use for "fais une présentation / des slides / un ppt / un pdf de présentation".
| File | Kind | Size |
|---|---|---|
| SKILL.md | skill md | 16.0 KB |
| requires.txt | markdown | 22 B |
| scripts/build_deck.pyexecutable | script | 35.8 KB |
| scripts/check_deck.pyexecutable | script | 13.2 KB |
| scripts/extract_style.pyexecutable | script | 7.6 KB |
| templates/canva.json | config | 527 B |
| templates/custom-example/theme.css | other | 1.8 KB |
| templates/custom-example/theme.json | config | 808 B |
| templates/deck-base.css | other | 2.1 KB |
| templates/deck.js | script | 16.0 KB |
| templates/example.json | config | 3.8 KB |
| templates/slides.css | other | 12.5 KB |
| templates/themes.json | config | 4.7 KB |
scripts/build_deck.py
View on GitHub#!/usr/bin/env python3
"""Build a presentation from one deck.json: an animated web page (one HTML file), a PDF, a PPTX and the oral notes.
python3 build_deck.py deck.json [-o OUTDIR] [--formats html,pdf,pptx,notes] [--theme NAME | --theme-file FILE] [--canva]
deck.json (see ../templates/example.json):
{"title", "subtitle", "lang", "author", "date", "duration": minutes, "theme": "blueprint", "slides": [slide, ...]}
slide = {"kind", "title", "chapter", "notes", "bg", ...}; kinds: title, section, bullets, columns, table, code, image, svg,
steps, quote, end. Text accepts `code` and **bold**. The HTML page shows the points one by one (build steps), the PDF
and the PPTX show every step at once. "bg" (one colour or two for a gradient) gives a slide its own background: the
web page fades the colours from one slide to the next. Themes: ../templates/themes.json, ~/.claude/presentation/themes/*.json, --theme-file.
"""
import argparse
import base64
import html
import json
import mimetypes
import re
import sys
import unicodedata
from pathlib import Path
HERE = Path(__file__).resolve().parent
TEMPLATES = HERE.parent / "templates"
USER_THEMES = Path.home() / ".claude" / "presentation" / "themes"
KINDS = ("title", "section", "bullets", "columns", "table", "code", "image", "svg", "steps", "quote", "end")
NEEDS = {"bullets": "points", "columns": "columns", "table": "rows", "code": "code", "image": "src", "svg": "svg", "steps": "steps",
"quote": "text"} # what a slide of this kind cannot be built without
LISTS = ("points", "columns", "rows", "steps")
THEME_VARS = ("bg", "paper", "ink", "ink2", "tx", "dim", "code", "codetx", "warn", "ok", "font", "head_font", "mono",
"head_transform", "head_spacing", "head_weight", "radius", "bgimg", "frame", "inner_frame") # always written
COLOUR = re.compile(r"^(#[0-9a-fA-F]{3,8}|(rgb|rgba|hsl|hsla)\(.*\))$") # an extra theme key holding a colour
SVG_PAINT = re.compile(r'\s(fill|stroke|stop-color|flood-color|color)="(var\(--[a-z0-9-]+\))"') # var() in an attribute
REVEAL_FIRST, REVEAL_GAP, REVEAL_MAX = 0.25, 0.12, 12 # "reveal": "auto": first delay, gap between steps (s), steps timed
BG_ANGLE, BG_FADE = "115deg", 1.4 # per slide gradients: direction, seconds of the colour fade on the web page
# ======================================================================= model
def load_themes() -> dict:
themes = json.loads((TEMPLATES / "themes.json").read_text(encoding="utf-8"))
if USER_THEMES.is_dir():
for file in sorted(USER_THEMES.glob("*.json")):
themes[file.stem] = {**json.loads(file.read_text(encoding="utf-8")), "_dir": str(file.parent)}
return themes
def pick_theme(deck: dict, args: argparse.Namespace) -> tuple[str, dict]:
if args.theme_file:
file = Path(args.theme_file).expanduser()
if not file.is_file():
sys.exit(f"build_deck: theme file {args.theme_file!r} not found")
try:
custom = json.loads(file.read_text(encoding="utf-8"))
except json.JSONDecodeError as error:
sys.exit(f"build_deck: {file}: not valid JSON ({error})")
name = file.stem if file.stem != "theme" else file.resolve().parent.name # <look>/theme.json: the folder names it
return name, {**load_themes()["blueprint"], **custom, "_dir": str(file.resolve().parent)}
themes = load_themes()
name = args.theme or deck.get("theme") or "blueprint"
if name not in themes:
sys.exit(f"build_deck: unknown theme {name!r} (known: {', '.join(themes)})")
return name, {**themes["blueprint"], **themes[name]}
class MissingTool(RuntimeError):
"""A format that cannot be built here (its library is not installed): the other formats are still built."""
def installed(family: str) -> bool:
import subprocess
out = subprocess.run(["fc-match", "-f", "%{family}", family], capture_output=True, text=True, check=False).stdout
return family.lower() in out.lower().split(",")[0].strip().lower() or out.lower().startswith(family.lower())
def canva_theme(theme: dict) -> dict:
"""Theme made for an import into Canva: ONE family per role taken from Canva's library (and installed here, so it is
embedded in the PDF), no fallback stack (a fallback glyph = another font = a text cut in pieces), no letter spacing,
no background drawn with gradients (hundreds of vector lines on import)."""
canva = json.loads((TEMPLATES / "canva.json").read_text(encoding="utf-8"))
def pick(stack: str, fallback: str) -> str:
for name in (n.strip().strip("'\"") for n in stack.split(",")):
if name in canva["fonts"] and installed(name):
return f"'{name}'"
return f"'{fallback}'"
sans = pick(theme["font"], canva["sans"])
return {**theme, "font": sans, "head_font": pick(theme["head_font"], canva["sans"]) if theme["head_font"] != theme["font"] else sans,
"mono": pick(theme["mono"], canva["mono"]), "head_spacing": "0", "bgimg": "none"}
def check_model(deck: dict) -> None:
slides = deck.get("slides")
if not isinstance(slides, list) or not slides:
sys.exit("build_deck: deck.json needs a non-empty 'slides' list")
for index, slide in enumerate(slides, 1):
if not isinstance(slide, dict):
sys.exit(f"build_deck: slide {index} must be an object with a 'kind'")
kind = slide.get("kind")
if kind not in KINDS:
sys.exit(f"build_deck: slide {index}: unknown kind {kind!r} (known: {', '.join(KINDS)})")
if kind not in ("quote", "svg", "end") and not slide.get("title"):
sys.exit(f"build_deck: slide {index} ({kind}) has no title")
key = NEEDS.get(kind)
if key and not slide.get(key):
sys.exit(f"build_deck: slide {index} ({kind}) has no '{key}'")
if key in LISTS and not isinstance(slide[key], list):
sys.exit(f"build_deck: slide {index} ({kind}): '{key}' must be a list")
if key in ("columns", "steps") and not all(isinstance(item, dict) for item in slide[key]):
sys.exit(f"build_deck: slide {index} ({kind}): each of '{key}' must be an object (head, {'points' if key == 'columns' else 'text'})")
if key == "points" and not all(isinstance(item, str) or (isinstance(item, dict) and item.get("text")) for item in slide[key]):
sys.exit(f"build_deck: slide {index} ({kind}): a point is a text or an object with a 'text' (and 'sub')")
def inline(text: str) -> str:
"""Escape, then `code` and **bold**."""
out = html.escape(str(text), quote=False)
out = re.sub(r"`([^`]+)`", r"<code>\1</code>", out)
return re.sub(r"\*\*([^*]+)\*\*", r"<strong>\1</strong>", out)
def slug(text: str) -> str:
"""File name of the outputs: accents dropped before the non letters, never an empty name."""
ascii_text = unicodedata.normalize("NFKD", text).encode("ascii", "ignore").decode("ascii")
return re.sub(r"[^a-z0-9]+", "-", ascii_text.lower()).strip("-") or "deck"
def svg_paint(markup: str) -> str:
"""A browser ignores var() in an SVG presentation attribute (fill="var(--ink)" is painted black): move it into the
style attribute of the same tag, merged with a style already there (two style attributes: the second is dropped)."""
def fix(match: re.Match) -> str:
tag = match.group(0)
styles = [s.strip().rstrip(";") for s in re.findall(r'\sstyle="([^"]*)"', tag)]
paints = [f"{name}:{value}" for name, value in SVG_PAINT.findall(tag)]
if not paints and len(styles) < 2:
return tag
tag = SVG_PAINT.sub("", re.sub(r'\sstyle="[^"]*"', "", tag))
merged = "; ".join(s for s in styles + paints if s)
end = "/>" if tag.endswith("/>") else ">"
return tag[:-len(end)].rstrip() + f' style="{merged}"' + end
return re.sub(r"<svg\b.*?</svg>", lambda block: re.sub(r"<[a-zA-Z][^<>]*>", fix, block.group(0)), markup, flags=re.S)
def plain(text: str) -> str:
return re.sub(r"\*\*([^*]+)\*\*|`([^`]+)`", lambda m: m.group(1) or m.group(2), str(text))
def points(slide: dict, key: str = "points") -> list[tuple[str, list[str]]]:
"""A list of "text" or {"text", "sub": [...]} as (text, subs)."""
out = []
for item in slide.get(key, []):
out.append((item, []) if isinstance(item, str) else (item["text"], list(item.get("sub", []))))
return out
def data_uri(path: str, base: Path) -> str:
file = (base / path).resolve()
if not file.is_file():
sys.exit(f"build_deck: image not found: {file}")
kind = mimetypes.guess_type(file.name)[0] or "application/octet-stream"
return f"data:{kind};base64," + base64.b64encode(file.read_bytes()).decode("ascii")
# ======================================================================= html (web page and PDF share it)
def slide_inner(slide: dict, number: int, base: Path) -> str:
kind, title = slide["kind"], slide.get("title", "")
head = f"<h2>{inline(title)}</h2>" if title and kind not in ("title", "section", "end") else ""
lead = f'<p class="lead">{inline(slide["lead"])}</p>' if slide.get("lead") else ""
if kind == "title":
meta = "".join(f"<li>{inline(m)}</li>" for m in slide.get("meta", []))
return (f"<h1>{inline(title)}</h1><p class=\"sub\">{inline(slide.get('subtitle', ''))}</p>"
+ (f'<ul class="meta">{meta}</ul>' if meta else ""))
if kind == "section":
return (f'<div class="num">{slide.get("number", "")}</div><h2>{inline(title)}</h2>'
+ (f'<p class="lead">{inline(slide["lead"])}</p>' if slide.get("lead") else ""))
if kind == "bullets":
items = "".join(
f'<li data-s="{i}">{inline(text)}' + (f"<ul>{''.join(f'<li>{inline(s)}</li>' for s in subs)}</ul>" if subs else "") + "</li>"
for i, (text, subs) in enumerate(points(slide), 1))
return f"{head}{lead}<ul>{items}</ul>"
if kind == "columns":
cols = slide.get("columns", [])
body = "".join(
f'<div class="col" data-s="{i}"><h3>{inline(c.get("head", ""))}</h3><ul>'
+ "".join(f"<li>{inline(p)}</li>" for p in c.get("points", [])) + "</ul></div>" for i, c in enumerate(cols, 1))
return f'{head}{lead}<div class="cols" style="--n:{len(cols)}">{body}</div>'
if kind == "table":
thead = "".join(f"<th scope=\"col\">{inline(h)}</th>" for h in slide.get("head", []))
rows = "".join("<tr>" + "".join(f"<td>{inline(c)}</td>" for c in row) + "</tr>" for row in slide.get("rows", []))
return f"{head}{lead}<table><thead><tr>{thead}</tr></thead><tbody>{rows}</tbody></table>"
if kind == "code":
caption = f'<p class="caption">{inline(slide["caption"])}</p>' if slide.get("caption") else ""
return f'{head}{lead}<pre><code>{html.escape(slide.get("code", ""))}</code></pre>{caption}'
if kind == "image":
alt = html.escape(slide.get("alt") or plain(title))
caption = f"<figcaption>{inline(slide['caption'])}</figcaption>" if slide.get("caption") else ""
return f'{head}{lead}<figure><img src="{data_uri(slide["src"], base)}" alt="{alt}">{caption}</figure>'
if kind == "svg":
caption = f"<figcaption>{inline(slide['caption'])}</figcaption>" if slide.get("caption") else ""
return f'{head}{lead}<figure>{svg_paint(slide.get("svg", ""))}{caption}</figure>'
if kind == "steps":
steps = slide.get("steps", [])
items = "".join(f'<li data-s="{i}"><h3>{inline(s.get("head", ""))}</h3><p>{inline(s.get("text", ""))}</p></li>' for i, s in enumerate(steps, 1))
return f'{head}{lead}<ol class="flow" style="--n:{min(len(steps), 4)}">{items}</ol>'
if kind == "quote":
who = f"<cite>{inline(slide['by'])}</cite>" if slide.get("by") else ""
return f"<blockquote>{inline(slide.get('text', ''))}</blockquote>{who}"
links = "".join(f"<li>{inline(link)}</li>" for link in slide.get("links", [])) # end
image = f'<figure><img src="{data_uri(slide["src"], base)}" alt="{html.escape(slide.get("alt", ""))}" style="max-height:260px"></figure>' if slide.get("src") else ""
return f'<h1>{inline(title or "Thank you")}</h1><p class="sub">{inline(slide.get("subtitle", ""))}</p><ul>{links}</ul>{image}'
def slides_html(deck: dict, base: Path, with_notes: bool) -> str:
out = []
chapter = ""
for number, slide in enumerate(deck["slides"], 1):
chapter = slide.get("chapter", chapter) or deck.get("title", "")
title = plain(slide.get("title") or slide.get("text") or deck.get("title", ""))
notes = f'<div class="notes">{inline(slide["notes"])}</div>' if with_notes and slide.get("notes") else ""
foot = f'<div class="foot"><span>{html.escape(plain(deck.get("title", "")))}</span><span>{html.escape(chapter)} · {number}</span></div>'
out.append(f'<section class="slide k-{slide["kind"]}" id="s{number}" data-ch="{html.escape(chapter)}" '
f'data-title="{html.escape(title)}">{slide_inner(slide, number, base)}{foot}{notes}</section>')
return "\n".join(out)
def custom_css(theme: dict, base: Path) -> str:
"""Free CSS of a 'Sur mesure' theme (`css` text and / or `css_file`): decor, shapes, gradients, @font-face, a layout
per kind, even its own transitions - the look is not limited to the variables. Not used for a Canva import."""
if theme.get("canva"):
return ""
text = theme.get("css", "")
if theme.get("css_file"):
for folder in (Path(theme.get("_dir", ".")), base):
if (folder / theme["css_file"]).is_file():
text += "\n" + (folder / theme["css_file"]).read_text(encoding="utf-8")
break
else:
sys.exit(f"build_deck: css_file {theme['css_file']!r} not found")
return "\n/* theme css */\n" + text if text else ""
def theme_css(theme: dict) -> str:
"""The theme as CSS variables: the fixed keys, then every extra key holding a colour (a custom theme.json may add
"acc1", "acc2_line"...: --acc1 and --acc2-line exist in the CSS and in the inline SVG of the PDF)."""
extra = [k for k, v in theme.items() if k not in THEME_VARS and not k.startswith("_") and isinstance(v, str) and COLOUR.match(v.strip())]
rows = "; ".join(f"--{k.replace('_', '-')}: {theme[k]}" for k in (*THEME_VARS, *extra))
return f":root {{ {rows}; }}"
def slide_bg(slide: dict, theme: dict) -> tuple[str, str] | None:
"""The two colours of a slide's own background ("bg": "#hex" or ["#hex", "#hex"]), None without one."""
value = slide.get("bg")
if not value:
return None
colours = [value] if isinstance(value, str) else list(value)
if not 1 <= len(colours) <= 2 or not all(isinstance(c, str) and COLOUR.match(c.strip()) for c in colours):
sys.exit(f"build_deck: slide {slide.get('title', '')!r}: bg must be a colour or two colours, not {value!r}")
return colours[0], colours[-1]
def bg_css(deck: dict, theme: dict, web: bool) -> str:
"""Per slide backgrounds. PDF / no script: each slide paints its gradient under the theme's bgimg. Web page: one layer
behind the slides whose two colours are registered (@property) so they fade from one slide to the next instead of
cutting; a slide without "bg" takes the theme background."""
pairs = [slide_bg(s, theme) for s in deck["slides"]]
if not any(pairs):
return ""
angle = deck.get("bg_angle", BG_ANGLE)
rows = ["/* per slide backgrounds */"]
scope = "html:not(.js) " if web else "" # with the script, the fading layer behind the slides paints them
for number, pair in enumerate(pairs, 1):
if pair and theme.get("canva"): # a Canva import turns a gradient into a picture: the first colour only
rows.append(f"{scope}#s{number} {{ background-color: {pair[0]}; }}")
elif pair:
rows.append(f"{scope}#s{number} {{ background-color: {pair[0]}; background-image: var(--bgimg), linear-gradient({angle}, {pair[0]}, {pair[1]}); }}")
if not web:
return "\n".join(rows) + "\n"
fade = deck.get("bg_fade", BG_FADE)
rows += ['@property --c1 { syntax: "<color>"; inherits: true; initial-value: transparent; }',
'@property --c2 { syntax: "<color>"; inherits: true; initial-value: transparent; }',
".js .slide { background-color: transparent; background-image: var(--bgimg); }",
"#deck { position: absolute; inset: 0; z-index: 1; }",
f'#stage::before {{ content: ""; position: absolute; inset: 0; z-index: 0; background-color: var(--c1); '
f"background-image: linear-gradient({angle}, var(--c1), var(--c2)); transition: --c1 {fade}s ease, --c2 {fade * 1.15:.2f}s ease; }}"]
for number, pair in enumerate(pairs, 1):
first, last = pair or (theme["bg"], theme["bg"])
rows.append(f'html[data-slide="{number}"] #stage::before {{ --c1: {first}; --c2: {last}; }}')
return "\n".join(rows) + "\n"
SKELETON = """<div id="stage"><main id="deck" class="{cls}">
{slides}
</main>
<div class="zone" id="zl" aria-hidden="true"></div><div class="zone" id="zr" aria-hidden="true"></div>
<div id="hud"><nav id="chapters" aria-label="Chapters"></nav><div id="bar"><i></i></div><div id="steps" aria-hidden="true"></div><span id="count"></span><div id="btns"><button type="button" id="prev" aria-label="Previous (left arrow)">‹</button><button type="button" id="ovbtn" aria-label="Chapter map (O)">▦</button><button type="button" id="fsbtn" aria-label="Full screen (F)">⛶</button><button type="button" id="next" aria-label="Next (right arrow)">›</button></div></div>
</div>
<div id="map" hidden></div>"""
def reveal_auto(slides: str) -> tuple[str, str]:
""""reveal": "auto": no click per point. The build steps (data-s) become an entrance played when the slide comes in,
one after the other; one click (or arrow) per slide. The PDF is not concerned (it shows every step anyway)."""
rows = ['@keyframes reveal-in { from { opacity: 0; transform: translateY(12px); } to { opacity: 1; transform: none; } }',
'.js .slide[data-state="active"] [data-in] { animation: reveal-in .5s cubic-bezier(.16,.84,.3,1) both; }']
rows += [f'.js .slide[data-state="active"] [data-in="{n}"] {{ animation-delay: {REVEAL_FIRST + (n - 1) * REVEAL_GAP:.2f}s; }}' for n in range(1, REVEAL_MAX + 1)]
return re.sub(r'\sdata-s="(\d+)"', r' data-in="\1"', slides), "\n".join(rows) + "\n"
def build_web(deck: dict, theme: dict, base: Path, out: Path) -> None:
css = (TEMPLATES / "deck-base.css").read_text(encoding="utf-8") + "\n" + theme_css(theme) + "\n" + (TEMPLATES / "slides.css").read_text(encoding="utf-8") \
+ "\n" + bg_css(deck, theme, True) + custom_css(theme, base)
js = (TEMPLATES / "deck.js").read_text(encoding="utf-8")
slides = slides_html(deck, base, True)
if deck.get("reveal", "click") == "auto":
slides, motion = reveal_auto(slides)
css += "\n" + motion
elif deck.get("reveal", "click") != "click":
sys.exit(f"build_deck: reveal must be \"click\" (a click per point) or \"auto\" (everything on arrival), not {deck['reveal']!r}")
slides = re.sub(r'\sdata-click="(\d+)"', r' data-s="\1"', slides) # always one click each, even with reveal auto
body = SKELETON.format(slides=slides, cls="cartouche" if theme.get("cartouche") else "")
page = (f'<!DOCTYPE html>\n<html lang="{deck.get("lang", "en")}">\n<head>\n<meta charset="utf-8">\n'
f'<meta name="viewport" content="width=device-width, initial-scale=1">\n<title>{html.escape(plain(deck.get("title", "")))}</title>\n'
f'<style>\n{css}\n</style>\n</head>\n<body>\n{body}\n<script>\n{js}\n</script>\n</body>\n</html>\n')
out.write_text(page, encoding="utf-8")
# what Canva cuts, merges or flattens when it reads a PDF: letter spacing, justified text, transforms, opacity, shadows
CANVA_CSS = """
.canva, .canva * { letter-spacing: 0 !important; text-shadow: none !important; box-shadow: none !important; text-align-last: auto; }
.canva .slide { text-rendering: auto; font-kerning: none; }
.canva .slide p, .canva .slide li, .canva .slide h1, .canva .slide h2, .canva .slide h3 { text-align: left; hyphens: none; }
.canva .slide code { background: none; padding: 0; }
.canva .slide pre { background: var(--code); }
"""
def build_pdf(deck: dict, theme: dict, base: Path, out: Path) -> None:
try:
from weasyprint import HTML
except ImportError:
raise MissingTool("the PDF needs weasyprint (sudo dnf install python3-weasyprint, or pip install --user weasyprint)") from None
css = theme_css(theme) + "\n" + (TEMPLATES / "slides.css").read_text(encoding="utf-8") + "\n" + bg_css(deck, theme, False) + custom_css(theme, base)
cls = "print cartouche" if theme.get("cartouche") else "print"
if theme.get("canva"):
css += CANVA_CSS
cls += " canva"
page = (f'<!DOCTYPE html><html lang="{deck.get("lang", "en")}"><head><meta charset="utf-8"><title>{html.escape(plain(deck.get("title", "")))}</title>'
f'<style>{css}</style></head><body class="{cls}"><div class="{cls}">{pdf_svg(slides_html(deck, base, False), theme)}</div></body></html>')
HTML(string=page, base_url=str(base)).write_pdf(str(out))
def pdf_svg(page: str, theme: dict) -> str:
"""WeasyPrint does not pass the page colour nor the CSS variables into an inline SVG: currentColor would be black.
Give each <svg> the text colour of the theme and turn var(--key) into its value (the web page keeps the variables)."""
def resolve(match: re.Match) -> str:
block = re.sub(r"var\(--([a-z0-9-]+)\)", lambda v: str(theme.get(v.group(1).replace("-", "_"), v.group(0))), match.group(0))
return block if re.match(r"<svg[^>]*\scolor=", block) else block.replace("<svg", f'<svg color="{theme["tx"]}"', 1)
return re.sub(r"<svg\b.*?</svg>", resolve, page, flags=re.S)
# ======================================================================= pptx
def rgb(value: str) -> object:
from pptx.dml.color import RGBColor
return RGBColor.from_string(value.lstrip("#").upper())
def first_font(stack: str) -> str:
return stack.split(",")[0].strip().strip("'\"")
def build_pptx(deck: dict, theme: dict, base: Path, out: Path) -> None:
try:
from pptx import Presentation
from pptx.enum.shapes import MSO_SHAPE
from pptx.enum.text import MSO_ANCHOR, MSO_AUTO_SIZE
from pptx.util import Emu, Inches, Pt
except ImportError:
raise MissingTool("the PPTX needs python-pptx (pip install --user python-pptx, or sudo dnf install python3-python-pptx)") from None
prs = Presentation()
prs.slide_width, prs.slide_height = Inches(13.333), Inches(7.5)
blank = prs.slide_layouts[6]
sans, mono, head = first_font(theme["font"]), first_font(theme["mono"]), first_font(theme["head_font"])
W, H, M = 13.333, 7.5, 0.7
def box(slide: object, x: float, y: float, w: float, h: float, fill: str | None = None, line: str | None = None, shape: object = MSO_SHAPE.RECTANGLE) -> object:
shp = slide.shapes.add_shape(shape, Inches(x), Inches(y), Inches(w), Inches(h))
if fill:
shp.fill.solid()
shp.fill.fore_color.rgb = rgb(fill)
else:
shp.fill.background()
if line:
shp.line.color.rgb = rgb(line)
shp.line.width = Pt(1.5)
else:
shp.line.fill.background()
shp.shadow.inherit = False
return shp
def borders(cell: object, color: str, width: int = 12700) -> None:
"""The four borders of a table cell (python-pptx has no API for it): drawn lines, as in the web page and the PDF."""
from lxml import etree
props = cell._tc.get_or_add_tcPr()
for tag in ("a:lnL", "a:lnR", "a:lnT", "a:lnB"):
for old in props.findall("{http://schemas.openxmlformats.org/drawingml/2006/main}" + tag[2:]):
props.remove(old)
ns = "http://schemas.openxmlformats.org/drawingml/2006/main"
for i, tag in enumerate(("lnL", "lnR", "lnT", "lnB")):
line = etree.Element(f"{{{ns}}}{tag}", w=str(width), cap="flat", cmpd="sng", algn="ctr")
fill = etree.SubElement(line, f"{{{ns}}}solidFill")
etree.SubElement(fill, f"{{{ns}}}srgbClr", val=color.lstrip("#").upper())
etree.SubElement(line, f"{{{ns}}}prstDash", val="solid")
props.insert(i, line) # the lines come before the fill in the schema
def text(slide: object, x: float, y: float, w: float, h: float, runs: list, size: int, color: str, font: str | None = None, bold: bool = False,
anchor: object = MSO_ANCHOR.TOP, upper: bool = False) -> object:
"""runs = list of paragraphs; a paragraph is a string (with **bold** / `code`) or (string, level)."""
frame = slide.shapes.add_textbox(Inches(x), Inches(y), Inches(w), Inches(h)).text_frame
frame.word_wrap = True
frame.auto_size = MSO_AUTO_SIZE.NONE # no "shrink on overflow": Canva would import the unscaled size
frame.vertical_anchor = anchor
for i, para in enumerate(runs):
string, level = (para, 0) if isinstance(para, str) else para
paragraph = frame.paragraphs[0] if i == 0 else frame.add_paragraph()
paragraph.space_after = Pt(8)
for part in re.split(r"(\*\*[^*]+\*\*|`[^`]+`)", string):
if not part:
continue
run = paragraph.add_run()
code, strong = part.startswith("`"), part.startswith("**")
run.text = part.strip("`") if code else part.strip("*") if strong else part
if upper:
run.text = run.text.upper()
run.font.size = Pt(size - (3 if level else 0))
run.font.name = mono if code else (font or sans)
run.font.bold = bold or strong
run.font.color.rgb = rgb(theme["ink"] if strong else (theme["dim"] if level else color))
return frame
chapter = ""
for number, spec in enumerate(deck["slides"], 1):
kind = spec["kind"]
chapter = spec.get("chapter", chapter) or deck.get("title", "")
slide = prs.slides.add_slide(blank)
slide.background.fill.solid()
own = slide_bg(spec, theme)
slide.background.fill.fore_color.rgb = rgb(own[0] if own and re.match(r"^#[0-9a-fA-F]{6}$", own[0]) else theme["bg"])
upper = theme["head_transform"] == "uppercase"
title = spec.get("title", "")
if theme.get("frame", "0") != "0":
box(slide, 0.12, 0.12, W - 0.24, H - 0.24, None, theme["ink"])
if kind in ("title", "end"):
text(slide, M, 2.0, W - 2 * M, 1.6, [title or "Thank you"], 48, theme["ink"], head, True, MSO_ANCHOR.BOTTOM, upper)
text(slide, M, 3.8, W - 2 * M, 1.2, [spec.get("subtitle", "")], 24, theme["dim"])
extra = spec.get("meta", []) if kind == "title" else spec.get("links", [])
if extra:
text(slide, M, 5.2, W - 2 * M, 1.6, [plain(e) for e in extra], 16, theme["ink"] if kind == "title" else theme["tx"])
if kind == "end" and spec.get("src"):
slide.shapes.add_picture(str((base / spec["src"]).resolve()), Inches(W - 3.2), Inches(4.4), height=Inches(2.4))
elif kind == "section":
text(slide, M, 2.4, 3, 0.6, [str(spec.get("number", ""))], 22, theme["ink2"], mono)
text(slide, M, 3.0, W - 2 * M, 1.4, [title], 44, theme["ink"], head, True, MSO_ANCHOR.TOP, upper)
if spec.get("lead"):
text(slide, M, 4.5, W - 2 * M, 1.0, [spec["lead"]], 22, theme["dim"])
elif kind == "quote":
box(slide, M, 2.0, 0.08, 2.6, theme["ink2"])
text(slide, M + 0.4, 2.0, W - 2 * M - 0.4, 2.6, [spec.get("text", "")], 34, theme["ink"], None, False, MSO_ANCHOR.MIDDLE)
if spec.get("by"):
text(slide, M + 0.4, 4.8, W - 2 * M, 0.6, [spec["by"]], 18, theme["dim"])
else:
text(slide, M, 0.45, W - 2 * M, 1.0, [title], 32, theme["ink"], head, True, MSO_ANCHOR.TOP, upper)
top = 1.55
if spec.get("lead"):
text(slide, M, 1.4, W - 2 * M, 0.7, [spec["lead"]], 18, theme["dim"])
top = 2.15
avail = H - top - 0.9
if kind == "bullets":
paras: list = []
for item, subs in points(spec):
paras.append("• " + item)
paras.extend(("– " + s, 1) for s in subs)
text(slide, M, top, W - 2 * M, avail, paras, 22, theme["tx"])
elif kind == "columns":
cols = spec.get("columns", [])
gap = 0.3
cw = (W - 2 * M - gap * (len(cols) - 1)) / max(1, len(cols))
for i, col in enumerate(cols):
x = M + i * (cw + gap)
height = min(avail, 0.95 + 0.42 * sum(1 + len(p) // max(1, int(cw * 7.5)) for p in col.get("points", [])))
box(slide, x, top, cw, height, theme["paper"], theme["ink"])
text(slide, x + 0.15, top + 0.1, cw - 0.3, 0.6, [col.get("head", "")], 20, theme["ink"], head, True)
text(slide, x + 0.15, top + 0.75, cw - 0.3, height - 0.85, ["• " + p for p in col.get("points", [])], 17, theme["tx"])
elif kind == "table":
headers, rows = spec.get("head", []), spec.get("rows", [])
shape = slide.shapes.add_table(len(rows) + 1, max(1, len(headers)), Inches(M), Inches(top), Inches(W - 2 * M), Inches(min(avail, 0.5 * (len(rows) + 1))))
for c, name in enumerate(headers):
cell = shape.table.cell(0, c)
cell.text = plain(name)
cell.fill.solid()
cell.fill.fore_color.rgb = rgb(theme["ink"])
borders(cell, theme["ink"])
for run in cell.text_frame.paragraphs[0].runs:
run.font.size, run.font.bold, run.font.name = Pt(16), True, sans
run.font.color.rgb = rgb(theme["paper"])
for r, row in enumerate(rows, 1):
for c, value in enumerate(row):
cell = shape.table.cell(r, c)
cell.text = plain(value)
cell.fill.solid()
cell.fill.fore_color.rgb = rgb(theme["paper"] if r % 2 == 0 else theme["bg"])
borders(cell, theme["dim"], 9525)
for run in cell.text_frame.paragraphs[0].runs:
run.font.size, run.font.name = Pt(15), sans
run.font.color.rgb = rgb(theme["tx"])
elif kind == "code":
lines = spec.get("code", "").split("\n")
height = min(avail - 0.5, 0.4 + 0.33 * len(lines))
box(slide, M, top, W - 2 * M, height, theme["code"], None)
box(slide, M, top, 0.07, height, theme["ink"])
text(slide, M + 0.3, top + 0.12, W - 2 * M - 0.5, height - 0.2, lines, 15, theme["codetx"], mono)
if spec.get("caption"):
text(slide, M, top + height + 0.1, W - 2 * M, 0.5, [spec["caption"]], 14, theme["dim"])
elif kind == "image":
if spec.get("src"):
pic = slide.shapes.add_picture(str((base / spec["src"]).resolve()), Inches(M), Inches(top))
scale = min((W - 2 * M) / (pic.width / 914400), (avail - 0.4) / (pic.height / 914400))
pic.width, pic.height = Emu(int(pic.width * scale)), Emu(int(pic.height * scale))
pic.left = Emu(int((prs.slide_width - pic.width) / 2))
if spec.get("caption"):
text(slide, M, H - 1.3, W - 2 * M, 0.5, [spec["caption"]], 14, theme["dim"])
elif kind == "svg":
text(slide, M, top, W - 2 * M, 1.0, ["(drawing: see the web page or the PDF)"], 16, theme["dim"])
elif kind == "steps":
steps = spec.get("steps", [])
n = max(1, min(len(steps), 4))
gap = 0.25
cw = (W - 2 * M - gap * (n - 1)) / n
for i, step in enumerate(steps):
x, y = M + (i % n) * (cw + gap), top + (i // n) * 2.4
box(slide, x, y, cw, 2.2, theme["paper"], theme["ink"])
text(slide, x + 0.12, y + 0.08, cw - 0.24, 0.5, [f"{i + 1}. " + plain(step.get("head", ""))], 18, theme["ink"], head, True)
text(slide, x + 0.12, y + 0.7, cw - 0.24, 1.4, [step.get("text", "")], 15, theme["tx"])
if kind not in ("title", "section", "end", "quote"):
text(slide, M, H - 0.55, W - 2 * M, 0.35, [f"{plain(deck.get('title', ''))} · {chapter} · {number}"], 10, theme["dim"])
if spec.get("notes"):
slide.notes_slide.notes_text_frame.text = plain(spec["notes"])
prs.core_properties.title = plain(deck.get("title", ""))
prs.core_properties.author = deck.get("author", "")
prs.save(str(out))
# ======================================================================= notes
def build_notes(deck: dict, out: Path) -> None:
rows = [f"# {plain(deck.get('title', 'Presentation'))}: speaker notes", "",
"What is said aloud and is not written on the slides. The numbers are the pages of the PDF and of the web page.", ""]
for number, slide in enumerate(deck["slides"], 1):
label = plain(slide.get("title") or slide.get("text") or slide["kind"])
rows += [f"## {number}. {label}", "", plain(slide.get("notes", "")) or "—", ""]
out.write_text("\n".join(rows), encoding="utf-8")
# ======================================================================= main
def main() -> None:
parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument("deck")
parser.add_argument("-o", "--out", default=None, help="output folder (default: next to deck.json)")
parser.add_argument("--formats", default="html,pdf,notes", help="html, pdf, pptx, notes (comma separated)")
parser.add_argument("--theme", default=None)
parser.add_argument("--theme-file", default=None)
parser.add_argument("--canva", action="store_true", help="made for an import into Canva: one Canva font per role, no gradient background, nothing a PDF import cuts or merges")
args = parser.parse_args()
source = Path(args.deck).expanduser().resolve()
if not source.is_file():
sys.exit(f"build_deck: {args.deck!r} not found")
try:
deck = json.loads(source.read_text(encoding="utf-8"))
except json.JSONDecodeError as error:
sys.exit(f"build_deck: {source.name}: not valid JSON ({error})")
if not isinstance(deck, dict):
sys.exit(f"build_deck: {source.name} must be an object with a 'slides' list")
formats = [f.strip() for f in args.formats.split(",") if f.strip()]
unknown = [f for f in formats if f not in ("html", "pdf", "pptx", "notes")]
if unknown or not formats:
sys.exit(f"build_deck: unknown format {', '.join(unknown) or '(none)'} (known: html, pdf, pptx, notes)")
check_model(deck)
name, theme = pick_theme(deck, args)
if args.canva:
theme = {**canva_theme(theme), "canva": True}
out = Path(args.out).resolve() if args.out else source.parent
out.mkdir(parents=True, exist_ok=True)
stem = slug(plain(deck.get("title", "deck")))
failed = []
for fmt in formats:
try:
if fmt == "html":
build_web(deck, theme, source.parent, out / f"{stem}.html")
elif fmt == "pdf":
build_pdf(deck, theme, source.parent, out / f"{stem}.pdf")
elif fmt == "pptx":
build_pptx(deck, theme, source.parent, out / f"{stem}.pptx")
else:
build_notes(deck, out / f"{stem}-notes.md")
except MissingTool as error: # the other formats are still built
failed.append(fmt)
sys.stderr.write(f"build_deck: {fmt} not built: {error}\n")
continue
print(f"{out / (stem + {'notes': '-notes.md'}.get(fmt, '.' + fmt))} ({len(deck['slides'])} slides, theme {name})")
if failed:
sys.exit(1)
if __name__ == "__main__":
main()
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| canvas-designanthropics/skillsCreate beautiful visual art in .png and .pdf documents using design philosophy. You should use this skill when the user asks to create a poster, piece of art, design, or other static piece. Create original visual designs, never copying existing artists' work to avoid copyright violations. | Stars: 197k | Updated | Claude CodeCodex | Verified | Install |
| docxanthropics/skillsUse this skill whenever the user wants to create, read, edit, or manipulate Word documents (.docx files) or Word templates (.dotx files). Triggers include: any mention of 'Word doc', 'word document', '.docx', '.dotx', or requests to produce professional documents with formatting like tables of contents, headings, page numbers, or letterheads. Also use when extracting or reorganizing content from .docx or .dotx files, inserting or replacing images in documents, performing find-and-replace in Word files, working with tracked changes or comments, or converting content into a polished Word document. If the user asks for a 'report', 'memo', 'letter', 'template', or similar deliverable as a Word or .docx file, use this skill. Do NOT use for PDFs, spreadsheets, Google Docs, or general coding tasks unrelated to document generation. | Stars: 197k | Updated | Claude CodeCodex | Verified | Install |
| pdfanthropics/skillsUse this skill whenever the user wants to do anything with PDF files. This includes reading or extracting text/tables from PDFs, combining or merging multiple PDFs into one, splitting PDFs apart, rotating pages, adding watermarks, creating new PDFs, filling PDF forms, encrypting/decrypting PDFs, extracting images, and OCR on scanned PDFs to make them searchable. If the user mentions a .pdf file or asks to produce one, use this skill. | Stars: 197k | Updated | Claude CodeCodex | Verified | Install |
| pptxanthropics/skillsUse this skill any time a .pptx or .potx file is involved in any way — as input, output, or both. This includes: creating slide decks, pitch decks, or presentations; reading, parsing, or extracting text from any .pptx or .potx file (even if the extracted content will be used elsewhere, like in an email or summary); editing, modifying, or updating existing presentations; combining or splitting slide files; working with templates (.potx), layouts, speaker notes, or comments. Trigger whenever the user mentions "deck," "slides," "presentation," or references a .pptx or .potx filename, regardless of what they plan to do with the content afterward. If a .pptx or .potx file needs to be opened, created, or touched, use this skill. | Stars: 197k | Updated | Claude CodeCodex | Verified | Install |