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/styles.py ADDED
@@ -0,0 +1,320 @@
1
+ """Custom element classes related to the styles part."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from docx.enum.style import WD_STYLE_TYPE
6
+ from docx.oxml.simpletypes import ST_DecimalNumber, ST_OnOff, ST_String
7
+ from docx.oxml.xmlchemy import (
8
+ BaseOxmlElement,
9
+ OptionalAttribute,
10
+ RequiredAttribute,
11
+ ZeroOrMore,
12
+ ZeroOrOne,
13
+ )
14
+
15
+
16
+ def styleId_from_name(name):
17
+ """Return the style id corresponding to `name`, taking into account special-case
18
+ names such as 'Heading 1'."""
19
+ return {
20
+ "caption": "Caption",
21
+ "heading 1": "Heading1",
22
+ "heading 2": "Heading2",
23
+ "heading 3": "Heading3",
24
+ "heading 4": "Heading4",
25
+ "heading 5": "Heading5",
26
+ "heading 6": "Heading6",
27
+ "heading 7": "Heading7",
28
+ "heading 8": "Heading8",
29
+ "heading 9": "Heading9",
30
+ }.get(name, name.replace(" ", ""))
31
+
32
+
33
+ class CT_LatentStyles(BaseOxmlElement):
34
+ """`w:latentStyles` element, defining behavior defaults for latent styles and
35
+ containing `w:lsdException` child elements that each override those defaults for a
36
+ named latent style."""
37
+
38
+ lsdException = ZeroOrMore("w:lsdException", successors=())
39
+
40
+ count = OptionalAttribute("w:count", ST_DecimalNumber)
41
+ defLockedState = OptionalAttribute("w:defLockedState", ST_OnOff)
42
+ defQFormat = OptionalAttribute("w:defQFormat", ST_OnOff)
43
+ defSemiHidden = OptionalAttribute("w:defSemiHidden", ST_OnOff)
44
+ defUIPriority = OptionalAttribute("w:defUIPriority", ST_DecimalNumber)
45
+ defUnhideWhenUsed = OptionalAttribute("w:defUnhideWhenUsed", ST_OnOff)
46
+
47
+ def bool_prop(self, attr_name):
48
+ """Return the boolean value of the attribute having `attr_name`, or |False| if
49
+ not present."""
50
+ value = getattr(self, attr_name)
51
+ if value is None:
52
+ return False
53
+ return value
54
+
55
+ def get_by_name(self, name):
56
+ """Return the `w:lsdException` child having `name`, or |None| if not found."""
57
+ found = self.xpath('w:lsdException[@w:name="%s"]' % name)
58
+ if not found:
59
+ return None
60
+ return found[0]
61
+
62
+ def set_bool_prop(self, attr_name, value):
63
+ """Set the on/off attribute having `attr_name` to `value`."""
64
+ setattr(self, attr_name, bool(value))
65
+
66
+
67
+ class CT_LsdException(BaseOxmlElement):
68
+ """``<w:lsdException>`` element, defining override visibility behaviors for a named
69
+ latent style."""
70
+
71
+ locked = OptionalAttribute("w:locked", ST_OnOff)
72
+ name = RequiredAttribute("w:name", ST_String)
73
+ qFormat = OptionalAttribute("w:qFormat", ST_OnOff)
74
+ semiHidden = OptionalAttribute("w:semiHidden", ST_OnOff)
75
+ uiPriority = OptionalAttribute("w:uiPriority", ST_DecimalNumber)
76
+ unhideWhenUsed = OptionalAttribute("w:unhideWhenUsed", ST_OnOff)
77
+
78
+ def delete(self):
79
+ """Remove this `w:lsdException` element from the XML document."""
80
+ self.getparent().remove(self)
81
+
82
+ def on_off_prop(self, attr_name):
83
+ """Return the boolean value of the attribute having `attr_name`, or |None| if
84
+ not present."""
85
+ return getattr(self, attr_name)
86
+
87
+ def set_on_off_prop(self, attr_name, value):
88
+ """Set the on/off attribute having `attr_name` to `value`."""
89
+ setattr(self, attr_name, value)
90
+
91
+
92
+ class CT_Style(BaseOxmlElement):
93
+ """A ``<w:style>`` element, representing a style definition."""
94
+
95
+ _tag_seq = (
96
+ "w:name",
97
+ "w:aliases",
98
+ "w:basedOn",
99
+ "w:next",
100
+ "w:link",
101
+ "w:autoRedefine",
102
+ "w:hidden",
103
+ "w:uiPriority",
104
+ "w:semiHidden",
105
+ "w:unhideWhenUsed",
106
+ "w:qFormat",
107
+ "w:locked",
108
+ "w:personal",
109
+ "w:personalCompose",
110
+ "w:personalReply",
111
+ "w:rsid",
112
+ "w:pPr",
113
+ "w:rPr",
114
+ "w:tblPr",
115
+ "w:trPr",
116
+ "w:tcPr",
117
+ "w:tblStylePr",
118
+ )
119
+ name = ZeroOrOne("w:name", successors=_tag_seq[1:])
120
+ basedOn = ZeroOrOne("w:basedOn", successors=_tag_seq[3:])
121
+ next = ZeroOrOne("w:next", successors=_tag_seq[4:])
122
+ uiPriority = ZeroOrOne("w:uiPriority", successors=_tag_seq[8:])
123
+ semiHidden = ZeroOrOne("w:semiHidden", successors=_tag_seq[9:])
124
+ unhideWhenUsed = ZeroOrOne("w:unhideWhenUsed", successors=_tag_seq[10:])
125
+ qFormat = ZeroOrOne("w:qFormat", successors=_tag_seq[11:])
126
+ locked = ZeroOrOne("w:locked", successors=_tag_seq[12:])
127
+ pPr = ZeroOrOne("w:pPr", successors=_tag_seq[17:])
128
+ rPr = ZeroOrOne("w:rPr", successors=_tag_seq[18:])
129
+ del _tag_seq
130
+
131
+ type: WD_STYLE_TYPE | None = OptionalAttribute( # pyright: ignore[reportAssignmentType]
132
+ "w:type", WD_STYLE_TYPE
133
+ )
134
+ styleId: str | None = OptionalAttribute( # pyright: ignore[reportAssignmentType]
135
+ "w:styleId", ST_String
136
+ )
137
+ default = OptionalAttribute("w:default", ST_OnOff)
138
+ customStyle = OptionalAttribute("w:customStyle", ST_OnOff)
139
+
140
+ @property
141
+ def basedOn_val(self):
142
+ """Value of `w:basedOn/@w:val` or |None| if not present."""
143
+ basedOn = self.basedOn
144
+ if basedOn is None:
145
+ return None
146
+ return basedOn.val
147
+
148
+ @basedOn_val.setter
149
+ def basedOn_val(self, value):
150
+ if value is None:
151
+ self._remove_basedOn()
152
+ else:
153
+ self.get_or_add_basedOn().val = value
154
+
155
+ @property
156
+ def base_style(self):
157
+ """Sibling CT_Style element this style is based on or |None| if no base style or
158
+ base style not found."""
159
+ basedOn = self.basedOn
160
+ if basedOn is None:
161
+ return None
162
+ styles = self.getparent()
163
+ base_style = styles.get_by_id(basedOn.val)
164
+ if base_style is None:
165
+ return None
166
+ return base_style
167
+
168
+ def delete(self):
169
+ """Remove this `w:style` element from its parent `w:styles` element."""
170
+ self.getparent().remove(self)
171
+
172
+ @property
173
+ def locked_val(self):
174
+ """Value of `w:locked/@w:val` or |False| if not present."""
175
+ locked = self.locked
176
+ if locked is None:
177
+ return False
178
+ return locked.val
179
+
180
+ @locked_val.setter
181
+ def locked_val(self, value):
182
+ self._remove_locked()
183
+ if bool(value) is True:
184
+ locked = self._add_locked()
185
+ locked.val = value
186
+
187
+ @property
188
+ def name_val(self):
189
+ """Value of ``<w:name>`` child or |None| if not present."""
190
+ name = self.name
191
+ if name is None:
192
+ return None
193
+ return name.val
194
+
195
+ @name_val.setter
196
+ def name_val(self, value):
197
+ self._remove_name()
198
+ if value is not None:
199
+ name = self._add_name()
200
+ name.val = value
201
+
202
+ @property
203
+ def next_style(self):
204
+ """Sibling CT_Style element identified by the value of `w:name/@w:val` or |None|
205
+ if no value is present or no style with that style id is found."""
206
+ next = self.next
207
+ if next is None:
208
+ return None
209
+ styles = self.getparent()
210
+ return styles.get_by_id(next.val) # None if not found
211
+
212
+ @property
213
+ def qFormat_val(self):
214
+ """Value of `w:qFormat/@w:val` or |False| if not present."""
215
+ qFormat = self.qFormat
216
+ if qFormat is None:
217
+ return False
218
+ return qFormat.val
219
+
220
+ @qFormat_val.setter
221
+ def qFormat_val(self, value):
222
+ self._remove_qFormat()
223
+ if bool(value):
224
+ self._add_qFormat()
225
+
226
+ @property
227
+ def semiHidden_val(self):
228
+ """Value of ``<w:semiHidden>`` child or |False| if not present."""
229
+ semiHidden = self.semiHidden
230
+ if semiHidden is None:
231
+ return False
232
+ return semiHidden.val
233
+
234
+ @semiHidden_val.setter
235
+ def semiHidden_val(self, value):
236
+ self._remove_semiHidden()
237
+ if bool(value) is True:
238
+ semiHidden = self._add_semiHidden()
239
+ semiHidden.val = value
240
+
241
+ @property
242
+ def uiPriority_val(self):
243
+ """Value of ``<w:uiPriority>`` child or |None| if not present."""
244
+ uiPriority = self.uiPriority
245
+ if uiPriority is None:
246
+ return None
247
+ return uiPriority.val
248
+
249
+ @uiPriority_val.setter
250
+ def uiPriority_val(self, value):
251
+ self._remove_uiPriority()
252
+ if value is not None:
253
+ uiPriority = self._add_uiPriority()
254
+ uiPriority.val = value
255
+
256
+ @property
257
+ def unhideWhenUsed_val(self):
258
+ """Value of `w:unhideWhenUsed/@w:val` or |False| if not present."""
259
+ unhideWhenUsed = self.unhideWhenUsed
260
+ if unhideWhenUsed is None:
261
+ return False
262
+ return unhideWhenUsed.val
263
+
264
+ @unhideWhenUsed_val.setter
265
+ def unhideWhenUsed_val(self, value):
266
+ self._remove_unhideWhenUsed()
267
+ if bool(value) is True:
268
+ unhideWhenUsed = self._add_unhideWhenUsed()
269
+ unhideWhenUsed.val = value
270
+
271
+
272
+ class CT_Styles(BaseOxmlElement):
273
+ """``<w:styles>`` element, the root element of a styles part, i.e. styles.xml."""
274
+
275
+ _tag_seq = ("w:docDefaults", "w:latentStyles", "w:style")
276
+ latentStyles = ZeroOrOne("w:latentStyles", successors=_tag_seq[2:])
277
+ style = ZeroOrMore("w:style", successors=())
278
+ del _tag_seq
279
+
280
+ def add_style_of_type(self, name, style_type, builtin):
281
+ """Return a newly added `w:style` element having `name` and `style_type`.
282
+
283
+ `w:style/@customStyle` is set based on the value of `builtin`.
284
+ """
285
+ style = self.add_style()
286
+ style.type = style_type
287
+ style.customStyle = None if builtin else True
288
+ style.styleId = styleId_from_name(name)
289
+ style.name_val = name
290
+ return style
291
+
292
+ def default_for(self, style_type):
293
+ """Return `w:style[@w:type="*{style_type}*][-1]` or |None| if not found."""
294
+ default_styles_for_type = [
295
+ s for s in self._iter_styles() if s.type == style_type and s.default
296
+ ]
297
+ if not default_styles_for_type:
298
+ return None
299
+ # spec calls for last default in document order
300
+ return default_styles_for_type[-1]
301
+
302
+ def get_by_id(self, styleId: str) -> CT_Style | None:
303
+ """`w:style` child where @styleId = `styleId`.
304
+
305
+ |None| if not found.
306
+ """
307
+ xpath = f'w:style[@w:styleId="{styleId}"]'
308
+ return next(iter(self.xpath(xpath)), None)
309
+
310
+ def get_by_name(self, name: str) -> CT_Style | None:
311
+ """`w:style` child with `w:name` grandchild having value `name`.
312
+
313
+ |None| if not found.
314
+ """
315
+ xpath = 'w:style[w:name/@w:val="%s"]' % name
316
+ return next(iter(self.xpath(xpath)), None)
317
+
318
+ def _iter_styles(self):
319
+ """Generate each of the `w:style` child elements in document order."""
320
+ return (style for style in self.xpath("w:style"))