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
@@ -0,0 +1,434 @@
1
+ # pyright: reportImportCycles=false
2
+
3
+ """Simple-type classes, corresponding to ST_* schema items.
4
+
5
+ These provide validation and format translation for values stored in XML element
6
+ attributes. Naming generally corresponds to the simple type in the associated XML
7
+ schema.
8
+ """
9
+
10
+ from __future__ import annotations
11
+
12
+ import datetime as dt
13
+ from typing import TYPE_CHECKING, Any, Tuple
14
+
15
+ from docx.exceptions import InvalidXmlError
16
+ from docx.shared import Emu, Pt, RGBColor, Twips
17
+
18
+ if TYPE_CHECKING:
19
+ from docx.shared import Length
20
+
21
+
22
+ class BaseSimpleType:
23
+ """Base class for simple-types."""
24
+
25
+ @classmethod
26
+ def from_xml(cls, xml_value: str) -> Any:
27
+ return cls.convert_from_xml(xml_value)
28
+
29
+ @classmethod
30
+ def to_xml(cls, value: Any) -> str:
31
+ cls.validate(value)
32
+ str_value = cls.convert_to_xml(value)
33
+ return str_value
34
+
35
+ @classmethod
36
+ def convert_from_xml(cls, str_value: str) -> Any:
37
+ return int(str_value)
38
+
39
+ @classmethod
40
+ def convert_to_xml(cls, value: Any) -> str: ...
41
+
42
+ @classmethod
43
+ def validate(cls, value: Any) -> None: ...
44
+
45
+ @classmethod
46
+ def validate_int(cls, value: object):
47
+ if not isinstance(value, int):
48
+ raise TypeError("value must be <type 'int'>, got %s" % type(value))
49
+
50
+ @classmethod
51
+ def validate_int_in_range(cls, value: int, min_inclusive: int, max_inclusive: int) -> None:
52
+ cls.validate_int(value)
53
+ if value < min_inclusive or value > max_inclusive:
54
+ raise ValueError(
55
+ "value must be in range %d to %d inclusive, got %d"
56
+ % (min_inclusive, max_inclusive, value)
57
+ )
58
+
59
+ @classmethod
60
+ def validate_string(cls, value: Any) -> str:
61
+ if not isinstance(value, str):
62
+ raise TypeError("value must be a string, got %s" % type(value))
63
+ return value
64
+
65
+
66
+ class BaseIntType(BaseSimpleType):
67
+ @classmethod
68
+ def convert_from_xml(cls, str_value: str) -> int:
69
+ return int(str_value)
70
+
71
+ @classmethod
72
+ def convert_to_xml(cls, value: int) -> str:
73
+ return str(value)
74
+
75
+ @classmethod
76
+ def validate(cls, value: Any) -> None:
77
+ cls.validate_int(value)
78
+
79
+
80
+ class BaseStringType(BaseSimpleType):
81
+ @classmethod
82
+ def convert_from_xml(cls, str_value: str) -> str:
83
+ return str_value
84
+
85
+ @classmethod
86
+ def convert_to_xml(cls, value: str) -> str:
87
+ return value
88
+
89
+ @classmethod
90
+ def validate(cls, value: str):
91
+ cls.validate_string(value)
92
+
93
+
94
+ class BaseStringEnumerationType(BaseStringType):
95
+ _members: Tuple[str, ...]
96
+
97
+ @classmethod
98
+ def validate(cls, value: Any) -> None:
99
+ cls.validate_string(value)
100
+ if value not in cls._members:
101
+ raise ValueError("must be one of %s, got '%s'" % (cls._members, value))
102
+
103
+
104
+ class XsdAnyUri(BaseStringType):
105
+ """There's a regex in the spec this is supposed to meet...
106
+
107
+ but current assessment is that spending cycles on validating wouldn't be worth it
108
+ for the number of programming errors it would catch.
109
+ """
110
+
111
+
112
+ class XsdBoolean(BaseSimpleType):
113
+ @classmethod
114
+ def convert_from_xml(cls, str_value: str) -> bool:
115
+ if str_value not in ("1", "0", "true", "false"):
116
+ raise InvalidXmlError(
117
+ "value must be one of '1', '0', 'true' or 'false', got '%s'" % str_value
118
+ )
119
+ return str_value in ("1", "true")
120
+
121
+ @classmethod
122
+ def convert_to_xml(cls, value: bool) -> str:
123
+ return {True: "1", False: "0"}[value]
124
+
125
+ @classmethod
126
+ def validate(cls, value: Any) -> None:
127
+ if value not in (True, False):
128
+ raise TypeError(
129
+ "only True or False (and possibly None) may be assigned, got '%s'" % value
130
+ )
131
+
132
+
133
+ class XsdId(BaseStringType):
134
+ """String that must begin with a letter or underscore and cannot contain any colons.
135
+
136
+ Not fully validated because not used in external API.
137
+ """
138
+
139
+ pass
140
+
141
+
142
+ class XsdInt(BaseIntType):
143
+ @classmethod
144
+ def validate(cls, value: Any) -> None:
145
+ cls.validate_int_in_range(value, -2147483648, 2147483647)
146
+
147
+
148
+ class XsdLong(BaseIntType):
149
+ @classmethod
150
+ def validate(cls, value: Any) -> None:
151
+ cls.validate_int_in_range(value, -9223372036854775808, 9223372036854775807)
152
+
153
+
154
+ class XsdString(BaseStringType):
155
+ pass
156
+
157
+
158
+ class XsdStringEnumeration(BaseStringEnumerationType):
159
+ """Set of enumerated xsd:string values."""
160
+
161
+
162
+ class XsdToken(BaseStringType):
163
+ """Xsd:string with whitespace collapsing, e.g. multiple spaces reduced to one,
164
+ leading and trailing space stripped."""
165
+
166
+ pass
167
+
168
+
169
+ class XsdUnsignedInt(BaseIntType):
170
+ @classmethod
171
+ def validate(cls, value: Any) -> None:
172
+ cls.validate_int_in_range(value, 0, 4294967295)
173
+
174
+
175
+ class XsdUnsignedLong(BaseIntType):
176
+ @classmethod
177
+ def validate(cls, value: Any) -> None:
178
+ cls.validate_int_in_range(value, 0, 18446744073709551615)
179
+
180
+
181
+ class ST_BrClear(XsdString):
182
+ @classmethod
183
+ def validate(cls, value: str) -> None:
184
+ cls.validate_string(value)
185
+ valid_values = ("none", "left", "right", "all")
186
+ if value not in valid_values:
187
+ raise ValueError("must be one of %s, got '%s'" % (valid_values, value))
188
+
189
+
190
+ class ST_BrType(XsdString):
191
+ @classmethod
192
+ def validate(cls, value: Any) -> None:
193
+ cls.validate_string(value)
194
+ valid_values = ("page", "column", "textWrapping")
195
+ if value not in valid_values:
196
+ raise ValueError("must be one of %s, got '%s'" % (valid_values, value))
197
+
198
+
199
+ class ST_Coordinate(BaseIntType):
200
+ @classmethod
201
+ def convert_from_xml(cls, str_value: str) -> Length:
202
+ if "i" in str_value or "m" in str_value or "p" in str_value:
203
+ return ST_UniversalMeasure.convert_from_xml(str_value)
204
+ return Emu(int(str_value))
205
+
206
+ @classmethod
207
+ def validate(cls, value: Any) -> None:
208
+ ST_CoordinateUnqualified.validate(value)
209
+
210
+
211
+ class ST_CoordinateUnqualified(XsdLong):
212
+ @classmethod
213
+ def validate(cls, value: Any) -> None:
214
+ cls.validate_int_in_range(value, -27273042329600, 27273042316900)
215
+
216
+
217
+ class ST_DateTime(BaseSimpleType):
218
+ @classmethod
219
+ def convert_from_xml(cls, str_value: str) -> dt.datetime:
220
+ """Convert an xsd:dateTime string to a datetime object."""
221
+
222
+ def parse_xsd_datetime(dt_str: str) -> dt.datetime:
223
+ # -- handle trailing 'Z' (Zulu/UTC), common in Word files --
224
+ if dt_str.endswith("Z"):
225
+ try:
226
+ # -- optional fractional seconds case --
227
+ return dt.datetime.strptime(dt_str, "%Y-%m-%dT%H:%M:%S.%fZ").replace(
228
+ tzinfo=dt.timezone.utc
229
+ )
230
+ except ValueError:
231
+ return dt.datetime.strptime(dt_str, "%Y-%m-%dT%H:%M:%SZ").replace(
232
+ tzinfo=dt.timezone.utc
233
+ )
234
+
235
+ # -- handles explicit offsets like +00:00, -05:00, or naive datetimes --
236
+ try:
237
+ return dt.datetime.fromisoformat(dt_str)
238
+ except ValueError:
239
+ # -- fall-back to parsing as naive datetime (with or without fractional seconds) --
240
+ try:
241
+ return dt.datetime.strptime(dt_str, "%Y-%m-%dT%H:%M:%S.%f")
242
+ except ValueError:
243
+ return dt.datetime.strptime(dt_str, "%Y-%m-%dT%H:%M:%S")
244
+
245
+ try:
246
+ # -- parse anything reasonable, but never raise, just use default epoch time --
247
+ return parse_xsd_datetime(str_value)
248
+ except Exception:
249
+ return dt.datetime(1970, 1, 1, tzinfo=dt.timezone.utc)
250
+
251
+ @classmethod
252
+ def convert_to_xml(cls, value: dt.datetime) -> str:
253
+ # -- convert naive datetime to timezon-aware assuming local timezone --
254
+ if value.tzinfo is None:
255
+ value = value.astimezone()
256
+
257
+ # -- convert to UTC if not already --
258
+ value = value.astimezone(dt.timezone.utc)
259
+
260
+ # -- format with 'Z' suffix for UTC --
261
+ return value.strftime("%Y-%m-%dT%H:%M:%SZ")
262
+
263
+ @classmethod
264
+ def validate(cls, value: Any) -> None:
265
+ if not isinstance(value, dt.datetime):
266
+ raise TypeError("only a datetime.datetime object may be assigned, got '%s'" % value)
267
+
268
+
269
+ class ST_DecimalNumber(XsdInt):
270
+ pass
271
+
272
+
273
+ class ST_DrawingElementId(XsdUnsignedInt):
274
+ pass
275
+
276
+
277
+ class ST_HexColor(BaseStringType):
278
+ @classmethod
279
+ def convert_from_xml( # pyright: ignore[reportIncompatibleMethodOverride]
280
+ cls, str_value: str
281
+ ) -> RGBColor | str:
282
+ if str_value == "auto":
283
+ return ST_HexColorAuto.AUTO
284
+ return RGBColor.from_string(str_value)
285
+
286
+ @classmethod
287
+ def convert_to_xml( # pyright: ignore[reportIncompatibleMethodOverride]
288
+ cls, value: RGBColor
289
+ ) -> str:
290
+ """Keep alpha hex numerals all uppercase just for consistency."""
291
+ # expecting 3-tuple of ints in range 0-255
292
+ return "%02X%02X%02X" % value
293
+
294
+ @classmethod
295
+ def validate(cls, value: Any) -> None:
296
+ # must be an RGBColor object ---
297
+ if not isinstance(value, RGBColor):
298
+ raise ValueError(
299
+ "rgb color value must be RGBColor object, got %s %s" % (type(value), value)
300
+ )
301
+
302
+
303
+ class ST_HexColorAuto(XsdStringEnumeration):
304
+ """Value for `w:color/[@val="auto"] attribute setting."""
305
+
306
+ AUTO = "auto"
307
+
308
+ _members = (AUTO,)
309
+
310
+
311
+ class ST_HpsMeasure(XsdUnsignedLong):
312
+ """Half-point measure, e.g. 24.0 represents 12.0 points."""
313
+
314
+ @classmethod
315
+ def convert_from_xml(cls, str_value: str) -> Length:
316
+ if "m" in str_value or "n" in str_value or "p" in str_value:
317
+ return ST_UniversalMeasure.convert_from_xml(str_value)
318
+ return Pt(int(str_value) / 2.0)
319
+
320
+ @classmethod
321
+ def convert_to_xml(cls, value: int | Length) -> str:
322
+ emu = Emu(value)
323
+ half_points = int(emu.pt * 2)
324
+ return str(half_points)
325
+
326
+
327
+ class ST_Merge(XsdStringEnumeration):
328
+ """Valid values for <w:xMerge val=""> attribute."""
329
+
330
+ CONTINUE = "continue"
331
+ RESTART = "restart"
332
+
333
+ _members = (CONTINUE, RESTART)
334
+
335
+
336
+ class ST_OnOff(XsdBoolean):
337
+ @classmethod
338
+ def convert_from_xml(cls, str_value: str) -> bool:
339
+ if str_value not in ("1", "0", "true", "false", "on", "off"):
340
+ raise InvalidXmlError(
341
+ "value must be one of '1', '0', 'true', 'false', 'on', or 'o"
342
+ "ff', got '%s'" % str_value
343
+ )
344
+ return str_value in ("1", "true", "on")
345
+
346
+
347
+ class ST_PositiveCoordinate(XsdLong):
348
+ @classmethod
349
+ def convert_from_xml(cls, str_value: str) -> Length:
350
+ return Emu(int(str_value))
351
+
352
+ @classmethod
353
+ def validate(cls, value: Any) -> None:
354
+ cls.validate_int_in_range(value, 0, 27273042316900)
355
+
356
+
357
+ class ST_RelationshipId(XsdString):
358
+ pass
359
+
360
+
361
+ class ST_SignedTwipsMeasure(XsdInt):
362
+ @classmethod
363
+ def convert_from_xml(cls, str_value: str) -> Length:
364
+ if "i" in str_value or "m" in str_value or "p" in str_value:
365
+ return ST_UniversalMeasure.convert_from_xml(str_value)
366
+ return Twips(int(round(float(str_value))))
367
+
368
+ @classmethod
369
+ def convert_to_xml(cls, value: int | Length) -> str:
370
+ emu = Emu(value)
371
+ twips = emu.twips
372
+ return str(twips)
373
+
374
+
375
+ class ST_String(XsdString):
376
+ pass
377
+
378
+
379
+ class ST_TblLayoutType(XsdString):
380
+ @classmethod
381
+ def validate(cls, value: Any) -> None:
382
+ cls.validate_string(value)
383
+ valid_values = ("fixed", "autofit")
384
+ if value not in valid_values:
385
+ raise ValueError("must be one of %s, got '%s'" % (valid_values, value))
386
+
387
+
388
+ class ST_TblWidth(XsdString):
389
+ @classmethod
390
+ def validate(cls, value: Any) -> None:
391
+ cls.validate_string(value)
392
+ valid_values = ("auto", "dxa", "nil", "pct")
393
+ if value not in valid_values:
394
+ raise ValueError("must be one of %s, got '%s'" % (valid_values, value))
395
+
396
+
397
+ class ST_TwipsMeasure(XsdUnsignedLong):
398
+ @classmethod
399
+ def convert_from_xml(cls, str_value: str) -> Length:
400
+ if "i" in str_value or "m" in str_value or "p" in str_value:
401
+ return ST_UniversalMeasure.convert_from_xml(str_value)
402
+ return Twips(int(str_value))
403
+
404
+ @classmethod
405
+ def convert_to_xml(cls, value: int | Length) -> str:
406
+ emu = Emu(value)
407
+ twips = emu.twips
408
+ return str(twips)
409
+
410
+
411
+ class ST_UniversalMeasure(BaseSimpleType):
412
+ @classmethod
413
+ def convert_from_xml(cls, str_value: str) -> Emu:
414
+ float_part, units_part = str_value[:-2], str_value[-2:]
415
+ quantity = float(float_part)
416
+ multiplier = {
417
+ "mm": 36000,
418
+ "cm": 360000,
419
+ "in": 914400,
420
+ "pt": 12700,
421
+ "pc": 152400,
422
+ "pi": 152400,
423
+ }[units_part]
424
+ return Emu(int(round(quantity * multiplier)))
425
+
426
+
427
+ class ST_VerticalAlignRun(XsdStringEnumeration):
428
+ """Valid values for `w:vertAlign/@val`."""
429
+
430
+ BASELINE = "baseline"
431
+ SUPERSCRIPT = "superscript"
432
+ SUBSCRIPT = "subscript"
433
+
434
+ _members = (BASELINE, SUPERSCRIPT, SUBSCRIPT)