python-pptx2 2.13.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 (175) hide show
  1. pptx2/__init__.py +152 -0
  2. pptx2/_color.py +75 -0
  3. pptx2/_slide_importer.py +597 -0
  4. pptx2/_svg.py +155 -0
  5. pptx2/_template_applier.py +292 -0
  6. pptx2/_textstyle.py +187 -0
  7. pptx2/accessibility.py +365 -0
  8. pptx2/action.py +270 -0
  9. pptx2/animation.py +2237 -0
  10. pptx2/api.py +49 -0
  11. pptx2/audit.py +258 -0
  12. pptx2/chart/__init__.py +0 -0
  13. pptx2/chart/analytics.py +381 -0
  14. pptx2/chart/axis.py +543 -0
  15. pptx2/chart/category.py +200 -0
  16. pptx2/chart/chart.py +670 -0
  17. pptx2/chart/data.py +864 -0
  18. pptx2/chart/datalabel.py +406 -0
  19. pptx2/chart/legend.py +86 -0
  20. pptx2/chart/marker.py +70 -0
  21. pptx2/chart/palettes.py +129 -0
  22. pptx2/chart/plot.py +462 -0
  23. pptx2/chart/point.py +101 -0
  24. pptx2/chart/quick_layouts.py +325 -0
  25. pptx2/chart/series.py +334 -0
  26. pptx2/chart/xlsx.py +272 -0
  27. pptx2/chart/xmlwriter.py +1845 -0
  28. pptx2/compose/__init__.py +28 -0
  29. pptx2/compose/from_spec.py +1094 -0
  30. pptx2/design/__init__.py +8 -0
  31. pptx2/design/components.py +607 -0
  32. pptx2/design/figures.py +389 -0
  33. pptx2/design/layout.py +370 -0
  34. pptx2/design/recipes.py +1967 -0
  35. pptx2/design/style.py +209 -0
  36. pptx2/design/tokens.py +915 -0
  37. pptx2/diagrams.py +754 -0
  38. pptx2/dml/__init__.py +0 -0
  39. pptx2/dml/chtfmt.py +40 -0
  40. pptx2/dml/color.py +496 -0
  41. pptx2/dml/effect.py +909 -0
  42. pptx2/dml/fill.py +691 -0
  43. pptx2/dml/line.py +287 -0
  44. pptx2/dml/picture.py +212 -0
  45. pptx2/dml/three_d.py +381 -0
  46. pptx2/enum/__init__.py +0 -0
  47. pptx2/enum/action.py +71 -0
  48. pptx2/enum/animation.py +31 -0
  49. pptx2/enum/base.py +218 -0
  50. pptx2/enum/chart.py +574 -0
  51. pptx2/enum/dml.py +740 -0
  52. pptx2/enum/lang.py +685 -0
  53. pptx2/enum/presentation.py +133 -0
  54. pptx2/enum/shapes.py +1029 -0
  55. pptx2/enum/text.py +230 -0
  56. pptx2/exc.py +42 -0
  57. pptx2/formats.py +139 -0
  58. pptx2/geometry.py +420 -0
  59. pptx2/inherit.py +109 -0
  60. pptx2/lint.py +2256 -0
  61. pptx2/math.py +177 -0
  62. pptx2/media.py +197 -0
  63. pptx2/opc/__init__.py +0 -0
  64. pptx2/opc/constants.py +332 -0
  65. pptx2/opc/oxml.py +188 -0
  66. pptx2/opc/package.py +762 -0
  67. pptx2/opc/packuri.py +109 -0
  68. pptx2/opc/serialized.py +296 -0
  69. pptx2/opc/shared.py +20 -0
  70. pptx2/opc/spec.py +45 -0
  71. pptx2/oxml/__init__.py +555 -0
  72. pptx2/oxml/action.py +53 -0
  73. pptx2/oxml/chart/__init__.py +0 -0
  74. pptx2/oxml/chart/axis.py +337 -0
  75. pptx2/oxml/chart/chart.py +481 -0
  76. pptx2/oxml/chart/datalabel.py +253 -0
  77. pptx2/oxml/chart/legend.py +72 -0
  78. pptx2/oxml/chart/marker.py +61 -0
  79. pptx2/oxml/chart/plot.py +365 -0
  80. pptx2/oxml/chart/series.py +425 -0
  81. pptx2/oxml/chart/shared.py +220 -0
  82. pptx2/oxml/coreprops.py +288 -0
  83. pptx2/oxml/dml/__init__.py +0 -0
  84. pptx2/oxml/dml/color.py +135 -0
  85. pptx2/oxml/dml/effect.py +213 -0
  86. pptx2/oxml/dml/fill.py +316 -0
  87. pptx2/oxml/dml/line.py +12 -0
  88. pptx2/oxml/dml/three_d.py +110 -0
  89. pptx2/oxml/ns.py +135 -0
  90. pptx2/oxml/presentation.py +313 -0
  91. pptx2/oxml/shapes/__init__.py +19 -0
  92. pptx2/oxml/shapes/autoshape.py +467 -0
  93. pptx2/oxml/shapes/connector.py +107 -0
  94. pptx2/oxml/shapes/graphfrm.py +347 -0
  95. pptx2/oxml/shapes/groupshape.py +329 -0
  96. pptx2/oxml/shapes/picture.py +270 -0
  97. pptx2/oxml/shapes/shared.py +577 -0
  98. pptx2/oxml/simpletypes.py +1027 -0
  99. pptx2/oxml/slide.py +563 -0
  100. pptx2/oxml/table.py +650 -0
  101. pptx2/oxml/text.py +815 -0
  102. pptx2/oxml/theme.py +36 -0
  103. pptx2/oxml/xmlchemy.py +717 -0
  104. pptx2/package.py +222 -0
  105. pptx2/parts/__init__.py +0 -0
  106. pptx2/parts/chart.py +95 -0
  107. pptx2/parts/coreprops.py +167 -0
  108. pptx2/parts/diagram.py +37 -0
  109. pptx2/parts/embeddedpackage.py +93 -0
  110. pptx2/parts/image.py +275 -0
  111. pptx2/parts/media.py +37 -0
  112. pptx2/parts/presentation.py +136 -0
  113. pptx2/parts/slide.py +371 -0
  114. pptx2/presentation.py +408 -0
  115. pptx2/py.typed +0 -0
  116. pptx2/render.py +586 -0
  117. pptx2/section.py +272 -0
  118. pptx2/shapes/__init__.py +26 -0
  119. pptx2/shapes/autoshape.py +442 -0
  120. pptx2/shapes/base.py +1078 -0
  121. pptx2/shapes/connector.py +297 -0
  122. pptx2/shapes/freeform.py +337 -0
  123. pptx2/shapes/graphfrm.py +316 -0
  124. pptx2/shapes/group.py +264 -0
  125. pptx2/shapes/picture.py +422 -0
  126. pptx2/shapes/placeholder.py +468 -0
  127. pptx2/shapes/shapetree.py +2027 -0
  128. pptx2/shared.py +82 -0
  129. pptx2/skill/SKILL.md +450 -0
  130. pptx2/skill/__init__.py +78 -0
  131. pptx2/skill/__main__.py +64 -0
  132. pptx2/skill/references/animations.md +189 -0
  133. pptx2/skill/references/basics.md +421 -0
  134. pptx2/skill/references/charts.md +254 -0
  135. pptx2/skill/references/compose.md +234 -0
  136. pptx2/skill/references/design.md +366 -0
  137. pptx2/skill/references/effects.md +249 -0
  138. pptx2/skill/references/end-to-end-deck.md +231 -0
  139. pptx2/skill/references/geometry-and-arrows.md +334 -0
  140. pptx2/skill/references/lint.md +275 -0
  141. pptx2/skill/references/math.md +86 -0
  142. pptx2/skill/references/picture-effects.md +129 -0
  143. pptx2/skill/references/render.md +151 -0
  144. pptx2/skill/references/smart-art.md +75 -0
  145. pptx2/skill/references/space-aware-authoring.md +249 -0
  146. pptx2/skill/references/tables.md +244 -0
  147. pptx2/skill/references/theme.md +127 -0
  148. pptx2/skill/references/three-d.md +109 -0
  149. pptx2/skill/references/transitions.md +100 -0
  150. pptx2/slide.py +1244 -0
  151. pptx2/smart_art.py +220 -0
  152. pptx2/spec.py +633 -0
  153. pptx2/table.py +1181 -0
  154. pptx2/table_styles.py +184 -0
  155. pptx2/templates/default.pptx +0 -0
  156. pptx2/templates/docx-icon.emf +0 -0
  157. pptx2/templates/generic-icon.emf +0 -0
  158. pptx2/templates/notes.xml +23 -0
  159. pptx2/templates/notesMaster.xml +352 -0
  160. pptx2/templates/pptx-icon.emf +0 -0
  161. pptx2/templates/theme.xml +321 -0
  162. pptx2/templates/xlsx-icon.emf +0 -0
  163. pptx2/text/__init__.py +0 -0
  164. pptx2/text/fonts.py +482 -0
  165. pptx2/text/layout.py +374 -0
  166. pptx2/text/text.py +1272 -0
  167. pptx2/theme.py +721 -0
  168. pptx2/types.py +36 -0
  169. pptx2/util.py +263 -0
  170. python_pptx2-2.13.0.dist-info/METADATA +351 -0
  171. python_pptx2-2.13.0.dist-info/RECORD +175 -0
  172. python_pptx2-2.13.0.dist-info/WHEEL +5 -0
  173. python_pptx2-2.13.0.dist-info/entry_points.txt +3 -0
  174. python_pptx2-2.13.0.dist-info/licenses/LICENSE +22 -0
  175. python_pptx2-2.13.0.dist-info/top_level.txt +1 -0
