44Custom element classes for shape-related elements like ``<w:inline>``
55"""
66
7- from . import OxmlElement , parse_xml
8- from .ns import nsdecls , nsmap , nspfxmap
7+ from . import parse_xml
8+ from .ns import nsdecls
99from .simpletypes import (
1010 ST_Coordinate , ST_DrawingElementId , ST_PositiveCoordinate ,
1111 ST_RelationshipId , XsdString , XsdToken
@@ -40,12 +40,6 @@ class CT_GraphicalObject(BaseOxmlElement):
4040 """
4141 graphicData = OneAndOnlyOne ('a:graphicData' )
4242
43- @classmethod
44- def new (cls , uri , pic ):
45- graphic = OxmlElement ('a:graphic' )
46- graphic .append (CT_GraphicalObjectData .new (uri , pic ))
47- return graphic
48-
4943
5044class CT_GraphicalObjectData (BaseOxmlElement ):
5145 """
@@ -54,19 +48,13 @@ class CT_GraphicalObjectData(BaseOxmlElement):
5448 pic = ZeroOrOne ('pic:pic' )
5549 uri = RequiredAttribute ('uri' , XsdToken )
5650
57- @classmethod
58- def new (cls , uri , pic ):
59- graphicData = OxmlElement ('a:graphicData' )
60- graphicData .uri = uri
61- graphicData ._insert_pic (pic )
62- return graphicData
63-
6451
6552class CT_Inline (BaseOxmlElement ):
6653 """
6754 ``<w:inline>`` element, container for an inline shape.
6855 """
6956 extent = OneAndOnlyOne ('wp:extent' )
57+ docPr = OneAndOnlyOne ('wp:docPr' )
7058 graphic = OneAndOnlyOne ('a:graphic' )
7159
7260 @classmethod
@@ -75,17 +63,32 @@ def new(cls, cx, cy, shape_id, pic):
7563 Return a new ``<wp:inline>`` element populated with the values passed
7664 as parameters.
7765 """
78- name = 'Picture %d' % shape_id
79- uri = nsmap [ 'pic' ]
80-
81- inline = OxmlElement ( 'wp:inline' , nsdecls = nspfxmap ( 'wp' , 'r' ))
82- inline .append ( CT_PositiveSize2D . new ( 'wp:extent' , cx , cy ))
83- inline .append ( CT_NonVisualDrawingProps . new (
84- 'wp:docPr' , shape_id , name
85- ))
86- inline .append ( CT_GraphicalObject . new ( uri , pic ) )
66+ inline = parse_xml ( cls . _inline_xml ())
67+ inline . extent . cx = cx
68+ inline . extent . cy = cy
69+ inline . docPr . id = shape_id
70+ inline .docPr . name = 'Picture %d' % shape_id
71+ inline .graphic . graphicData . uri = (
72+ 'http://schemas.openxmlformats.org/drawingml/2006/picture'
73+ )
74+ inline .graphic . graphicData . _insert_pic ( pic )
8775 return inline
8876
77+ @classmethod
78+ def _inline_xml (cls ):
79+ return (
80+ '<wp:inline %s>\n '
81+ ' <wp:extent cx="914400" cy="914400"/>\n '
82+ ' <wp:docPr id="666" name="unnamed"/>\n '
83+ ' <wp:cNvGraphicFramePr>\n '
84+ ' <a:graphicFrameLocks noChangeAspect="1"/>\n '
85+ ' </wp:cNvGraphicFramePr>\n '
86+ ' <a:graphic>\n '
87+ ' <a:graphicData uri="URI not set"/>\n '
88+ ' </a:graphic>\n '
89+ '</wp:inline>' % nsdecls ('wp' , 'a' , 'pic' , 'r' )
90+ )
91+
8992
9093class CT_NonVisualDrawingProps (BaseOxmlElement ):
9194 """
@@ -95,13 +98,6 @@ class CT_NonVisualDrawingProps(BaseOxmlElement):
9598 id = RequiredAttribute ('id' , ST_DrawingElementId )
9699 name = RequiredAttribute ('name' , XsdString )
97100
98- @classmethod
99- def new (cls , nsptagname_str , shape_id , name ):
100- elm = OxmlElement (nsptagname_str )
101- elm .set ('id' , str (shape_id ))
102- elm .set ('name' , name )
103- return elm
104-
105101
106102class CT_NonVisualPictureProperties (BaseOxmlElement ):
107103 """
@@ -182,13 +178,6 @@ class CT_PositiveSize2D(BaseOxmlElement):
182178 cx = RequiredAttribute ('cx' , ST_PositiveCoordinate )
183179 cy = RequiredAttribute ('cy' , ST_PositiveCoordinate )
184180
185- @classmethod
186- def new (cls , nsptagname_str , cx , cy ):
187- elm = OxmlElement (nsptagname_str )
188- elm .cx = cx
189- elm .cy = cy
190- return elm
191-
192181
193182class CT_PresetGeometry2D (BaseOxmlElement ):
194183 """
0 commit comments