ps-python-docx 1.3.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 (126) hide show
  1. docx/__init__.py +68 -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 +275 -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 +23 -0
  19. docx/image/bmp.py +43 -0
  20. docx/image/constants.py +172 -0
  21. docx/image/exceptions.py +13 -0
  22. docx/image/gif.py +38 -0
  23. docx/image/helpers.py +86 -0
  24. docx/image/image.py +234 -0
  25. docx/image/jpeg.py +425 -0
  26. docx/image/png.py +253 -0
  27. docx/image/tiff.py +289 -0
  28. docx/opc/__init__.py +0 -0
  29. docx/opc/constants.py +306 -0
  30. docx/opc/coreprops.py +142 -0
  31. docx/opc/exceptions.py +12 -0
  32. docx/opc/oxml.py +247 -0
  33. docx/opc/package.py +219 -0
  34. docx/opc/packuri.py +109 -0
  35. docx/opc/part.py +247 -0
  36. docx/opc/parts/__init__.py +0 -0
  37. docx/opc/parts/coreprops.py +48 -0
  38. docx/opc/phys_pkg.py +119 -0
  39. docx/opc/pkgreader.py +254 -0
  40. docx/opc/pkgwriter.py +115 -0
  41. docx/opc/rel.py +153 -0
  42. docx/opc/shared.py +31 -0
  43. docx/opc/spec.py +24 -0
  44. docx/oxml/__init__.py +261 -0
  45. docx/oxml/comments.py +124 -0
  46. docx/oxml/coreprops.py +298 -0
  47. docx/oxml/document.py +88 -0
  48. docx/oxml/drawing.py +11 -0
  49. docx/oxml/exceptions.py +10 -0
  50. docx/oxml/ns.py +109 -0
  51. docx/oxml/numbering.py +109 -0
  52. docx/oxml/parser.py +62 -0
  53. docx/oxml/section.py +537 -0
  54. docx/oxml/settings.py +138 -0
  55. docx/oxml/shape.py +299 -0
  56. docx/oxml/shared.py +52 -0
  57. docx/oxml/simpletypes.py +434 -0
  58. docx/oxml/styles.py +341 -0
  59. docx/oxml/table.py +977 -0
  60. docx/oxml/text/__init__.py +0 -0
  61. docx/oxml/text/font.py +333 -0
  62. docx/oxml/text/hyperlink.py +45 -0
  63. docx/oxml/text/pagebreak.py +278 -0
  64. docx/oxml/text/paragraph.py +106 -0
  65. docx/oxml/text/parfmt.py +392 -0
  66. docx/oxml/text/run.py +307 -0
  67. docx/oxml/xmlchemy.py +696 -0
  68. docx/package.py +110 -0
  69. docx/parts/__init__.py +0 -0
  70. docx/parts/comments.py +51 -0
  71. docx/parts/document.py +182 -0
  72. docx/parts/hdrftr.py +53 -0
  73. docx/parts/image.py +80 -0
  74. docx/parts/numbering.py +32 -0
  75. docx/parts/settings.py +50 -0
  76. docx/parts/story.py +95 -0
  77. docx/parts/styles.py +42 -0
  78. docx/parts/theme.py +53 -0
  79. docx/py.typed +0 -0
  80. docx/section.py +479 -0
  81. docx/settings.py +35 -0
  82. docx/shape.py +103 -0
  83. docx/shared.py +382 -0
  84. docx/styles/__init__.py +40 -0
  85. docx/styles/latent.py +198 -0
  86. docx/styles/style.py +264 -0
  87. docx/styles/styles.py +147 -0
  88. docx/table.py +537 -0
  89. docx/templates/default-comments.xml +12 -0
  90. docx/templates/default-docx-template/[Content_Types].xml +17 -0
  91. docx/templates/default-docx-template/_rels/.rels +7 -0
  92. docx/templates/default-docx-template/customXml/_rels/item1.xml.rels +4 -0
  93. docx/templates/default-docx-template/customXml/item1.xml +2 -0
  94. docx/templates/default-docx-template/customXml/itemProps1.xml +6 -0
  95. docx/templates/default-docx-template/docProps/app.xml +36 -0
  96. docx/templates/default-docx-template/docProps/core.xml +13 -0
  97. docx/templates/default-docx-template/docProps/thumbnail.jpeg +0 -0
  98. docx/templates/default-docx-template/word/_rels/document.xml.rels +11 -0
  99. docx/templates/default-docx-template/word/document.xml +11 -0
  100. docx/templates/default-docx-template/word/fontTable.xml +61 -0
  101. docx/templates/default-docx-template/word/numbering.xml +201 -0
  102. docx/templates/default-docx-template/word/settings.xml +53 -0
  103. docx/templates/default-docx-template/word/styles.xml +11844 -0
  104. docx/templates/default-docx-template/word/stylesWithEffects.xml +11800 -0
  105. docx/templates/default-docx-template/word/theme/theme1.xml +318 -0
  106. docx/templates/default-docx-template/word/webSettings.xml +5 -0
  107. docx/templates/default-footer.xml +27 -0
  108. docx/templates/default-header.xml +27 -0
  109. docx/templates/default-settings.xml +26 -0
  110. docx/templates/default-styles.xml +190 -0
  111. docx/templates/default.docx +0 -0
  112. docx/text/__init__.py +0 -0
  113. docx/text/font.py +472 -0
  114. docx/text/hyperlink.py +121 -0
  115. docx/text/pagebreak.py +104 -0
  116. docx/text/paragraph.py +173 -0
  117. docx/text/parfmt.py +286 -0
  118. docx/text/run.py +257 -0
  119. docx/text/tabstops.py +123 -0
  120. docx/theme.py +67 -0
  121. docx/types.py +34 -0
  122. ps_python_docx-1.3.0.dist-info/METADATA +77 -0
  123. ps_python_docx-1.3.0.dist-info/RECORD +126 -0
  124. ps_python_docx-1.3.0.dist-info/WHEEL +5 -0
  125. ps_python_docx-1.3.0.dist-info/licenses/LICENSE +20 -0
  126. ps_python_docx-1.3.0.dist-info/top_level.txt +1 -0