pptx2/oxml/table.py ADDED
@@ -0,0 +1,650 @@
1
+ """Custom element classes for table-related XML elements"""
2
+
3
+ from __future__ import annotations
4
+
5
+ from typing import TYPE_CHECKING, Callable, Iterator, cast
6
+
7
+ from pptx2.enum.text import MSO_VERTICAL_ANCHOR
8
+ from pptx2.oxml import parse_xml
9
+ from pptx2.oxml.dml.fill import CT_GradientFillProperties
10
+ from pptx2.oxml.ns import nsdecls
11
+ from pptx2.oxml.simpletypes import (
12
+ ST_Coordinate,
13
+ ST_Coordinate32,
14
+ ST_TextVerticalType,
15
+ XsdBoolean,
16
+ XsdInt,
17
+ )
18
+ from pptx2.oxml.text import CT_TextBody
19
+ from pptx2.oxml.xmlchemy import (
20
+ BaseOxmlElement,
21
+ Choice,
22
+ OneAndOnlyOne,
23
+ OptionalAttribute,
24
+ RequiredAttribute,
25
+ ZeroOrMore,
26
+ ZeroOrOne,
27
+ ZeroOrOneChoice,
28
+ )
29
+ from pptx2.util import Emu, lazyproperty
30
+
31
+ if TYPE_CHECKING:
32
+ from pptx2.util import Length
33
+
34
+
35
+ class CT_Table(BaseOxmlElement):
36
+ """`a:tbl` custom element class"""
37
+
38
+ get_or_add_tblPr: Callable[[], CT_TableProperties]
39
+ tr_lst: list[CT_TableRow]
40
+ _add_tr: Callable[..., CT_TableRow]
41
+
42
+ _tag_seq = ("a:tblPr", "a:tblGrid", "a:tr")
43
+ tblPr: CT_TableProperties | None = ZeroOrOne( # pyright: ignore[reportAssignmentType]
44
+ "a:tblPr", successors=_tag_seq[1:]
45
+ )
46
+ tblGrid: CT_TableGrid = OneAndOnlyOne("a:tblGrid") # pyright: ignore[reportAssignmentType]
47
+ tr = ZeroOrMore("a:tr", successors=_tag_seq[3:])
48
+ del _tag_seq
49
+
50
+ def add_tr(self, height: Length) -> CT_TableRow:
51
+ """Return a newly created `a:tr` child element having its `h` attribute set to `height`."""
52
+ return self._add_tr(h=height)
53
+
54
+ @property
55
+ def bandCol(self) -> bool:
56
+ return self._get_boolean_property("bandCol")
57
+
58
+ @bandCol.setter
59
+ def bandCol(self, value: bool):
60
+ self._set_boolean_property("bandCol", value)
61
+
62
+ @property
63
+ def bandRow(self) -> bool:
64
+ return self._get_boolean_property("bandRow")
65
+
66
+ @bandRow.setter
67
+ def bandRow(self, value: bool):
68
+ self._set_boolean_property("bandRow", value)
69
+
70
+ @property
71
+ def firstCol(self) -> bool:
72
+ return self._get_boolean_property("firstCol")
73
+
74
+ @firstCol.setter
75
+ def firstCol(self, value: bool):
76
+ self._set_boolean_property("firstCol", value)
77
+
78
+ @property
79
+ def firstRow(self) -> bool:
80
+ return self._get_boolean_property("firstRow")
81
+
82
+ @firstRow.setter
83
+ def firstRow(self, value: bool):
84
+ self._set_boolean_property("firstRow", value)
85
+
86
+ def iter_tcs(self) -> Iterator[CT_TableCell]:
87
+ """Generate each `a:tc` element in this tbl.
88
+
89
+ `a:tc` elements are generated left-to-right, top-to-bottom.
90
+ """
91
+ return (tc for tr in self.tr_lst for tc in tr.tc_lst)
92
+
93
+ @property
94
+ def lastCol(self) -> bool:
95
+ return self._get_boolean_property("lastCol")
96
+
97
+ @lastCol.setter
98
+ def lastCol(self, value: bool):
99
+ self._set_boolean_property("lastCol", value)
100
+
101
+ @property
102
+ def lastRow(self) -> bool:
103
+ return self._get_boolean_property("lastRow")
104
+
105
+ @lastRow.setter
106
+ def lastRow(self, value: bool):
107
+ self._set_boolean_property("lastRow", value)
108
+
109
+ @classmethod
110
+ def new_tbl(
111
+ cls, rows: int, cols: int, width: int, height: int, tableStyleId: str | None = None
112
+ ) -> CT_Table:
113
+ """Return a new `p:tbl` element tree."""
114
+ # working hypothesis is this is the default table style GUID
115
+ if tableStyleId is None:
116
+ tableStyleId = "{5C22544A-7EE6-4342-B048-85BDC9FD1C3A}"
117
+
118
+ xml = cls._tbl_tmpl() % (tableStyleId)
119
+ tbl = cast(CT_Table, parse_xml(xml))
120
+
121
+ # add specified number of rows and columns
122
+ rowheight = height // rows
123
+ colwidth = width // cols
124
+
125
+ for col in range(cols):
126
+ # adjust width of last col to absorb any div error
127
+ if col == cols - 1:
128
+ colwidth = width - ((cols - 1) * colwidth)
129
+ tbl.tblGrid.add_gridCol(width=Emu(colwidth))
130
+
131
+ for row in range(rows):
132
+ # adjust height of last row to absorb any div error
133
+ if row == rows - 1:
134
+ rowheight = height - ((rows - 1) * rowheight)
135
+ tr = tbl.add_tr(height=Emu(rowheight))
136
+ for col in range(cols):
137
+ tr.add_tc()
138
+
139
+ return tbl
140
+
141
+ def tc(self, row_idx: int, col_idx: int) -> CT_TableCell:
142
+ """Return `a:tc` element at `row_idx`, `col_idx`."""
143
+ return self.tr_lst[row_idx].tc_lst[col_idx]
144
+
145
+ def _get_boolean_property(self, propname: str) -> bool:
146
+ """Generalized getter for the boolean properties on the `a:tblPr` child element.
147
+
148
+ Defaults to False if `propname` attribute is missing or `a:tblPr` element itself is not
149
+ present.
150
+ """
151
+ tblPr = self.tblPr
152
+ if tblPr is None:
153
+ return False
154
+ propval = getattr(tblPr, propname)
155
+ return {True: True, False: False, None: False}[propval]
156
+
157
+ def _set_boolean_property(self, propname: str, value: bool) -> None:
158
+ """Generalized setter for boolean properties on the `a:tblPr` child element.
159
+
160
+ Sets `propname` attribute appropriately based on `value`. If `value` is True, the
161
+ attribute is set to "1"; a tblPr child element is added if necessary. If `value` is False,
162
+ the `propname` attribute is removed if present, allowing its default value of False to be
163
+ its effective value.
164
+ """
165
+ if value not in (True, False):
166
+ raise ValueError("assigned value must be either True or False, got %s" % value)
167
+ tblPr = self.get_or_add_tblPr()
168
+ setattr(tblPr, propname, value)
169
+
170
+ @classmethod
171
+ def _tbl_tmpl(cls):
172
+ return (
173
+ "<a:tbl %s>\n"
174
+ ' <a:tblPr firstRow="1" bandRow="1">\n'
175
+ " <a:tableStyleId>%s</a:tableStyleId>\n"
176
+ " </a:tblPr>\n"
177
+ " <a:tblGrid/>\n"
178
+ "</a:tbl>" % (nsdecls("a"), "%s")
179
+ )
180
+
181
+
182
+ class CT_TableCell(BaseOxmlElement):
183
+ """`a:tc` custom element class"""
184
+
185
+ get_or_add_tcPr: Callable[[], CT_TableCellProperties]
186
+ get_or_add_txBody: Callable[[], CT_TextBody]
187
+
188
+ _tag_seq = ("a:txBody", "a:tcPr", "a:extLst")
189
+ txBody: CT_TextBody | None = ZeroOrOne( # pyright: ignore[reportAssignmentType]
190
+ "a:txBody", successors=_tag_seq[1:]
191
+ )
192
+ tcPr: CT_TableCellProperties | None = ZeroOrOne( # pyright: ignore[reportAssignmentType]
193
+ "a:tcPr", successors=_tag_seq[2:]
194
+ )
195
+ del _tag_seq
196
+
197
+ gridSpan: int = OptionalAttribute( # pyright: ignore[reportAssignmentType]
198
+ "gridSpan", XsdInt, default=1
199
+ )
200
+ rowSpan: int = OptionalAttribute( # pyright: ignore[reportAssignmentType]
201
+ "rowSpan", XsdInt, default=1
202
+ )
203
+ hMerge: bool = OptionalAttribute( # pyright: ignore[reportAssignmentType]
204
+ "hMerge", XsdBoolean, default=False
205
+ )
206
+ vMerge: bool = OptionalAttribute( # pyright: ignore[reportAssignmentType]
207
+ "vMerge", XsdBoolean, default=False
208
+ )
209
+
210
+ @property
211
+ def anchor(self) -> MSO_VERTICAL_ANCHOR | None:
212
+ """String held in `anchor` attribute of `a:tcPr` child element of this `a:tc` element."""
213
+ if self.tcPr is None:
214
+ return None
215
+ return self.tcPr.anchor
216
+
217
+ @anchor.setter
218
+ def anchor(self, anchor_enum_idx: MSO_VERTICAL_ANCHOR | None):
219
+ """Set value of anchor attribute on `a:tcPr` child element."""
220
+ if anchor_enum_idx is None and self.tcPr is None:
221
+ return
222
+ tcPr = self.get_or_add_tcPr()
223
+ tcPr.anchor = anchor_enum_idx
224
+
225
+ def append_ps_from(self, spanned_tc: CT_TableCell):
226
+ """Append `a:p` elements taken from `spanned_tc`.
227
+
228
+ Any non-empty paragraph elements in `spanned_tc` are removed and appended to the
229
+ text-frame of this cell. If `spanned_tc` is left with no content after this process, a
230
+ single empty `a:p` element is added to ensure the cell is compliant with the spec.
231
+ """
232
+ source_txBody = spanned_tc.get_or_add_txBody()
233
+ target_txBody = self.get_or_add_txBody()
234
+
235
+ # ---if source is empty, there's nothing to do---
236
+ if source_txBody.is_empty:
237
+ return
238
+
239
+ # ---a single empty paragraph in target is overwritten---
240
+ if target_txBody.is_empty:
241
+ target_txBody.clear_content()
242
+
243
+ for p in source_txBody.p_lst:
244
+ target_txBody.append(p)
245
+
246
+ # ---neither source nor target can be left without ps---
247
+ source_txBody.unclear_content()
248
+ target_txBody.unclear_content()
249
+
250
+ @property
251
+ def col_idx(self) -> int:
252
+ """Offset of this cell's column in its table."""
253
+ # ---tc elements come before any others in `a:tr` element---
254
+ return cast(CT_TableRow, self.getparent()).index(self)
255
+
256
+ @property
257
+ def is_merge_origin(self) -> bool:
258
+ """True if cell is top-left in merged cell range."""
259
+ if self.gridSpan > 1 and not self.vMerge:
260
+ return True
261
+ return self.rowSpan > 1 and not self.hMerge
262
+
263
+ @property
264
+ def is_spanned(self) -> bool:
265
+ """True if cell is in merged cell range but not merge origin cell."""
266
+ return self.hMerge or self.vMerge
267
+
268
+ @property
269
+ def marT(self) -> Length:
270
+ """Top margin for this cell.
271
+
272
+ This value is stored in the `marT` attribute of the `a:tcPr` child element of this `a:tc`.
273
+
274
+ Read/write. If the attribute is not present, the default value `45720` (0.05 inches) is
275
+ returned for top and bottom; `91440` (0.10 inches) is the default for left and right.
276
+ Assigning |None| to any `marX` property clears that attribute from the element,
277
+ effectively setting it to the default value.
278
+ """
279
+ return self._get_marX("marT", Emu(45720))
280
+
281
+ @marT.setter
282
+ def marT(self, value: Length | None):
283
+ self._set_marX("marT", value)
284
+
285
+ @property
286
+ def marR(self) -> Length:
287
+ """Right margin value represented in `marR` attribute."""
288
+ return self._get_marX("marR", Emu(91440))
289
+
290
+ @marR.setter
291
+ def marR(self, value: Length | None):
292
+ self._set_marX("marR", value)
293
+
294
+ @property
295
+ def marB(self) -> Length:
296
+ """Bottom margin value represented in `marB` attribute."""
297
+ return self._get_marX("marB", Emu(45720))
298
+
299
+ @marB.setter
300
+ def marB(self, value: Length | None):
301
+ self._set_marX("marB", value)
302
+
303
+ @property
304
+ def marL(self) -> Length:
305
+ """Left margin value represented in `marL` attribute."""
306
+ return self._get_marX("marL", Emu(91440))
307
+
308
+ @marL.setter
309
+ def marL(self, value: Length | None):
310
+ self._set_marX("marL", value)
311
+
312
+ @classmethod
313
+ def new(cls) -> CT_TableCell:
314
+ """Return a new `a:tc` element subtree."""
315
+ return cast(
316
+ CT_TableCell,
317
+ parse_xml(
318
+ f"<a:tc {nsdecls('a')}>\n"
319
+ f" <a:txBody>\n"
320
+ f" <a:bodyPr/>\n"
321
+ f" <a:lstStyle/>\n"
322
+ f" <a:p/>\n"
323
+ f" </a:txBody>\n"
324
+ f" <a:tcPr/>\n"
325
+ f"</a:tc>"
326
+ ),
327
+ )
328
+
329
+ @property
330
+ def row_idx(self) -> int:
331
+ """Offset of this cell's row in its table."""
332
+ return cast(CT_TableRow, self.getparent()).row_idx
333
+
334
+ @property
335
+ def tbl(self) -> CT_Table:
336
+ """Table element this cell belongs to."""
337
+ return cast(CT_Table, self.xpath("ancestor::a:tbl")[0])
338
+
339
+ @property
340
+ def text(self) -> str: # pyright: ignore[reportIncompatibleMethodOverride]
341
+ """str text contained in cell"""
342
+ # ---note this shadows lxml _Element.text---
343
+ txBody = self.txBody
344
+ if txBody is None:
345
+ return ""
346
+ return "\n".join([p.text for p in txBody.p_lst])
347
+
348
+ def _get_marX(self, attr_name: str, default: Length) -> Length:
349
+ """Generalized method to get margin values."""
350
+ if self.tcPr is None:
351
+ return Emu(default)
352
+ return Emu(int(self.tcPr.get(attr_name, default)))
353
+
354
+ def _new_txBody(self) -> CT_TextBody:
355
+ return CT_TextBody.new_a_txBody()
356
+
357
+ def _set_marX(self, marX: str, value: Length | None) -> None:
358
+ """Set value of marX attribute on `a:tcPr` child element.
359
+
360
+ If `marX` is |None|, the marX attribute is removed. `marX` is a string, one of `('marL',
361
+ 'marR', 'marT', 'marB')`.
362
+ """
363
+ if value is None and self.tcPr is None:
364
+ return
365
+ tcPr = self.get_or_add_tcPr()
366
+ setattr(tcPr, marX, value)
367
+
368
+
369
+ class CT_TableCellProperties(BaseOxmlElement):
370
+ """`a:tcPr` custom element class"""
371
+
372
+ _tag_seq = (
373
+ "a:lnL",
374
+ "a:lnR",
375
+ "a:lnT",
376
+ "a:lnB",
377
+ "a:lnTlToBr",
378
+ "a:lnBlToTr",
379
+ "a:cell3D",
380
+ "a:noFill",
381
+ "a:solidFill",
382
+ "a:gradFill",
383
+ "a:blipFill",
384
+ "a:pattFill",
385
+ "a:grpFill",
386
+ "a:headers",
387
+ "a:extLst",
388
+ )
389
+ lnL = ZeroOrOne("a:lnL", successors=_tag_seq[1:])
390
+ lnR = ZeroOrOne("a:lnR", successors=_tag_seq[2:])
391
+ lnT = ZeroOrOne("a:lnT", successors=_tag_seq[3:])
392
+ lnB = ZeroOrOne("a:lnB", successors=_tag_seq[4:])
393
+ lnTlToBr = ZeroOrOne("a:lnTlToBr", successors=_tag_seq[5:])
394
+ lnBlToTr = ZeroOrOne("a:lnBlToTr", successors=_tag_seq[6:])
395
+ eg_fillProperties = ZeroOrOneChoice(
396
+ (
397
+ Choice("a:noFill"),
398
+ Choice("a:solidFill"),
399
+ Choice("a:gradFill"),
400
+ Choice("a:blipFill"),
401
+ Choice("a:pattFill"),
402
+ Choice("a:grpFill"),
403
+ ),
404
+ successors=("a:headers", "a:extLst"),
405
+ )
406
+ del _tag_seq
407
+ anchor: MSO_VERTICAL_ANCHOR | None = OptionalAttribute( # pyright: ignore[reportAssignmentType]
408
+ "anchor", MSO_VERTICAL_ANCHOR
409
+ )
410
+ vert: str | None = OptionalAttribute( # pyright: ignore[reportAssignmentType]
411
+ "vert", ST_TextVerticalType
412
+ )
413
+ marL: Length | None = OptionalAttribute( # pyright: ignore[reportAssignmentType]
414
+ "marL", ST_Coordinate32
415
+ )
416
+ marR: Length | None = OptionalAttribute( # pyright: ignore[reportAssignmentType]
417
+ "marR", ST_Coordinate32
418
+ )
419
+ marT: Length | None = OptionalAttribute( # pyright: ignore[reportAssignmentType]
420
+ "marT", ST_Coordinate32
421
+ )
422
+ marB: Length | None = OptionalAttribute( # pyright: ignore[reportAssignmentType]
423
+ "marB", ST_Coordinate32
424
+ )
425
+
426
+ def _new_gradFill(self):
427
+ return CT_GradientFillProperties.new_gradFill()
428
+
429
+
430
+ class CT_TableCol(BaseOxmlElement):
431
+ """`a:gridCol` custom element class."""
432
+
433
+ w: Length = RequiredAttribute("w", ST_Coordinate) # pyright: ignore[reportAssignmentType]
434
+
435
+
436
+ class CT_TableGrid(BaseOxmlElement):
437
+ """`a:tblGrid` custom element class."""
438
+
439
+ gridCol_lst: list[CT_TableCol]
440
+ _add_gridCol: Callable[..., CT_TableCol]
441
+
442
+ gridCol = ZeroOrMore("a:gridCol")
443
+
444
+ def add_gridCol(self, width: Length) -> CT_TableCol:
445
+ """A newly appended `a:gridCol` child element having its `w` attribute set to `width`."""
446
+ return self._add_gridCol(w=width)
447
+
448
+
449
+ class CT_TableProperties(BaseOxmlElement):
450
+ """`a:tblPr` custom element class."""
451
+
452
+ get_or_add_tableStyleId: Callable[[], BaseOxmlElement]
453
+ _remove_tableStyleId: Callable[[], None]
454
+
455
+ # `a:tableStyleId` is the second member of a choice
456
+ # (`a:tableStyle` | `a:tableStyleId`) that follows the fill/effect
457
+ # property groups and precedes `a:extLst` in `CT_TableProperties`
458
+ # (ISO/IEC 29500-1, dml-main.xsd). Built-in styles are referenced by
459
+ # GUID through this element.
460
+ tableStyleId: BaseOxmlElement | None = ZeroOrOne( # pyright: ignore[reportAssignmentType]
461
+ "a:tableStyleId", successors=("a:extLst",)
462
+ )
463
+
464
+ bandRow = OptionalAttribute("bandRow", XsdBoolean, default=False)
465
+ bandCol = OptionalAttribute("bandCol", XsdBoolean, default=False)
466
+ firstRow = OptionalAttribute("firstRow", XsdBoolean, default=False)
467
+ firstCol = OptionalAttribute("firstCol", XsdBoolean, default=False)
468
+ lastRow = OptionalAttribute("lastRow", XsdBoolean, default=False)
469
+ lastCol = OptionalAttribute("lastCol", XsdBoolean, default=False)
470
+
471
+ @property
472
+ def tableStyleId_val(self) -> str | None:
473
+ """The GUID string held by the `a:tableStyleId` child, or |None|."""
474
+ tableStyleId = self.tableStyleId
475
+ if tableStyleId is None:
476
+ return None
477
+ text = tableStyleId.text
478
+ return text if text else None
479
+
480
+ @tableStyleId_val.setter
481
+ def tableStyleId_val(self, guid: str | None) -> None:
482
+ """Set (or clear when |None|) the GUID held by the `a:tableStyleId` child."""
483
+ if guid is None:
484
+ self._remove_tableStyleId()
485
+ return
486
+ self.get_or_add_tableStyleId().text = guid
487
+
488
+
489
+ class CT_TableRow(BaseOxmlElement):
490
+ """`a:tr` custom element class."""
491
+
492
+ tc_lst: list[CT_TableCell]
493
+ _add_tc: Callable[[], CT_TableCell]
494
+
495
+ tc = ZeroOrMore("a:tc", successors=("a:extLst",))
496
+ h: Length = RequiredAttribute("h", ST_Coordinate) # pyright: ignore[reportAssignmentType]
497
+
498
+ def add_tc(self) -> CT_TableCell:
499
+ """A newly added minimal valid `a:tc` child element."""
500
+ return self._add_tc()
501
+
502
+ @property
503
+ def row_idx(self) -> int:
504
+ """Offset of this row in its table."""
505
+ return cast(CT_Table, self.getparent()).tr_lst.index(self)
506
+
507
+ def _new_tc(self):
508
+ return CT_TableCell.new()
509
+
510
+
511
+ class TcRange(object):
512
+ """A 2D block of `a:tc` cell elements in a table.
513
+
514
+ This object assumes the structure of the underlying table does not change during its lifetime.
515
+ Structural changes in this context would be insertion or removal of rows or columns.
516
+
517
+ The client is expected to create, use, and then abandon an instance in the context of a single
518
+ user operation that is known to have no structural side-effects of this type.
519
+ """
520
+
521
+ def __init__(self, tc: CT_TableCell, other_tc: CT_TableCell):
522
+ self._tc = tc
523
+ self._other_tc = other_tc
524
+
525
+ @classmethod
526
+ def from_merge_origin(cls, tc: CT_TableCell):
527
+ """Return instance created from merge-origin tc element."""
528
+ other_tc = tc.tbl.tc(
529
+ tc.row_idx + tc.rowSpan - 1, # ---other_row_idx
530
+ tc.col_idx + tc.gridSpan - 1, # ---other_col_idx
531
+ )
532
+ return cls(tc, other_tc)
533
+
534
+ @lazyproperty
535
+ def contains_merged_cell(self) -> bool:
536
+ """True if one or more cells in range are part of a merged cell."""
537
+ for tc in self.iter_tcs():
538
+ if tc.gridSpan > 1:
539
+ return True
540
+ if tc.rowSpan > 1:
541
+ return True
542
+ if tc.hMerge:
543
+ return True
544
+ if tc.vMerge:
545
+ return True
546
+ return False
547
+
548
+ @lazyproperty
549
+ def dimensions(self) -> tuple[int, int]:
550
+ """(row_count, col_count) pair describing size of range."""
551
+ _, _, width, height = self._extents
552
+ return height, width
553
+
554
+ @lazyproperty
555
+ def in_same_table(self):
556
+ """True if both cells provided to constructor are in same table."""
557
+ if self._tc.tbl is self._other_tc.tbl:
558
+ return True
559
+ return False
560
+
561
+ def iter_except_left_col_tcs(self):
562
+ """Generate each `a:tc` element not in leftmost column of range."""
563
+ for tr in self._tbl.tr_lst[self._top : self._bottom]:
564
+ for tc in tr.tc_lst[self._left + 1 : self._right]:
565
+ yield tc
566
+
567
+ def iter_except_top_row_tcs(self):
568
+ """Generate each `a:tc` element in non-first rows of range."""
569
+ for tr in self._tbl.tr_lst[self._top + 1 : self._bottom]:
570
+ for tc in tr.tc_lst[self._left : self._right]:
571
+ yield tc
572
+
573
+ def iter_left_col_tcs(self):
574
+ """Generate each `a:tc` element in leftmost column of range."""
575
+ col_idx = self._left
576
+ for tr in self._tbl.tr_lst[self._top : self._bottom]:
577
+ yield tr.tc_lst[col_idx]
578
+
579
+ def iter_tcs(self):
580
+ """Generate each `a:tc` element in this range.
581
+
582
+ Cell elements are generated left-to-right, top-to-bottom.
583
+ """
584
+ return (
585
+ tc
586
+ for tr in self._tbl.tr_lst[self._top : self._bottom]
587
+ for tc in tr.tc_lst[self._left : self._right]
588
+ )
589
+
590
+ def iter_top_row_tcs(self):
591
+ """Generate each `a:tc` element in topmost row of range."""
592
+ tr = self._tbl.tr_lst[self._top]
593
+ for tc in tr.tc_lst[self._left : self._right]:
594
+ yield tc
595
+
596
+ def move_content_to_origin(self):
597
+ """Move all paragraphs in range to origin cell."""
598
+ tcs = list(self.iter_tcs())
599
+ origin_tc = tcs[0]
600
+ for spanned_tc in tcs[1:]:
601
+ origin_tc.append_ps_from(spanned_tc)
602
+
603
+ @lazyproperty
604
+ def _bottom(self):
605
+ """Index of row following last row of range"""
606
+ _, top, _, height = self._extents
607
+ return top + height
608
+
609
+ @lazyproperty
610
+ def _extents(self) -> tuple[int, int, int, int]:
611
+ """A (left, top, width, height) tuple describing range extents.
612
+
613
+ Note this is normalized to accommodate the various orderings of the corner cells provided
614
+ on construction, which may be in any of four configurations such as (top-left,
615
+ bottom-right), (bottom-left, top-right), etc.
616
+ """
617
+
618
+ def start_and_size(idx: int, other_idx: int) -> tuple[int, int]:
619
+ """Return beginning and length of range based on two indexes."""
620
+ return min(idx, other_idx), abs(idx - other_idx) + 1
621
+
622
+ tc, other_tc = self._tc, self._other_tc
623
+
624
+ left, width = start_and_size(tc.col_idx, other_tc.col_idx)
625
+ top, height = start_and_size(tc.row_idx, other_tc.row_idx)
626
+
627
+ return left, top, width, height
628
+
629
+ @lazyproperty
630
+ def _left(self):
631
+ """Index of leftmost column in range."""
632
+ left, _, _, _ = self._extents
633
+ return left
634
+
635
+ @lazyproperty
636
+ def _right(self):
637
+ """Index of column following the last column in range."""
638
+ left, _, width, _ = self._extents
639
+ return left + width
640
+
641
+ @lazyproperty
642
+ def _tbl(self):
643
+ """`a:tbl` element containing this cell range."""
644
+ return self._tc.tbl
645
+
646
+ @lazyproperty
647
+ def _top(self):
648
+ """Index of topmost row in range."""
649
+ _, top, _, _ = self._extents
650
+ return top