44Custom element classes for shape-related elements like ``<w:inline>``
55"""
66
7- from docx .oxml .shared import OxmlBaseElement , qn
7+ from docx .oxml .shared import (
8+ nsmap , nspfxmap , OxmlBaseElement , OxmlElement , qn
9+ )
810
911
1012class CT_Blip (OxmlBaseElement ):
@@ -16,6 +18,12 @@ class CT_Blip(OxmlBaseElement):
1618 def link (self ):
1719 return self .get (qn ('r:link' ))
1820
21+ @classmethod
22+ def new (cls , rId ):
23+ blip = OxmlElement ('a:blip' )
24+ blip .set (qn ('r:embed' ), rId )
25+ return blip
26+
1927
2028class CT_BlipFillProperties (OxmlBaseElement ):
2129 """
@@ -25,6 +33,13 @@ class CT_BlipFillProperties(OxmlBaseElement):
2533 def blip (self ):
2634 return self .find (qn ('a:blip' ))
2735
36+ @classmethod
37+ def new (cls , rId ):
38+ blipFill = OxmlElement ('pic:blipFill' )
39+ blipFill .append (CT_Blip .new (rId ))
40+ blipFill .append (CT_StretchInfoProperties .new ())
41+ return blipFill
42+
2843
2944class CT_GraphicalObject (OxmlBaseElement ):
3045 """
@@ -34,11 +49,24 @@ class CT_GraphicalObject(OxmlBaseElement):
3449 def graphicData (self ):
3550 return self .find (qn ('a:graphicData' ))
3651
52+ @classmethod
53+ def new (cls , uri , pic ):
54+ graphic = OxmlElement ('a:graphic' )
55+ graphic .append (CT_GraphicalObjectData .new (uri , pic ))
56+ return graphic
57+
3758
3859class CT_GraphicalObjectData (OxmlBaseElement ):
3960 """
4061 ``<a:graphicData>`` element, container for the XML of a DrawingML object
4162 """
63+ @classmethod
64+ def new (cls , uri , pic ):
65+ graphicData = OxmlElement ('a:graphicData' )
66+ graphicData .set ('uri' , uri )
67+ graphicData .append (pic )
68+ return graphicData
69+
4270 @property
4371 def pic (self ):
4472 return self .find (qn ('pic:pic' ))
@@ -56,6 +84,46 @@ class CT_Inline(OxmlBaseElement):
5684 def graphic (self ):
5785 return self .find (qn ('a:graphic' ))
5886
87+ @classmethod
88+ def new (cls , width , height , shape_id , pic ):
89+ """
90+ Return a new ``<wp:inline>`` element populated with the values passed
91+ as parameters.
92+ """
93+ name = 'Picture %d' % shape_id
94+ uri = nsmap ['pic' ]
95+
96+ inline = OxmlElement ('wp:inline' , nsmap = nspfxmap ('wp' , 'r' ))
97+ inline .append (CT_PositiveSize2D .new ('wp:extent' , width , height ))
98+ inline .append (CT_NonVisualDrawingProps .new (
99+ 'wp:docPr' , shape_id , name
100+ ))
101+ inline .append (CT_GraphicalObject .new (uri , pic ))
102+ return inline
103+
104+
105+ class CT_NonVisualDrawingProps (OxmlBaseElement ):
106+ """
107+ Used for ``<wp:docPr>`` element, and perhaps others. Specifies the id and
108+ name of a DrawingML drawing.
109+ """
110+ @classmethod
111+ def new (cls , nsptagname_str , shape_id , name ):
112+ elt = OxmlElement (nsptagname_str )
113+ elt .set ('id' , str (shape_id ))
114+ elt .set ('name' , name )
115+ return elt
116+
117+
118+ class CT_NonVisualPictureProperties (OxmlBaseElement ):
119+ """
120+ ``<pic:cNvPicPr>`` element, specifies picture locking and resize
121+ behaviors.
122+ """
123+ @classmethod
124+ def new (cls ):
125+ return OxmlElement ('pic:cNvPicPr' )
126+
59127
60128class CT_Picture (OxmlBaseElement ):
61129 """
@@ -64,3 +132,114 @@ class CT_Picture(OxmlBaseElement):
64132 @property
65133 def blipFill (self ):
66134 return self .find (qn ('pic:blipFill' ))
135+
136+ @classmethod
137+ def new (cls , pic_id , filename , rId , cx , cy ):
138+ """
139+ Return a new ``<pic:pic>`` element populated with the minimal
140+ contents required to define a viable picture element, based on the
141+ values passed as parameters.
142+ """
143+ pic = OxmlElement ('pic:pic' , nsmap = nspfxmap ('pic' , 'r' ))
144+ pic .append (CT_PictureNonVisual .new (pic_id , filename ))
145+ pic .append (CT_BlipFillProperties .new (rId ))
146+ pic .append (CT_ShapeProperties .new (cx , cy ))
147+ return pic
148+
149+
150+ class CT_PictureNonVisual (OxmlBaseElement ):
151+ """
152+ ``<pic:nvPicPr>`` element, non-visual picture properties
153+ """
154+ @classmethod
155+ def new (cls , pic_id , image_filename ):
156+ nvPicPr = OxmlElement ('pic:nvPicPr' )
157+ nvPicPr .append (CT_NonVisualDrawingProps .new (
158+ 'pic:cNvPr' , pic_id , image_filename
159+ ))
160+ nvPicPr .append (CT_NonVisualPictureProperties .new ())
161+ return nvPicPr
162+
163+
164+ class CT_Point2D (OxmlBaseElement ):
165+ """
166+ Used for ``<a:off>`` element, and perhaps others. Specifies an x, y
167+ coordinate (point).
168+ """
169+ @classmethod
170+ def new (cls , nsptagname_str , x , y ):
171+ elm = OxmlElement (nsptagname_str )
172+ elm .set ('x' , str (x ))
173+ elm .set ('y' , str (y ))
174+ return elm
175+
176+
177+ class CT_PositiveSize2D (OxmlBaseElement ):
178+ """
179+ Used for ``<wp:extent>`` element, and perhaps others later. Specifies the
180+ size of a DrawingML drawing.
181+ """
182+ @classmethod
183+ def new (cls , nsptagname_str , cx , cy ):
184+ elt = OxmlElement (nsptagname_str )
185+ elt .set ('cx' , str (cx ))
186+ elt .set ('cy' , str (cy ))
187+ return elt
188+
189+
190+ class CT_PresetGeometry2D (OxmlBaseElement ):
191+ """
192+ ``<a:prstGeom>`` element, specifies an preset autoshape geometry, such
193+ as ``rect``.
194+ """
195+ @classmethod
196+ def new (cls , prst ):
197+ prstGeom = OxmlElement ('a:prstGeom' )
198+ prstGeom .set ('prst' , prst )
199+ return prstGeom
200+
201+
202+ class CT_RelativeRect (OxmlBaseElement ):
203+ """
204+ ``<a:fillRect>`` element, specifying picture should fill containing
205+ rectangle shape.
206+ """
207+ @classmethod
208+ def new (cls ):
209+ return OxmlElement ('a:fillRect' )
210+
211+
212+ class CT_ShapeProperties (OxmlBaseElement ):
213+ """
214+ ``<pic:spPr>`` element, specifies size and shape of picture container.
215+ """
216+ @classmethod
217+ def new (cls , cx , cy ):
218+ spPr = OxmlElement ('pic:spPr' )
219+ spPr .append (CT_Transform2D .new (cx , cy ))
220+ spPr .append (CT_PresetGeometry2D .new ('rect' ))
221+ return spPr
222+
223+
224+ class CT_StretchInfoProperties (OxmlBaseElement ):
225+ """
226+ ``<a:stretch>`` element, specifies how picture should fill its containing
227+ shape.
228+ """
229+ @classmethod
230+ def new (cls ):
231+ stretch = OxmlElement ('a:stretch' )
232+ stretch .append (CT_RelativeRect .new ())
233+ return stretch
234+
235+
236+ class CT_Transform2D (OxmlBaseElement ):
237+ """
238+ ``<a:xfrm>`` element, specifies size and shape of picture container.
239+ """
240+ @classmethod
241+ def new (cls , cx , cy ):
242+ spPr = OxmlElement ('a:xfrm' )
243+ spPr .append (CT_Point2D .new ('a:off' , 0 , 0 ))
244+ spPr .append (CT_PositiveSize2D .new ('a:ext' , cx , cy ))
245+ return spPr
0 commit comments