python-docx-oss 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (132) hide show
  1. docx/__init__.py +71 -0
  2. docx/api.py +37 -0
  3. docx/blkcntnr.py +101 -0
  4. docx/comments.py +163 -0
  5. docx/dml/__init__.py +0 -0
  6. docx/dml/color.py +112 -0
  7. docx/document.py +285 -0
  8. docx/drawing/__init__.py +59 -0
  9. docx/enum/__init__.py +0 -0
  10. docx/enum/base.py +150 -0
  11. docx/enum/dml.py +103 -0
  12. docx/enum/section.py +86 -0
  13. docx/enum/shape.py +19 -0
  14. docx/enum/style.py +452 -0
  15. docx/enum/table.py +136 -0
  16. docx/enum/text.py +367 -0
  17. docx/exceptions.py +18 -0
  18. docx/image/__init__.py +32 -0
  19. docx/image/bmp.py +43 -0
  20. docx/image/constants.py +172 -0
  21. docx/image/emf.py +46 -0
  22. docx/image/exceptions.py +13 -0
  23. docx/image/gif.py +38 -0
  24. docx/image/helpers.py +86 -0
  25. docx/image/image.py +237 -0
  26. docx/image/jpeg.py +469 -0
  27. docx/image/png.py +253 -0
  28. docx/image/svg.py +118 -0
  29. docx/image/tiff.py +289 -0
  30. docx/opc/__init__.py +0 -0
  31. docx/opc/constants.py +307 -0
  32. docx/opc/coreprops.py +142 -0
  33. docx/opc/customprops.py +59 -0
  34. docx/opc/exceptions.py +12 -0
  35. docx/opc/oxml.py +247 -0
  36. docx/opc/package.py +254 -0
  37. docx/opc/packuri.py +109 -0
  38. docx/opc/part.py +267 -0
  39. docx/opc/parts/__init__.py +0 -0
  40. docx/opc/parts/coreprops.py +48 -0
  41. docx/opc/parts/customprops.py +61 -0
  42. docx/opc/phys_pkg.py +119 -0
  43. docx/opc/pkgreader.py +254 -0
  44. docx/opc/pkgwriter.py +115 -0
  45. docx/opc/rel.py +169 -0
  46. docx/opc/shared.py +31 -0
  47. docx/opc/spec.py +24 -0
  48. docx/oxml/__init__.py +252 -0
  49. docx/oxml/comments.py +124 -0
  50. docx/oxml/coreprops.py +298 -0
  51. docx/oxml/customprops.py +141 -0
  52. docx/oxml/dml/CT_ExtensionList.py +48 -0
  53. docx/oxml/document.py +88 -0
  54. docx/oxml/drawing.py +11 -0
  55. docx/oxml/exceptions.py +10 -0
  56. docx/oxml/ns.py +111 -0
  57. docx/oxml/numbering.py +109 -0
  58. docx/oxml/parser.py +62 -0
  59. docx/oxml/section.py +537 -0
  60. docx/oxml/settings.py +138 -0
  61. docx/oxml/shape.py +426 -0
  62. docx/oxml/shared.py +52 -0
  63. docx/oxml/simpletypes.py +434 -0
  64. docx/oxml/styles.py +320 -0
  65. docx/oxml/table.py +977 -0
  66. docx/oxml/text/__init__.py +0 -0
  67. docx/oxml/text/font.py +358 -0
  68. docx/oxml/text/hyperlink.py +45 -0
  69. docx/oxml/text/pagebreak.py +278 -0
  70. docx/oxml/text/paragraph.py +106 -0
  71. docx/oxml/text/parfmt.py +392 -0
  72. docx/oxml/text/run.py +316 -0
  73. docx/oxml/xmlchemy.py +711 -0
  74. docx/package.py +119 -0
  75. docx/parts/__init__.py +0 -0
  76. docx/parts/comments.py +51 -0
  77. docx/parts/customxml.py +67 -0
  78. docx/parts/document.py +193 -0
  79. docx/parts/hdrftr.py +53 -0
  80. docx/parts/image.py +80 -0
  81. docx/parts/numbering.py +51 -0
  82. docx/parts/settings.py +50 -0
  83. docx/parts/story.py +113 -0
  84. docx/parts/styles.py +42 -0
  85. docx/py.typed +0 -0
  86. docx/section.py +479 -0
  87. docx/settings.py +35 -0
  88. docx/shape.py +103 -0
  89. docx/shared.py +382 -0
  90. docx/styles/__init__.py +40 -0
  91. docx/styles/latent.py +198 -0
  92. docx/styles/style.py +254 -0
  93. docx/styles/styles.py +136 -0
  94. docx/table.py +537 -0
  95. docx/templates/default-comments.xml +12 -0
  96. docx/templates/default-docx-template/[Content_Types].xml +17 -0
  97. docx/templates/default-docx-template/_rels/.rels +7 -0
  98. docx/templates/default-docx-template/customXml/_rels/item1.xml.rels +4 -0
  99. docx/templates/default-docx-template/customXml/item1.xml +2 -0
  100. docx/templates/default-docx-template/customXml/itemProps1.xml +6 -0
  101. docx/templates/default-docx-template/docProps/app.xml +36 -0
  102. docx/templates/default-docx-template/docProps/core.xml +13 -0
  103. docx/templates/default-docx-template/docProps/thumbnail.jpeg +0 -0
  104. docx/templates/default-docx-template/word/_rels/document.xml.rels +11 -0
  105. docx/templates/default-docx-template/word/document.xml +11 -0
  106. docx/templates/default-docx-template/word/fontTable.xml +61 -0
  107. docx/templates/default-docx-template/word/numbering.xml +201 -0
  108. docx/templates/default-docx-template/word/settings.xml +53 -0
  109. docx/templates/default-docx-template/word/styles.xml +11844 -0
  110. docx/templates/default-docx-template/word/stylesWithEffects.xml +11800 -0
  111. docx/templates/default-docx-template/word/theme/theme1.xml +318 -0
  112. docx/templates/default-docx-template/word/webSettings.xml +5 -0
  113. docx/templates/default-footer.xml +27 -0
  114. docx/templates/default-header.xml +27 -0
  115. docx/templates/default-numbering.xml +20 -0
  116. docx/templates/default-settings.xml +26 -0
  117. docx/templates/default-styles.xml +190 -0
  118. docx/templates/default.docx +0 -0
  119. docx/text/__init__.py +0 -0
  120. docx/text/font.py +466 -0
  121. docx/text/hyperlink.py +121 -0
  122. docx/text/pagebreak.py +104 -0
  123. docx/text/paragraph.py +173 -0
  124. docx/text/parfmt.py +286 -0
  125. docx/text/run.py +272 -0
  126. docx/text/tabstops.py +123 -0
  127. docx/types.py +34 -0
  128. python_docx_oss-0.1.0.dist-info/METADATA +57 -0
  129. python_docx_oss-0.1.0.dist-info/RECORD +132 -0
  130. python_docx_oss-0.1.0.dist-info/WHEEL +5 -0
  131. python_docx_oss-0.1.0.dist-info/licenses/LICENSE +21 -0
  132. python_docx_oss-0.1.0.dist-info/top_level.txt +1 -0