docx/oxml/styles.py ADDED
@@ -0,0 +1,341 @@
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_DocDefaults(BaseOxmlElement):
34
+ """Document-wide defaults for paragraph and character formatting."""
35
+
36
+ rPrDefault = ZeroOrOne("w:rPrDefault", successors=("w:pPrDefault",))
37
+
38
+
39
+ class CT_RPrDefault(BaseOxmlElement):
40
+ """Container for default run properties."""
41
+
42
+ rPr = ZeroOrOne("w:rPr", successors=())
43
+
44
+
45
+ class CT_LatentStyles(BaseOxmlElement):
46
+ """`w:latentStyles` element, defining behavior defaults for latent styles and
47
+ containing `w:lsdException` child elements that each override those defaults for a
48
+ named latent style."""
49
+
50
+ lsdException = ZeroOrMore("w:lsdException", successors=())
51
+
52
+ count = OptionalAttribute("w:count", ST_DecimalNumber)
53
+ defLockedState = OptionalAttribute("w:defLockedState", ST_OnOff)
54
+ defQFormat = OptionalAttribute("w:defQFormat", ST_OnOff)
55
+ defSemiHidden = OptionalAttribute("w:defSemiHidden", ST_OnOff)
56
+ defUIPriority = OptionalAttribute("w:defUIPriority", ST_DecimalNumber)
57
+ defUnhideWhenUsed = OptionalAttribute("w:defUnhideWhenUsed", ST_OnOff)
58
+
59
+ def bool_prop(self, attr_name):
60
+ """Return the boolean value of the attribute having `attr_name`, or |False| if
61
+ not present."""
62
+ value = getattr(self, attr_name)
63
+ if value is None:
64
+ return False
65
+ return value
66
+
67
+ def get_by_name(self, name):
68
+ """Return the `w:lsdException` child having `name`, or |None| if not found."""
69
+ found = self.xpath('w:lsdException[@w:name="%s"]' % name)
70
+ if not found:
71
+ return None
72
+ return found[0]
73
+
74
+ def set_bool_prop(self, attr_name, value):
75
+ """Set the on/off attribute having `attr_name` to `value`."""
76
+ setattr(self, attr_name, bool(value))
77
+
78
+
79
+ class CT_LsdException(BaseOxmlElement):
80
+ """``<w:lsdException>`` element, defining override visibility behaviors for a named
81
+ latent style."""
82
+
83
+ locked = OptionalAttribute("w:locked", ST_OnOff)
84
+ name = RequiredAttribute("w:name", ST_String)
85
+ qFormat = OptionalAttribute("w:qFormat", ST_OnOff)
86
+ semiHidden = OptionalAttribute("w:semiHidden", ST_OnOff)
87
+ uiPriority = OptionalAttribute("w:uiPriority", ST_DecimalNumber)
88
+ unhideWhenUsed = OptionalAttribute("w:unhideWhenUsed", ST_OnOff)
89
+
90
+ def delete(self):
91
+ """Remove this `w:lsdException` element from the XML document."""
92
+ self.getparent().remove(self)
93
+
94
+ def on_off_prop(self, attr_name):
95
+ """Return the boolean value of the attribute having `attr_name`, or |None| if
96
+ not present."""
97
+ return getattr(self, attr_name)
98
+
99
+ def set_on_off_prop(self, attr_name, value):
100
+ """Set the on/off attribute having `attr_name` to `value`."""
101
+ setattr(self, attr_name, value)
102
+
103
+
104
+ class CT_Style(BaseOxmlElement):
105
+ """A ``<w:style>`` element, representing a style definition."""
106
+
107
+ _tag_seq = (
108
+ "w:name",
109
+ "w:aliases",
110
+ "w:basedOn",
111
+ "w:next",
112
+ "w:link",
113
+ "w:autoRedefine",
114
+ "w:hidden",
115
+ "w:uiPriority",
116
+ "w:semiHidden",
117
+ "w:unhideWhenUsed",
118
+ "w:qFormat",
119
+ "w:locked",
120
+ "w:personal",
121
+ "w:personalCompose",
122
+ "w:personalReply",
123
+ "w:rsid",
124
+ "w:pPr",
125
+ "w:rPr",
126
+ "w:tblPr",
127
+ "w:trPr",
128
+ "w:tcPr",
129
+ "w:tblStylePr",
130
+ )
131
+ name = ZeroOrOne("w:name", successors=_tag_seq[1:])
132
+ basedOn = ZeroOrOne("w:basedOn", successors=_tag_seq[3:])
133
+ next = ZeroOrOne("w:next", successors=_tag_seq[4:])
134
+ link = ZeroOrOne("w:link", successors=_tag_seq[5:])
135
+ uiPriority = ZeroOrOne("w:uiPriority", successors=_tag_seq[8:])
136
+ semiHidden = ZeroOrOne("w:semiHidden", successors=_tag_seq[9:])
137
+ unhideWhenUsed = ZeroOrOne("w:unhideWhenUsed", successors=_tag_seq[10:])
138
+ qFormat = ZeroOrOne("w:qFormat", successors=_tag_seq[11:])
139
+ locked = ZeroOrOne("w:locked", successors=_tag_seq[12:])
140
+ pPr = ZeroOrOne("w:pPr", successors=_tag_seq[17:])
141
+ rPr = ZeroOrOne("w:rPr", successors=_tag_seq[18:])
142
+ del _tag_seq
143
+
144
+ type: WD_STYLE_TYPE | None = OptionalAttribute( # pyright: ignore[reportAssignmentType]
145
+ "w:type", WD_STYLE_TYPE
146
+ )
147
+ styleId: str | None = OptionalAttribute( # pyright: ignore[reportAssignmentType]
148
+ "w:styleId", ST_String
149
+ )
150
+ default = OptionalAttribute("w:default", ST_OnOff)
151
+ customStyle = OptionalAttribute("w:customStyle", ST_OnOff)
152
+
153
+ @property
154
+ def basedOn_val(self):
155
+ """Value of `w:basedOn/@w:val` or |None| if not present."""
156
+ basedOn = self.basedOn
157
+ if basedOn is None:
158
+ return None
159
+ return basedOn.val
160
+
161
+ @basedOn_val.setter
162
+ def basedOn_val(self, value):
163
+ if value is None:
164
+ self._remove_basedOn()
165
+ else:
166
+ self.get_or_add_basedOn().val = value
167
+
168
+ @property
169
+ def linked_style(self):
170
+ """Sibling style referenced by ``w:link``, or |None| for a missing target."""
171
+ if self.link is None or self.getparent() is None:
172
+ return None
173
+ return self.getparent().get_by_id(self.link.val)
174
+
175
+ @property
176
+ def base_style(self):
177
+ """Sibling CT_Style element this style is based on or |None| if no base style or
178
+ base style not found."""
179
+ basedOn = self.basedOn
180
+ if basedOn is None:
181
+ return None
182
+ styles = self.getparent()
183
+ base_style = styles.get_by_id(basedOn.val)
184
+ if base_style is None:
185
+ return None
186
+ return base_style
187
+
188
+ def delete(self):
189
+ """Remove this `w:style` element from its parent `w:styles` element."""
190
+ self.getparent().remove(self)
191
+
192
+ @property
193
+ def locked_val(self):
194
+ """Value of `w:locked/@w:val` or |False| if not present."""
195
+ locked = self.locked
196
+ if locked is None:
197
+ return False
198
+ return locked.val
199
+
200
+ @locked_val.setter
201
+ def locked_val(self, value):
202
+ self._remove_locked()
203
+ if bool(value) is True:
204
+ locked = self._add_locked()
205
+ locked.val = value
206
+
207
+ @property
208
+ def name_val(self):
209
+ """Value of ``<w:name>`` child or |None| if not present."""
210
+ name = self.name
211
+ if name is None:
212
+ return None
213
+ return name.val
214
+
215
+ @name_val.setter
216
+ def name_val(self, value):
217
+ self._remove_name()
218
+ if value is not None:
219
+ name = self._add_name()
220
+ name.val = value
221
+
222
+ @property
223
+ def next_style(self):
224
+ """Sibling CT_Style element identified by the value of `w:name/@w:val` or |None|
225
+ if no value is present or no style with that style id is found."""
226
+ next = self.next
227
+ if next is None:
228
+ return None
229
+ styles = self.getparent()
230
+ return styles.get_by_id(next.val) # None if not found
231
+
232
+ @property
233
+ def qFormat_val(self):
234
+ """Value of `w:qFormat/@w:val` or |False| if not present."""
235
+ qFormat = self.qFormat
236
+ if qFormat is None:
237
+ return False
238
+ return qFormat.val
239
+
240
+ @qFormat_val.setter
241
+ def qFormat_val(self, value):
242
+ self._remove_qFormat()
243
+ if bool(value):
244
+ self._add_qFormat()
245
+
246
+ @property
247
+ def semiHidden_val(self):
248
+ """Value of ``<w:semiHidden>`` child or |False| if not present."""
249
+ semiHidden = self.semiHidden
250
+ if semiHidden is None:
251
+ return False
252
+ return semiHidden.val
253
+
254
+ @semiHidden_val.setter
255
+ def semiHidden_val(self, value):
256
+ self._remove_semiHidden()
257
+ if bool(value) is True:
258
+ semiHidden = self._add_semiHidden()
259
+ semiHidden.val = value
260
+
261
+ @property
262
+ def uiPriority_val(self):
263
+ """Value of ``<w:uiPriority>`` child or |None| if not present."""
264
+ uiPriority = self.uiPriority
265
+ if uiPriority is None:
266
+ return None
267
+ return uiPriority.val
268
+
269
+ @uiPriority_val.setter
270
+ def uiPriority_val(self, value):
271
+ self._remove_uiPriority()
272
+ if value is not None:
273
+ uiPriority = self._add_uiPriority()
274
+ uiPriority.val = value
275
+
276
+ @property
277
+ def unhideWhenUsed_val(self):
278
+ """Value of `w:unhideWhenUsed/@w:val` or |False| if not present."""
279
+ unhideWhenUsed = self.unhideWhenUsed
280
+ if unhideWhenUsed is None:
281
+ return False
282
+ return unhideWhenUsed.val
283
+
284
+ @unhideWhenUsed_val.setter
285
+ def unhideWhenUsed_val(self, value):
286
+ self._remove_unhideWhenUsed()
287
+ if bool(value) is True:
288
+ unhideWhenUsed = self._add_unhideWhenUsed()
289
+ unhideWhenUsed.val = value
290
+
291
+
292
+ class CT_Styles(BaseOxmlElement):
293
+ """``<w:styles>`` element, the root element of a styles part, i.e. styles.xml."""
294
+
295
+ _tag_seq = ("w:docDefaults", "w:latentStyles", "w:style")
296
+ docDefaults = ZeroOrOne("w:docDefaults", successors=_tag_seq[1:])
297
+ latentStyles = ZeroOrOne("w:latentStyles", successors=_tag_seq[2:])
298
+ style = ZeroOrMore("w:style", successors=())
299
+ del _tag_seq
300
+
301
+ def add_style_of_type(self, name, style_type, builtin):
302
+ """Return a newly added `w:style` element having `name` and `style_type`.
303
+
304
+ `w:style/@customStyle` is set based on the value of `builtin`.
305
+ """
306
+ style = self.add_style()
307
+ style.type = style_type
308
+ style.customStyle = None if builtin else True
309
+ style.styleId = styleId_from_name(name)
310
+ style.name_val = name
311
+ return style
312
+
313
+ def default_for(self, style_type):
314
+ """Return `w:style[@w:type="*{style_type}*][-1]` or |None| if not found."""
315
+ default_styles_for_type = [
316
+ s for s in self._iter_styles() if s.type == style_type and s.default
317
+ ]
318
+ if not default_styles_for_type:
319
+ return None
320
+ # spec calls for last default in document order
321
+ return default_styles_for_type[-1]
322
+
323
+ def get_by_id(self, styleId: str) -> CT_Style | None:
324
+ """`w:style` child where @styleId = `styleId`.
325
+
326
+ |None| if not found.
327
+ """
328
+ xpath = f'w:style[@w:styleId="{styleId}"]'
329
+ return next(iter(self.xpath(xpath)), None)
330
+
331
+ def get_by_name(self, name: str) -> CT_Style | None:
332
+ """`w:style` child with `w:name` grandchild having value `name`.
333
+
334
+ |None| if not found.
335
+ """
336
+ xpath = 'w:style[w:name/@w:val="%s"]' % name
337
+ return next(iter(self.xpath(xpath)), None)
338
+
339
+ def _iter_styles(self):
340
+ """Generate each of the `w:style` child elements in document order."""
341
+ return (style for style in self.xpath("w:style"))