Add basic support for adding SVG pictures to docx files by lsaint · Pull Request #27 · lsaint/python-docx-oss

@@ -1,6 +1,7 @@ """ Custom element classes for shape-related elements like ``<w:inline>`` """
from docx.oxml import parse_xml from docx.oxml.ns import nsdecls from docx.oxml.xmlchemy import BaseOxmlElement, OneAndOnlyOne Expand Down Expand Up @@ -31,6 +32,7 @@ class CT_Blip(BaseOxmlElement):
embed = OptionalAttribute("r:embed", ST_RelationshipId) link = OptionalAttribute("r:link", ST_RelationshipId) extLst = ZeroOrOne("a:extLst")

class CT_BlipFillProperties(BaseOxmlElement): Expand Down Expand Up @@ -108,7 +110,7 @@ def _inline_xml(cls): " <a:graphic>\n" ' <a:graphicData uri="URI not set"/>\n' " </a:graphic>\n" "</wp:inline>" % nsdecls("wp", "a", "pic", "r") "</wp:inline>" % nsdecls("wp", "a", "pic", "r", "asvg") )

Expand Down Expand Up @@ -145,14 +147,48 @@ def new(cls, pic_id, filename, rId, cx, cy): 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 ( "<pic:pic %s>\n" " <pic:nvPicPr>\n" ' <pic:cNvPr id="666" name="unnamed"/>\n' " <pic:cNvPicPr/>\n" " </pic:nvPicPr>\n" " <pic:blipFill>\n" " <a:blip>\n" " <a:extLst>\n" ' <a:ext uri="{96DAC541-7B7A-43D3-8B79-37D633B846F1}">\n' " <asvg:svgBlip/>\n" " </a:ext>\n" " </a:extLst>\n" " </a:blip>\n" " <a:stretch>\n" " <a:fillRect/>\n" " </a:stretch>\n" " </pic:blipFill>\n" " <pic:spPr>\n" " <a:xfrm>\n" ' <a:off x="0" y="0"/>\n' ' <a:ext cx="914400" cy="914400"/>\n' " </a:xfrm>\n" ' <a:prstGeom prst="rect"/>\n' " </pic:spPr>\n" "</pic:pic>" % nsdecls("pic", "a", "r", "asvg") )
@classmethod def _pic_xml(cls): return ( Expand All @@ -174,7 +210,7 @@ def _pic_xml(cls): " </a:xfrm>\n" ' <a:prstGeom prst="rect"/>\n' " </pic:spPr>\n" "</pic:pic>" % nsdecls("pic", "a", "r") "</pic:pic>" % nsdecls("pic", "a", "r", "asvg") )

Expand Down Expand Up @@ -204,6 +240,7 @@ class CT_PositiveSize2D(BaseOxmlElement):
cx = RequiredAttribute("cx", ST_PositiveCoordinate) cy = RequiredAttribute("cy", ST_PositiveCoordinate) svgBlip = ZeroOrOne("asvg:svgBlip")

class CT_PresetGeometry2D(BaseOxmlElement): Expand Down Expand Up @@ -260,9 +297,7 @@ def cy(self): Shape height as an instance of Emu, or None if not present. """ xfrm = self.xfrm if xfrm is None: return None return xfrm.cy return None if xfrm is None else xfrm.cy
@cy.setter def cy(self, value): Expand All @@ -284,13 +319,12 @@ 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): ext = self.ext if ext is None: return None return ext.cx return None if ext is None else ext.cx
@cx.setter def cx(self, value): Expand All @@ -300,9 +334,7 @@ def cx(self, value): @property def cy(self): ext = self.ext if ext is None: return None return ext.cy return None if ext is None else ext.cy
@cy.setter def cy(self, value): Expand Down