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(svg_to_pptx): honor inline styles and CSS colors

hugohe367c4535b68ca
diff --git a/skills/ppt-master/scripts/svg_to_pptx/drawingml_elements.py b/skills/ppt-master/scripts/svg_to_pptx/drawingml_elements.py
index ff70b3b5..e71f28cb 100644
--- a/skills/ppt-master/scripts/svg_to_pptx/drawingml_elements.py
+++ b/skills/ppt-master/scripts/svg_to_pptx/drawingml_elements.py
@@ -18,7 +18,7 @@ from .drawingml_utils import (
ctx_x, ctx_y, ctx_w, ctx_h,
rect_to_dml_xfrm,
parse_hex_color, resolve_url_id, get_effective_filter_id,
- parse_font_family, is_cjk_char, estimate_text_width,
+ parse_inline_style, parse_font_family, is_cjk_char, estimate_text_width,
detect_text_lang, resolve_text_run_fonts,
matrix_multiply, parse_transform_matrix, transform_point, _xml_escape,
)
@@ -1097,42 +1097,47 @@ def _override_run_attrs(
) -> dict[str, Any]:
"""Layer a tspan's styling attributes over the inherited run attrs."""
run_attrs = dict(parent_attrs)
- if tspan.get('font-weight'):
- run_attrs['font_weight'] = tspan.get('font-weight')
- if tspan.get('fill'):
- child_fill = tspan.get('fill')
+ inline_style = parse_inline_style(tspan.get('style'))
+
+ def tspan_attr(name: str) -> str | None:
+ return inline_style.get(name) or tspan.get(name)
+
+ if tspan_attr('font-weight'):
+ run_attrs['font_weight'] = tspan_attr('font-weight')
+ if tspan_attr('fill'):
+ child_fill = tspan_attr('fill')
run_attrs['fill_raw'] = child_fill
c = parse_hex_color(child_fill)
if c:
run_attrs['fill'] = c
- if tspan.get('stroke'):
- run_attrs['stroke_raw'] = tspan.get('stroke')
- if tspan.get('stroke-width'):
+ if tspan_attr('stroke'):
+ run_attrs['stroke_raw'] = tspan_attr('stroke')
+ if tspan_attr('stroke-width'):
run_attrs['stroke_width'] = parse_svg_length(
- tspan.get('stroke-width'),
+ tspan_attr('stroke-width'),
run_attrs.get('stroke_width', 1.0),
font_size=float(run_attrs.get('font_size', 16)),
)
- if tspan.get('stroke-opacity'):
+ if tspan_attr('stroke-opacity'):
try:
- run_attrs['stroke_opacity'] = float(tspan.get('stroke-opacity', '1'))
+ run_attrs['stroke_opacity'] = float(tspan_attr('stroke-opacity') or '1')
except ValueError:
pass
- if tspan.get('font-size'):
+ if tspan_attr('font-size'):
run_attrs['font_size'] = parse_svg_length(
- tspan.get('font-size'),
+ tspan_attr('font-size'),
run_attrs['font_size'],
font_size=float(run_attrs.get('font_size', 16)),
)
- if tspan.get('font-family'):
- run_attrs['font_family'] = tspan.get('font-family')
- if tspan.get('font-style'):
- run_attrs['font_style'] = tspan.get('font-style')
- if tspan.get('text-decoration'):
- run_attrs['text_decoration'] = tspan.get('text-decoration')
- if tspan.get('letter-spacing'):
+ if tspan_attr('font-family'):
+ run_attrs['font_family'] = tspan_attr('font-family')
+ if tspan_attr('font-style'):
+ run_attrs['font_style'] = tspan_attr('font-style')
+ if tspan_attr('text-decoration'):
+ run_attrs['text_decoration'] = tspan_attr('text-decoration')
+ if tspan_attr('letter-spacing'):
run_attrs['letter_spacing'] = _parse_letter_spacing_px(
- tspan.get('letter-spacing'),
+ tspan_attr('letter-spacing'),
font_size=float(run_attrs.get('font_size', 16)),
scale_x=float(run_attrs.get('_scale_x', 1.0)),
)
@@ -1213,7 +1218,7 @@ def _build_text_fill_xml(
ctx: ConvertContext | None,
) -> str:
"""Build DrawingML fill XML for a text run."""
- if fill_raw == 'none':
+ if fill_raw.strip().lower() in ('none', 'transparent'):
return '<a:noFill/>'
grad_id = resolve_url_id(fill_raw)
@@ -1229,7 +1234,7 @@ def _build_text_fill_xml(
def _build_text_outline_xml(run: dict[str, Any]) -> str:
"""Build DrawingML outline XML for a text run from SVG stroke attributes."""
stroke_raw = run.get('stroke_raw')
- if not stroke_raw or stroke_raw == 'none':
+ if not stroke_raw or stroke_raw.strip().lower() in ('none', 'transparent'):
return ''
color = parse_hex_color(stroke_raw)
diff --git a/skills/ppt-master/scripts/svg_to_pptx/drawingml_styles.py b/skills/ppt-master/scripts/svg_to_pptx/drawingml_styles.py
index 4693afee..7d34f2fb 100644
--- a/skills/ppt-master/scripts/svg_to_pptx/drawingml_styles.py
+++ b/skills/ppt-master/scripts/svg_to_pptx/drawingml_styles.py
@@ -9,7 +9,7 @@ from xml.etree import ElementTree as ET
from .drawingml_context import ConvertContext
from .drawingml_utils import (
SVG_NS, ANGLE_UNIT, DASH_PRESETS,
- px_to_emu, _f, _get_attr,
+ px_to_emu, _f, _get_attr, parse_svg_length,
parse_hex_color, parse_stop_style, resolve_url_id,
)
@@ -118,7 +118,7 @@ def build_fill_xml(
if fill is None:
fill = '#000000' # SVG default fill is black
- if fill == 'none':
+ if fill.strip().lower() in ('none', 'transparent'):
return '<a:noFill/>'
ref_id = resolve_url_id(fill)
@@ -343,10 +343,10 @@ def build_stroke_xml(
) -> str:
"""Build <a:ln> XML for stroke, with inherited style support."""
stroke = _get_attr(elem, 'stroke', ctx)
- if not stroke or stroke == 'none':
+ if not stroke or stroke.strip().lower() in ('none', 'transparent'):
return '<a:ln><a:noFill/></a:ln>'
- width = _f(_get_attr(elem, 'stroke-width', ctx), 1.0)
+ width = parse_svg_length(_get_attr(elem, 'stroke-width', ctx), 1.0)
width_emu = px_to_emu(width)
# Dash pattern
diff --git a/skills/ppt-master/scripts/svg_to_pptx/drawingml_utils.py b/skills/ppt-master/scripts/svg_to_pptx/drawingml_utils.py
index 4423b974..213b8f6e 100644
--- a/skills/ppt-master/scripts/svg_to_pptx/drawingml_utils.py
+++ b/skills/ppt-master/scripts/svg_to_pptx/drawingml_utils.py
@@ -356,11 +356,19 @@ def _extract_inheritable_styles(elem: ET.Element) -> dict[str, str]:
val = elem.get(attr)
if val is not None:
styles[attr] = val
+ styles.update({
+ attr: val
+ for attr, val in parse_inline_style(elem.get('style')).items()
+ if attr in INHERITABLE_ATTRS
+ })
return styles
def _get_attr(elem: ET.Element, attr: str, ctx: ConvertContext) -> str | None:
"""Get effective attribute: element's own value first, then inherited."""
+ style_val = parse_inline_style(elem.get('style')).get(attr)
+ if style_val is not None:
+ return style_val
val = elem.get(attr)
if val is not None:
return val
@@ -391,11 +399,82 @@ def ctx_h(val: float, ctx: ConvertContext) -> float:
# Color / style parsing
# ---------------------------------------------------------------------------
+_CSS_NAMED_COLORS = {
+ 'black': '000000',
+ 'silver': 'C0C0C0',
+ 'gray': '808080',
+ 'grey': '808080',
+ 'white': 'FFFFFF',
+ 'maroon': '800000',
+ 'red': 'FF0000',
+ 'purple': '800080',
+ 'fuchsia': 'FF00FF',
+ 'magenta': 'FF00FF',
+ 'green': '008000',
+ 'lime': '00FF00',
+ 'olive': '808000',
+ 'yellow': 'FFFF00',
+ 'navy': '000080',
+ 'blue': '0000FF',
+ 'teal': '008080',
+ 'aqua': '00FFFF',
+ 'cyan': '00FFFF',
+ 'orange': 'FFA500',
+ 'brown': 'A52A2A',
+ 'pink': 'FFC0CB',
+ 'gold': 'FFD700',
+ 'transparent': None,
+ 'lightgray': 'D3D3D3',
+ 'lightgrey': 'D3D3D3',
+ 'darkgray': 'A9A9A9',
+ 'darkgrey': 'A9A9A9',
+}
+
+
+def parse_inline_style(style_str: str | None) -> dict[str, str]:
+ """Parse an SVG inline style declaration into ``property: value`` pairs."""
+ styles: dict[str, str] = {}
+ if not style_str:
+ return styles
+ for part in style_str.split(';'):
+ if ':' not in part:
+ continue
+ name, value = part.split(':', 1)
+ name = name.strip().lower()
+ value = value.strip()
+ if name and value:
+ styles[name] = value
+ return styles
+
+
+def _parse_color_channel(raw: str) -> int:
+ raw = raw.strip()
+ if raw.endswith('%'):
+ value = float(raw[:-1]) * 255.0 / 100.0
+ else:
+ value = float(raw)
+ return max(0, min(255, int(round(value))))
+
+
def parse_hex_color(color_str: str) -> str | None:
- """Parse '#RRGGBB' or '#RGB' to 'RRGGBB'. Returns None on failure."""
+ """Parse SVG color values to 'RRGGBB'. Returns None on failure."""
if not color_str:
return None
color_str = color_str.strip()
+ named = _CSS_NAMED_COLORS.get(color_str.lower())
+ if named is not None or color_str.lower() in _CSS_NAMED_COLORS:
+ return named
+
+ rgb_match = re.match(r'rgba?\((.+)\)$', color_str, flags=re.IGNORECASE)
+ if rgb_match:
+ channels = re.findall(r'[-+]?(?:\d*\.\d+|\d+\.?)(?:[eE][-+]?\d+)?%?', rgb_match.group(1))
+ if len(channels) >= 3:
+ try:
+ r, g, b = (_parse_color_channel(ch) for ch in channels[:3])
+ return f'{r:02X}{g:02X}{b:02X}'
+ except ValueError:
+ return None
+
if color_str.startswith('#'):
color_str = color_str[1:]
if len(color_str) == 3: