Skip to content

AI turns documents or topics into real, native PowerPoint decks—with native shapes, transitions and animations, data-backed charts and tables on demand, audio narration from speaker notes, and support for your own .pptx templates. · by Hugo He

fix(pptx_to_svg): clip image-filled shapes

hugohe3caf7b161c6fe
diff --git a/skills/ppt-master/scripts/pptx_to_svg/pic_to_svg.py b/skills/ppt-master/scripts/pptx_to_svg/pic_to_svg.py
index ef3606ca..193b0ecd 100644
--- a/skills/ppt-master/scripts/pptx_to_svg/pic_to_svg.py
+++ b/skills/ppt-master/scripts/pptx_to_svg/pic_to_svg.py
@@ -46,8 +46,8 @@ class PictureResult:
media: dict[str, bytes] = field(default_factory=dict)
-def convert_picture(
- pic_elem: ET.Element,
+def convert_blip_fill(
+ blip_fill_elem: ET.Element,
xfrm: Xfrm,
slide_part: PartRef,
pkg: OoxmlPackage,
@@ -55,12 +55,13 @@ def convert_picture(
media_subdir: str = "assets",
embed_inline: bool = False,
) -> PictureResult:
- """Translate <p:pic> to SVG <image> (or nested <svg>+<image> for cropping)."""
- blip_fill = pic_elem.find("p:blipFill", NS)
- if blip_fill is None:
- return PictureResult()
+ """Convert an <a:blipFill> element to SVG <image>.
- blip = blip_fill.find("a:blip", NS)
+ Handles image fill for both:
+ - <p:pic><p:blipFill> (standard picture elements)
+ - <p:sp><p:spPr><a:blipFill> (shape with image fill, e.g. Canva exports)
+ """
+ blip = blip_fill_elem.find("a:blip", NS)
if blip is None:
return PictureResult()
@@ -81,11 +82,11 @@ def convert_picture(
href = _build_href(filename, img_bytes, media_subdir, embed_inline)
# srcRect: l/t/r/b in 1/100000ths (so 50000 = 50%).
- src_rect = blip_fill.find("a:srcRect", NS)
+ src_rect = blip_fill_elem.find("a:srcRect", NS)
crop = _parse_src_rect(src_rect)
# stretch / tile: default stretch+fillRect means "fill, ignore aspect ratio".
- has_stretch = blip_fill.find("a:stretch") is not None
+ has_stretch = blip_fill_elem.find("a:stretch") is not None
if not has_stretch:
# tile mode is rare; for v1 fall back to plain image.
pass
@@ -117,6 +118,27 @@ def convert_picture(
return PictureResult(svg=svg, media=media)
+def convert_picture(
+ pic_elem: ET.Element,
+ xfrm: Xfrm,
+ slide_part: PartRef,
+ pkg: OoxmlPackage,
+ *,
+ media_subdir: str = "assets",
+ embed_inline: bool = False,
+) -> PictureResult:
+ """Translate <p:pic> to SVG <image> (or nested <svg>+<image> for cropping)."""
+ blip_fill = pic_elem.find("p:blipFill", NS)
+ if blip_fill is None:
+ return PictureResult()
+
+ return convert_blip_fill(
+ blip_fill, xfrm, slide_part, pkg,
+ media_subdir=media_subdir,
+ embed_inline=embed_inline,
+ )
+
+
# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
diff --git a/skills/ppt-master/scripts/pptx_to_svg/slide_to_svg.py b/skills/ppt-master/scripts/pptx_to_svg/slide_to_svg.py
index 66cc7442..b42a8425 100644
--- a/skills/ppt-master/scripts/pptx_to_svg/slide_to_svg.py
+++ b/skills/ppt-master/scripts/pptx_to_svg/slide_to_svg.py
@@ -32,7 +32,7 @@ from .emu_units import NS, fmt_num
from .fill_to_svg import resolve_fill
from .ln_to_svg import resolve_stroke
from .ooxml_loader import OoxmlPackage, PartRef, SlideRef
-from .pic_to_svg import convert_picture
+from .pic_to_svg import convert_blip_fill, convert_picture
from .prstgeom_to_svg import GeomResult, convert_prst_geom
from .shape_walker import (
CONNECTOR, GRAPHIC, GROUP, PICTURE, SHAPE,
@@ -68,6 +68,7 @@ class AssemblyContext:
marker_seq: list[int] = field(default_factory=lambda: [0])
filter_seq: list[int] = field(default_factory=lambda: [0])
shape_seq: list[int] = field(default_factory=lambda: [0])
+ clip_seq: list[int] = field(default_factory=lambda: [0])
# Accumulated outputs
defs: list[str] = field(default_factory=list)
@@ -161,8 +162,24 @@ def _convert_node(node: ShapeNode, ctx: AssemblyContext, *, top_level: bool) ->
def _convert_shape(node: ShapeNode, ctx: AssemblyContext, *, top_level: bool) -> str:
sp_pr = node.xml.find("p:spPr", NS)
- # Geometry
- geom_xml = _build_geometry_xml(node, sp_pr, ctx)
+ # Check for blipFill (image-filled shape, e.g. Canva exports where images
+ # are expressed as <p:sp> + <a:blipFill> rather than <p:pic>).
+ geom = _resolve_geometry(node, sp_pr)
+
+ blip_fill_elem = sp_pr.find("a:blipFill", NS) if sp_pr is not None else None
+ blip_image = ""
+ if blip_fill_elem is not None:
+ blip_result = convert_blip_fill(
+ blip_fill_elem, node.xfrm, ctx.slide_part, ctx.pkg,
+ media_subdir=ctx.media_subdir,
+ embed_inline=ctx.embed_images,
+ )
+ if blip_result.svg:
+ blip_image = _clip_blip_image(blip_result.svg, geom, ctx)
+ ctx.media.update(blip_result.media)
+
+ # Geometry (fill is "none" when blipFill is present, so only stroke draws)
+ geom_xml = _build_geometry_xml(node, sp_pr, ctx, geom=geom)
# Text body (a:txBody)
tx_body = node.xml.find("p:txBody", NS)
@@ -179,7 +196,9 @@ def _convert_shape(node: ShapeNode, ctx: AssemblyContext, *, top_level: bool) ->
) if tx_body is not None else TextResult()
if is_vertical:
- shape_xml = _wrap_shape_group(geom_xml, node, ctx, top_level=top_level)
+ # Vertical text: geometry + image in one group, text in separate group
+ geom_inner = (blip_image + "\n" + geom_xml) if blip_image else geom_xml
+ shape_xml = _wrap_shape_group(geom_inner, node, ctx, top_level=top_level)
if not text_result.svg:
return shape_xml
text_group = (
@@ -189,14 +208,20 @@ def _convert_shape(node: ShapeNode, ctx: AssemblyContext, *, top_level: bool) ->
)
return f"{shape_xml}\n{text_group}"
- inner = (geom_xml + ("\n" + text_result.svg if text_result.svg else ""))
+ # Normal: image (behind) + geometry (stroke) + text (top)
+ inner_parts = []
+ if blip_image:
+ inner_parts.append(blip_image)
+ if geom_xml:
+ inner_parts.append(geom_xml)
+ if text_result.svg:
+ inner_parts.append(text_result.svg)
+ inner = "\n".join(inner_parts) if inner_parts else ""
return _wrap_shape_group(inner, node, ctx, top_level=top_level)
-def _build_geometry_xml(node: ShapeNode, sp_pr: ET.Element | None,
- ctx: AssemblyContext) -> str:
- """Build the SVG geometry element with fill/stroke/effect attributes."""
- # Resolve geometry
+def _resolve_geometry(node: ShapeNode, sp_pr: ET.Element | None) -> GeomResult | None:
+ """Resolve a DrawingML shape geometry into an absolute SVG geometry model."""
prst_geom = sp_pr.find("a:prstGeom", NS) if sp_pr is not None else None
cust_geom = sp_pr.find("a:custGeom", NS) if sp_pr is not None else None
@@ -216,8 +241,19 @@ def _build_geometry_xml(node: ShapeNode, sp_pr: ET.Element | None,
geom = convert_prst_geom("rect", node.xfrm, None)
if geom is None:
- return ""
+ return None
if geom.tag != "line" and (node.xfrm.w <= 0 or node.xfrm.h <= 0):
+ return None
+ return geom
+
+
+def _build_geometry_xml(node: ShapeNode, sp_pr: ET.Element | None,
+ ctx: AssemblyContext,
+ geom: GeomResult | None = None) -> str:
+ """Build the SVG geometry element with fill/stroke/effect attributes."""
+ if geom is None:
+ geom = _resolve_geometry(node, sp_pr)
+ if geom is None:
return ""
# Fill / stroke / effect
@@ -251,12 +287,43 @@ def _build_geometry_xml(node: ShapeNode, sp_pr: ET.Element | None,
pass
geom_attrs_xml = _attrs_to_xml({**geom.attrs, **attrs})
+ return _geom_to_svg(geom, geom_attrs_xml)
+
+def _geom_to_svg(geom: GeomResult, attrs_xml: str = "") -> str:
+ """Serialize a resolved geometry with optional SVG attributes."""
if geom.tag == "path":
- return f'<path d="{geom.path_d}"{geom_attrs_xml}/>'
+ return f'<path d="{geom.path_d}"{attrs_xml}/>'
if geom.tag in ("polygon", "polyline"):
- return f'<{geom.tag} points="{geom.points}"{geom_attrs_xml}/>'
- return f"<{geom.tag}{geom_attrs_xml}/>"
+ return f'<{geom.tag} points="{geom.points}"{attrs_xml}/>'
+ return f"<{geom.tag}{attrs_xml}/>"
+
+
+def _clip_blip_image(image_xml: str, geom: GeomResult | None,
+ ctx: AssemblyContext) -> str:
+ """Clip image fills to the owning shape geometry when it is not a plain rect."""
+ if geom is None or geom.tag == "line":
+ return image_xml
+ if geom.tag == "rect" and not geom.attrs.get("rx") and not geom.attrs.get("ry"):
+ return image_xml
+
+ ctx.clip_seq[0] += 1
+ clip_id = f"{ctx.group_id_prefix}clip{ctx.clip_seq[0]}"
+ clip_shape = _geom_to_svg(geom)
+ ctx.defs.append(
+ f'<clipPath id="{clip_id}" clipPathUnits="userSpaceOnUse">'
+ f'{clip_shape}</clipPath>'
+ )
+ return _inject_clip_path(image_xml, clip_id)
+
+
+def _inject_clip_path(image_xml: str, clip_id: str) -> str:
+ clip_attr = f' clip-path="url(#{clip_id})"'
+ if image_xml.startswith("<image"):
+ return image_xml.replace("<image", f"<image{clip_attr}", 1)
+ if image_xml.startswith("<svg"):
+ return image_xml.replace("<svg", f"<svg{clip_attr}", 1)
+ return image_xml
# ---------------------------------------------------------------------------