docx/oxml/shape.py ADDED
@@ -0,0 +1,426 @@
1
+ """Custom element classes for shape-related elements like `<w:inline>`."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from typing import TYPE_CHECKING, Any, cast
6
+
7
+ from docx.oxml.ns import nsdecls
8
+ from docx.oxml.parser import parse_xml
9
+ from docx.oxml.simpletypes import (
10
+ ST_Coordinate,
11
+ ST_DrawingElementId,
12
+ ST_PositiveCoordinate,
13
+ ST_RelationshipId,
14
+ XsdString,
15
+ XsdToken,
16
+ )
17
+ from docx.oxml.xmlchemy import (
18
+ BaseOxmlElement,
19
+ OneAndOnlyOne,
20
+ OptionalAttribute,
21
+ RequiredAttribute,
22
+ ZeroOrOne,
23
+ )
24
+
25
+ if TYPE_CHECKING:
26
+ from docx.shared import Length
27
+ from docx.oxml.dml.CT_ExtensionList import CT_ExtensionList
28
+
29
+
30
+ class CT_Anchor(BaseOxmlElement):
31
+ """`<w:anchor>` element, container for a floating image."""
32
+
33
+ extent: CT_PositiveSize2D = OneAndOnlyOne("wp:extent") # pyright: ignore[reportAssignmentType]
34
+ docPr: CT_NonVisualDrawingProps = OneAndOnlyOne( # pyright: ignore[reportAssignmentType]
35
+ "wp:docPr"
36
+ )
37
+ graphic: CT_GraphicalObject = OneAndOnlyOne( # pyright: ignore[reportAssignmentType]
38
+ "a:graphic"
39
+ )
40
+
41
+ @classmethod
42
+ def new(
43
+ cls,
44
+ cx: Length,
45
+ cy: Length,
46
+ shape_id: int,
47
+ pic: CT_Picture,
48
+ pos_x: int | Length,
49
+ pos_y: int | Length,
50
+ ) -> CT_Anchor:
51
+ """Return a new ``<wp:anchor>`` element populated with the values passed
52
+ as parameters.
53
+ """
54
+ anchor = cast(CT_Anchor, parse_xml(cls._anchor_xml(pos_x, pos_y)))
55
+ anchor.extent.cx = cx
56
+ anchor.extent.cy = cy
57
+ anchor.docPr.id = shape_id
58
+ anchor.docPr.name = "Picture %d" % shape_id
59
+ anchor.graphic.graphicData.uri = (
60
+ "http://schemas.openxmlformats.org/drawingml/2006/picture"
61
+ )
62
+ anchor.graphic.graphicData._insert_pic(pic)
63
+ return anchor
64
+
65
+ @classmethod
66
+ def new_pic_anchor(
67
+ cls,
68
+ shape_id: int,
69
+ rId: str,
70
+ filename: str,
71
+ cx: Length,
72
+ cy: Length,
73
+ pos_x: int | Length,
74
+ pos_y: int | Length,
75
+ ) -> CT_Anchor:
76
+ """Return a new `wp:anchor` element containing the `pic:pic` element
77
+ specified by the argument values.
78
+ """
79
+ pic_id = 0 # Word doesn't seem to use this, but does not omit it
80
+ pic = CT_Picture.new(pic_id, filename, rId, cx, cy)
81
+ anchor = cls.new(cx, cy, shape_id, pic, pos_x, pos_y)
82
+ return anchor
83
+
84
+ @classmethod
85
+ def _anchor_xml(cls, pos_x: int | Length, pos_y: int | Length):
86
+ return (
87
+ '<wp:anchor distT="0" distB="0" distL="0" distR="0" simplePos="0" relativeHeight="0" \n'
88
+ ' behindDoc="1" locked="0" layoutInCell="1" allowOverlap="1" \n'
89
+ " %s>\n"
90
+ ' <wp:simplePos x="0" y="0"/>\n'
91
+ ' <wp:positionH relativeFrom="page">\n'
92
+ " <wp:posOffset>%d</wp:posOffset>\n"
93
+ " </wp:positionH>\n"
94
+ ' <wp:positionV relativeFrom="page">\n'
95
+ " <wp:posOffset>%d</wp:posOffset>\n"
96
+ " </wp:positionV>\n"
97
+ ' <wp:extent cx="914400" cy="914400"/>\n'
98
+ " <wp:wrapNone/>\n"
99
+ ' <wp:docPr id="666" name="unnamed"/>\n'
100
+ " <wp:cNvGraphicFramePr>\n"
101
+ ' <a:graphicFrameLocks noChangeAspect="1"/>\n'
102
+ " </wp:cNvGraphicFramePr>\n"
103
+ " <a:graphic>\n"
104
+ ' <a:graphicData uri="URI not set"/>\n'
105
+ " </a:graphic>\n"
106
+ "</wp:anchor>" % (nsdecls("wp", "a", "pic", "r"), int(pos_x), int(pos_y))
107
+ )
108
+
109
+
110
+ class CT_Blip(BaseOxmlElement):
111
+ """``<a:blip>`` element, specifies image source and adjustments such as alpha and
112
+ tint."""
113
+
114
+ embed: str | None = OptionalAttribute( # pyright: ignore[reportAssignmentType]
115
+ "r:embed", ST_RelationshipId
116
+ )
117
+ link: str | None = OptionalAttribute( # pyright: ignore[reportAssignmentType]
118
+ "r:link", ST_RelationshipId
119
+ )
120
+ extLst: CT_ExtensionList | None = ZeroOrOne("a:extLst")
121
+
122
+
123
+ class CT_BlipFillProperties(BaseOxmlElement):
124
+ """``<pic:blipFill>`` element, specifies picture properties."""
125
+
126
+ blip: CT_Blip = ZeroOrOne( # pyright: ignore[reportAssignmentType]
127
+ "a:blip", successors=("a:srcRect", "a:tile", "a:stretch")
128
+ )
129
+
130
+
131
+ class CT_GraphicalObject(BaseOxmlElement):
132
+ """``<a:graphic>`` element, container for a DrawingML object."""
133
+
134
+ graphicData: CT_GraphicalObjectData = OneAndOnlyOne( # pyright: ignore[reportAssignmentType]
135
+ "a:graphicData"
136
+ )
137
+
138
+
139
+ class CT_GraphicalObjectData(BaseOxmlElement):
140
+ """``<a:graphicData>`` element, container for the XML of a DrawingML object."""
141
+
142
+ pic: CT_Picture = ZeroOrOne("pic:pic") # pyright: ignore[reportAssignmentType]
143
+ uri: str = RequiredAttribute("uri", XsdToken) # pyright: ignore[reportAssignmentType]
144
+
145
+
146
+ class CT_Inline(BaseOxmlElement):
147
+ """`<wp:inline>` element, container for an inline shape."""
148
+
149
+ extent: CT_PositiveSize2D = OneAndOnlyOne("wp:extent") # pyright: ignore[reportAssignmentType]
150
+ docPr: CT_NonVisualDrawingProps = OneAndOnlyOne( # pyright: ignore[reportAssignmentType]
151
+ "wp:docPr"
152
+ )
153
+ graphic: CT_GraphicalObject = OneAndOnlyOne( # pyright: ignore[reportAssignmentType]
154
+ "a:graphic"
155
+ )
156
+
157
+ @classmethod
158
+ def new(cls, cx: Length, cy: Length, shape_id: int, pic: CT_Picture) -> CT_Inline:
159
+ """Return a new ``<wp:inline>`` element populated with the values passed as
160
+ parameters."""
161
+ inline = cast(CT_Inline, parse_xml(cls._inline_xml()))
162
+ inline.extent.cx = cx
163
+ inline.extent.cy = cy
164
+ inline.docPr.id = shape_id
165
+ inline.docPr.name = "Picture %d" % shape_id
166
+ inline.graphic.graphicData.uri = "http://schemas.openxmlformats.org/drawingml/2006/picture"
167
+ inline.graphic.graphicData._insert_pic(pic)
168
+ return inline
169
+
170
+ @classmethod
171
+ def new_pic_inline(
172
+ cls, shape_id: int, rId: str, filename: str, cx: Length, cy: Length
173
+ ) -> CT_Inline:
174
+ """Create `wp:inline` element containing a `pic:pic` element.
175
+
176
+ The contents of the `pic:pic` element is taken from the argument values.
177
+ """
178
+ pic_id = 0 # Word doesn't seem to use this, but does not omit it
179
+ pic = CT_Picture.new(pic_id, filename, rId, cx, cy)
180
+ inline = cls.new(cx, cy, shape_id, pic)
181
+ return inline
182
+
183
+ @classmethod
184
+ def _inline_xml(cls):
185
+ return (
186
+ "<wp:inline %s>\n"
187
+ ' <wp:extent cx="914400" cy="914400"/>\n'
188
+ ' <wp:docPr id="666" name="unnamed"/>\n'
189
+ " <wp:cNvGraphicFramePr>\n"
190
+ ' <a:graphicFrameLocks noChangeAspect="1"/>\n'
191
+ " </wp:cNvGraphicFramePr>\n"
192
+ " <a:graphic>\n"
193
+ ' <a:graphicData uri="URI not set"/>\n'
194
+ " </a:graphic>\n"
195
+ "</wp:inline>" % nsdecls("wp", "a", "pic", "r")
196
+ )
197
+
198
+
199
+ class CT_Extension(BaseOxmlElement):
200
+ """
201
+ `<a:ext>` element in `extLst`, which can hold a `svgBlip` element.
202
+ """
203
+
204
+ svgBlip = ZeroOrOne("asvg:svgBlip")
205
+
206
+
207
+ class CT_NonVisualDrawingProps(BaseOxmlElement):
208
+ """Used for ``<wp:docPr>`` element, and perhaps others.
209
+
210
+ Specifies the id and name of a DrawingML drawing.
211
+ """
212
+
213
+ id = RequiredAttribute("id", ST_DrawingElementId)
214
+ name = RequiredAttribute("name", XsdString)
215
+
216
+
217
+ class CT_NonVisualPictureProperties(BaseOxmlElement):
218
+ """``<pic:cNvPicPr>`` element, specifies picture locking and resize behaviors."""
219
+
220
+
221
+ class CT_Picture(BaseOxmlElement):
222
+ """``<pic:pic>`` element, a DrawingML picture."""
223
+
224
+ nvPicPr: CT_PictureNonVisual = OneAndOnlyOne( # pyright: ignore[reportAssignmentType]
225
+ "pic:nvPicPr"
226
+ )
227
+ blipFill: CT_BlipFillProperties = OneAndOnlyOne( # pyright: ignore[reportAssignmentType]
228
+ "pic:blipFill"
229
+ )
230
+ spPr: CT_ShapeProperties = OneAndOnlyOne("pic:spPr") # pyright: ignore[reportAssignmentType]
231
+
232
+ @classmethod
233
+ def new(cls, pic_id: int, filename: str, rId: str, cx: Length, cy: Length) -> CT_Picture:
234
+ """A new minimum viable `<pic:pic>` (picture) element."""
235
+ if filename.endswith(".svg"):
236
+ pic = cast(CT_Picture, parse_xml(cls._pic_xml_svg()))
237
+ blip = pic.blipFill.blip
238
+ assert blip is not None
239
+ blip.extLst.ext.svgBlip.embed = rId
240
+ else:
241
+ pic = cast(CT_Picture, parse_xml(cls._pic_xml()))
242
+ blip = pic.blipFill.blip
243
+ assert blip is not None
244
+ blip.embed = rId
245
+ pic.nvPicPr.cNvPr.id = pic_id
246
+ pic.nvPicPr.cNvPr.name = filename
247
+ pic.spPr.cx = cx
248
+ pic.spPr.cy = cy
249
+ return pic
250
+
251
+ @classmethod
252
+ def _pic_xml_svg(cls):
253
+ return (
254
+ "<pic:pic %s>\n"
255
+ " <pic:nvPicPr>\n"
256
+ ' <pic:cNvPr id="666" name="unnamed"/>\n'
257
+ " <pic:cNvPicPr/>\n"
258
+ " </pic:nvPicPr>\n"
259
+ " <pic:blipFill>\n"
260
+ " <a:blip>\n"
261
+ " <a:extLst>\n"
262
+ ' <a:ext uri="{96DAC541-7B7A-43D3-8B79-37D633B846F1}">\n'
263
+ " <asvg:svgBlip/>\n"
264
+ " </a:ext>\n"
265
+ " </a:extLst>\n"
266
+ " </a:blip>\n"
267
+ " <a:stretch>\n"
268
+ " <a:fillRect/>\n"
269
+ " </a:stretch>\n"
270
+ " </pic:blipFill>\n"
271
+ " <pic:spPr>\n"
272
+ " <a:xfrm>\n"
273
+ ' <a:off x="0" y="0"/>\n'
274
+ ' <a:ext cx="914400" cy="914400"/>\n'
275
+ " </a:xfrm>\n"
276
+ ' <a:prstGeom prst="rect"/>\n'
277
+ " </pic:spPr>\n"
278
+ "</pic:pic>" % nsdecls("pic", "a", "r", "asvg")
279
+ )
280
+
281
+ @classmethod
282
+ def _pic_xml(cls):
283
+ return (
284
+ "<pic:pic %s>\n"
285
+ " <pic:nvPicPr>\n"
286
+ ' <pic:cNvPr id="666" name="unnamed"/>\n'
287
+ " <pic:cNvPicPr/>\n"
288
+ " </pic:nvPicPr>\n"
289
+ " <pic:blipFill>\n"
290
+ " <a:blip/>\n"
291
+ " <a:stretch>\n"
292
+ " <a:fillRect/>\n"
293
+ " </a:stretch>\n"
294
+ " </pic:blipFill>\n"
295
+ " <pic:spPr>\n"
296
+ " <a:xfrm>\n"
297
+ ' <a:off x="0" y="0"/>\n'
298
+ ' <a:ext cx="914400" cy="914400"/>\n'
299
+ " </a:xfrm>\n"
300
+ ' <a:prstGeom prst="rect"/>\n'
301
+ " </pic:spPr>\n"
302
+ "</pic:pic>" % nsdecls("pic", "a", "r", "asvg")
303
+ )
304
+
305
+
306
+ class CT_PictureNonVisual(BaseOxmlElement):
307
+ """``<pic:nvPicPr>`` element, non-visual picture properties."""
308
+
309
+ cNvPr = OneAndOnlyOne("pic:cNvPr")
310
+
311
+
312
+ class CT_Point2D(BaseOxmlElement):
313
+ """Used for ``<a:off>`` element, and perhaps others.
314
+
315
+ Specifies an x, y coordinate (point).
316
+ """
317
+
318
+ x = RequiredAttribute("x", ST_Coordinate)
319
+ y = RequiredAttribute("y", ST_Coordinate)
320
+
321
+
322
+ class CT_PositiveSize2D(BaseOxmlElement):
323
+ """Used for ``<wp:extent>`` element, and perhaps others later.
324
+
325
+ Specifies the size of a DrawingML drawing.
326
+ """
327
+
328
+ cx: Length = RequiredAttribute( # pyright: ignore[reportAssignmentType]
329
+ "cx", ST_PositiveCoordinate
330
+ )
331
+ cy: Length = RequiredAttribute( # pyright: ignore[reportAssignmentType]
332
+ "cy", ST_PositiveCoordinate
333
+ )
334
+
335
+
336
+ class CT_PresetGeometry2D(BaseOxmlElement):
337
+ """``<a:prstGeom>`` element, specifies an preset autoshape geometry, such as
338
+ ``rect``."""
339
+
340
+
341
+ class CT_RelativeRect(BaseOxmlElement):
342
+ """``<a:fillRect>`` element, specifying picture should fill containing rectangle
343
+ shape."""
344
+
345
+
346
+ class CT_ShapeProperties(BaseOxmlElement):
347
+ """``<pic:spPr>`` element, specifies size and shape of picture container."""
348
+
349
+ xfrm = ZeroOrOne(
350
+ "a:xfrm",
351
+ successors=(
352
+ "a:custGeom",
353
+ "a:prstGeom",
354
+ "a:ln",
355
+ "a:effectLst",
356
+ "a:effectDag",
357
+ "a:scene3d",
358
+ "a:sp3d",
359
+ "a:extLst",
360
+ ),
361
+ )
362
+
363
+ @property
364
+ def cx(self):
365
+ """Shape width as an instance of Emu, or None if not present."""
366
+ xfrm = self.xfrm
367
+ if xfrm is None:
368
+ return None
369
+ return xfrm.cx
370
+
371
+ @cx.setter
372
+ def cx(self, value):
373
+ xfrm = self.get_or_add_xfrm()
374
+ xfrm.cx = value
375
+
376
+ @property
377
+ def cy(self):
378
+ """Shape height as an instance of Emu, or None if not present."""
379
+ xfrm = self.xfrm
380
+ if xfrm is None:
381
+ return None
382
+ return xfrm.cy
383
+
384
+ @cy.setter
385
+ def cy(self, value):
386
+ xfrm = self.get_or_add_xfrm()
387
+ xfrm.cy = value
388
+
389
+
390
+ class CT_StretchInfoProperties(BaseOxmlElement):
391
+ """``<a:stretch>`` element, specifies how picture should fill its containing
392
+ shape."""
393
+
394
+
395
+ class CT_Transform2D(BaseOxmlElement):
396
+ """``<a:xfrm>`` element, specifies size and shape of picture container."""
397
+
398
+ off = ZeroOrOne("a:off", successors=("a:ext",))
399
+ ext = ZeroOrOne("a:ext", successors=())
400
+ embed: str | None = OptionalAttribute( # pyright: ignore[reportAssignmentType]
401
+ "r:embed", ST_RelationshipId
402
+ )
403
+
404
+ @property
405
+ def cx(self):
406
+ ext = self.ext
407
+ if ext is None:
408
+ return None
409
+ return ext.cx
410
+
411
+ @cx.setter
412
+ def cx(self, value):
413
+ ext = self.get_or_add_ext()
414
+ ext.cx = value
415
+
416
+ @property
417
+ def cy(self):
418
+ ext = self.ext
419
+ if ext is None:
420
+ return None
421
+ return ext.cy
422
+
423
+ @cy.setter
424
+ def cy(self, value):
425
+ ext = self.get_or_add_ext()
426
+ ext.cy = value
docx/oxml/shared.py ADDED
@@ -0,0 +1,52 @@
1
+ """Objects shared by modules in the docx.oxml subpackage."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from typing import cast
6
+
7
+ from docx.oxml.ns import qn
8
+ from docx.oxml.parser import OxmlElement
9
+ from docx.oxml.simpletypes import ST_DecimalNumber, ST_OnOff, ST_String
10
+ from docx.oxml.xmlchemy import BaseOxmlElement, OptionalAttribute, RequiredAttribute
11
+
12
+
13
+ class CT_DecimalNumber(BaseOxmlElement):
14
+ """Used for ``<w:numId>``, ``<w:ilvl>``, ``<w:abstractNumId>`` and several others,
15
+ containing a text representation of a decimal number (e.g. 42) in its ``val``
16
+ attribute."""
17
+
18
+ val: int = RequiredAttribute("w:val", ST_DecimalNumber) # pyright: ignore[reportAssignmentType]
19
+
20
+ @classmethod
21
+ def new(cls, nsptagname: str, val: int):
22
+ """Return a new ``CT_DecimalNumber`` element having tagname `nsptagname` and
23
+ ``val`` attribute set to `val`."""
24
+ return OxmlElement(nsptagname, attrs={qn("w:val"): str(val)})
25
+
26
+
27
+ class CT_OnOff(BaseOxmlElement):
28
+ """Used for `w:b`, `w:i` elements and others.
29
+
30
+ Contains a bool-ish string in its `val` attribute, xsd:boolean plus "on" and
31
+ "off". Defaults to `True`, so `<w:b>` for example means "bold is turned on".
32
+ """
33
+
34
+ val: bool = OptionalAttribute( # pyright: ignore[reportAssignmentType]
35
+ "w:val", ST_OnOff, default=True
36
+ )
37
+
38
+
39
+ class CT_String(BaseOxmlElement):
40
+ """Used for `w:pStyle` and `w:tblStyle` elements and others.
41
+
42
+ In those cases, it containing a style name in its `val` attribute.
43
+ """
44
+
45
+ val: str = RequiredAttribute("w:val", ST_String) # pyright: ignore[reportAssignmentType]
46
+
47
+ @classmethod
48
+ def new(cls, nsptagname: str, val: str):
49
+ """A new `CT_String`` element with tagname `nsptagname` and `val` attribute set to `val`."""
50
+ elm = cast(CT_String, OxmlElement(nsptagname))
51
+ elm.val = val
52
+ return elm