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