"""Attach SVG as Word svgBlip alongside a PNG InlineShape (Office 2016+).""" from __future__ import annotations from pathlib import Path from docx.opc.constants import RELATIONSHIP_TYPE as RT from docx.opc.part import Part from docx.oxml import OxmlElement from docx.oxml.ns import qn from lxml import etree SVG_CONTENT_TYPE = "image/svg+xml" SVG_BLIP_URI = "{96DAC541-7B7A-43C6-8E14-B03AE682B59D}" ASVG_NS = "http://schemas.microsoft.com/office/drawing/2016/SVG/main" ASVG_SVG_BLIP = f"{{{ASVG_NS}}}svgBlip" def attach_svg_blip(run, inline_shape, svg_path: str | Path) -> bool: """ After run.add_picture(png), add an SVG part and asvg:svgBlip on the PNG blip. Word 2016+ uses the vector; older apps keep the PNG. Returns True if the SVG was attached. """ path = Path(svg_path) if not path.is_file() or path.stat().st_size <= 0: return False data = path.read_bytes() head = data.lstrip()[:200].lower() if not (head.startswith(b"