diff --git a/src/docx/image/__init__.py b/src/docx/image/__init__.py index d28033ef1..c67cc32a0 100644 --- a/src/docx/image/__init__.py +++ b/src/docx/image/__init__.py @@ -9,6 +9,7 @@ from docx.image.jpeg import Exif, Jfif from docx.image.png import Png from docx.image.tiff import Tiff +from docx.image.svg import Svg SIGNATURES = ( # class, offset, signature_bytes @@ -20,4 +21,5 @@ (Tiff, 0, b"MM\x00*"), # big-endian (Motorola) TIFF (Tiff, 0, b"II*\x00"), # little-endian (Intel) TIFF (Bmp, 0, b"BM"), + (Svg, 0, b"\n" ' \n' " \n" - "" % nsdecls("wp", "a", "pic", "r") + "" % nsdecls("wp", "a", "pic", "r", "asvg") ) @@ -135,14 +136,48 @@ def new(cls, pic_id, filename, rId, cx, cy): """Return a new ```` element populated with the minimal contents required to define a viable picture element, based on the values passed as parameters.""" - pic = parse_xml(cls._pic_xml()) + if filename.endswith(".svg"): + pic = parse_xml(cls._pic_xml_svg()) + pic.blipFill.blip.extLst.ext.svgBlip.embed = rId + else: + pic = parse_xml(cls._pic_xml()) + pic.blipFill.blip.embed = rId pic.nvPicPr.cNvPr.id = pic_id pic.nvPicPr.cNvPr.name = filename - pic.blipFill.blip.embed = rId pic.spPr.cx = cx pic.spPr.cy = cy return pic + @classmethod + def _pic_xml_svg(cls): + return ( + "\n" + " \n" + ' \n' + " \n" + " \n" + " \n" + " \n" + " \n" + ' \n' + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + ' \n' + ' \n' + " \n" + ' \n' + " \n" + "" % nsdecls("pic", "a", "r", "asvg") + ) + @classmethod def _pic_xml(cls): return ( @@ -164,7 +199,7 @@ def _pic_xml(cls): " \n" ' \n' " \n" - "" % nsdecls("pic", "a", "r") + "" % nsdecls("pic", "a", "r", "asvg") ) @@ -192,6 +227,7 @@ class CT_PositiveSize2D(BaseOxmlElement): cx = RequiredAttribute("cx", ST_PositiveCoordinate) cy = RequiredAttribute("cy", ST_PositiveCoordinate) + svgBlip = ZeroOrOne("asvg:svgBlip") class CT_PresetGeometry2D(BaseOxmlElement): @@ -258,6 +294,7 @@ class CT_Transform2D(BaseOxmlElement): off = ZeroOrOne("a:off", successors=("a:ext",)) ext = ZeroOrOne("a:ext", successors=()) + embed = OptionalAttribute("r:embed", ST_RelationshipId) @property def cx(self):