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.
- docx/__init__.py +68 -0
- docx/api.py +37 -0
- docx/blkcntnr.py +101 -0
- docx/comments.py +163 -0
- docx/dml/__init__.py +0 -0
- docx/dml/color.py +112 -0
- docx/document.py +275 -0
- docx/drawing/__init__.py +59 -0
- docx/enum/__init__.py +0 -0
- docx/enum/base.py +150 -0
- docx/enum/dml.py +103 -0
- docx/enum/section.py +86 -0
- docx/enum/shape.py +19 -0
- docx/enum/style.py +452 -0
- docx/enum/table.py +136 -0
- docx/enum/text.py +367 -0
- docx/exceptions.py +18 -0
- docx/image/__init__.py +23 -0
- docx/image/bmp.py +43 -0
- docx/image/constants.py +172 -0
- docx/image/exceptions.py +13 -0
- docx/image/gif.py +38 -0
- docx/image/helpers.py +86 -0
- docx/image/image.py +234 -0
- docx/image/jpeg.py +425 -0
- docx/image/png.py +253 -0
- docx/image/tiff.py +289 -0
- docx/opc/__init__.py +0 -0
- docx/opc/constants.py +306 -0
- docx/opc/coreprops.py +142 -0
- docx/opc/exceptions.py +12 -0
- docx/opc/oxml.py +247 -0
- docx/opc/package.py +219 -0
- docx/opc/packuri.py +109 -0
- docx/opc/part.py +247 -0
- docx/opc/parts/__init__.py +0 -0
- docx/opc/parts/coreprops.py +48 -0
- docx/opc/phys_pkg.py +119 -0
- docx/opc/pkgreader.py +254 -0
- docx/opc/pkgwriter.py +115 -0
- docx/opc/rel.py +153 -0
- docx/opc/shared.py +31 -0
- docx/opc/spec.py +24 -0
- docx/oxml/__init__.py +261 -0
- docx/oxml/comments.py +124 -0
- docx/oxml/coreprops.py +298 -0
- docx/oxml/document.py +88 -0
- docx/oxml/drawing.py +11 -0
- docx/oxml/exceptions.py +10 -0
- docx/oxml/ns.py +109 -0
- docx/oxml/numbering.py +109 -0
- docx/oxml/parser.py +62 -0
- docx/oxml/section.py +537 -0
- docx/oxml/settings.py +138 -0
- docx/oxml/shape.py +299 -0
- docx/oxml/shared.py +52 -0
- docx/oxml/simpletypes.py +434 -0
- docx/oxml/styles.py +341 -0
- docx/oxml/table.py +977 -0
- docx/oxml/text/__init__.py +0 -0
- docx/oxml/text/font.py +333 -0
- docx/oxml/text/hyperlink.py +45 -0
- docx/oxml/text/pagebreak.py +278 -0
- docx/oxml/text/paragraph.py +106 -0
- docx/oxml/text/parfmt.py +392 -0
- docx/oxml/text/run.py +307 -0
- docx/oxml/xmlchemy.py +696 -0
- docx/package.py +110 -0
- docx/parts/__init__.py +0 -0
- docx/parts/comments.py +51 -0
- docx/parts/document.py +182 -0
- docx/parts/hdrftr.py +53 -0
- docx/parts/image.py +80 -0
- docx/parts/numbering.py +32 -0
- docx/parts/settings.py +50 -0
- docx/parts/story.py +95 -0
- docx/parts/styles.py +42 -0
- docx/parts/theme.py +53 -0
- docx/py.typed +0 -0
- docx/section.py +479 -0
- docx/settings.py +35 -0
- docx/shape.py +103 -0
- docx/shared.py +382 -0
- docx/styles/__init__.py +40 -0
- docx/styles/latent.py +198 -0
- docx/styles/style.py +264 -0
- docx/styles/styles.py +147 -0
- docx/table.py +537 -0
- docx/templates/default-comments.xml +12 -0
- docx/templates/default-docx-template/[Content_Types].xml +17 -0
- docx/templates/default-docx-template/_rels/.rels +7 -0
- docx/templates/default-docx-template/customXml/_rels/item1.xml.rels +4 -0
- docx/templates/default-docx-template/customXml/item1.xml +2 -0
- docx/templates/default-docx-template/customXml/itemProps1.xml +6 -0
- docx/templates/default-docx-template/docProps/app.xml +36 -0
- docx/templates/default-docx-template/docProps/core.xml +13 -0
- docx/templates/default-docx-template/docProps/thumbnail.jpeg +0 -0
- docx/templates/default-docx-template/word/_rels/document.xml.rels +11 -0
- docx/templates/default-docx-template/word/document.xml +11 -0
- docx/templates/default-docx-template/word/fontTable.xml +61 -0
- docx/templates/default-docx-template/word/numbering.xml +201 -0
- docx/templates/default-docx-template/word/settings.xml +53 -0
- docx/templates/default-docx-template/word/styles.xml +11844 -0
- docx/templates/default-docx-template/word/stylesWithEffects.xml +11800 -0
- docx/templates/default-docx-template/word/theme/theme1.xml +318 -0
- docx/templates/default-docx-template/word/webSettings.xml +5 -0
- docx/templates/default-footer.xml +27 -0
- docx/templates/default-header.xml +27 -0
- docx/templates/default-settings.xml +26 -0
- docx/templates/default-styles.xml +190 -0
- docx/templates/default.docx +0 -0
- docx/text/__init__.py +0 -0
- docx/text/font.py +472 -0
- docx/text/hyperlink.py +121 -0
- docx/text/pagebreak.py +104 -0
- docx/text/paragraph.py +173 -0
- docx/text/parfmt.py +286 -0
- docx/text/run.py +257 -0
- docx/text/tabstops.py +123 -0
- docx/theme.py +67 -0
- docx/types.py +34 -0
- ps_python_docx-1.3.0.dist-info/METADATA +77 -0
- ps_python_docx-1.3.0.dist-info/RECORD +126 -0
- ps_python_docx-1.3.0.dist-info/WHEEL +5 -0
- ps_python_docx-1.3.0.dist-info/licenses/LICENSE +20 -0
- ps_python_docx-1.3.0.dist-info/top_level.txt +1 -0
docx/table.py
ADDED
|
@@ -0,0 +1,537 @@
|
|
|
1
|
+
"""The |Table| object and related proxy classes."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from typing import TYPE_CHECKING, Iterator, cast, overload
|
|
6
|
+
|
|
7
|
+
from typing_extensions import TypeAlias
|
|
8
|
+
|
|
9
|
+
from docx.blkcntnr import BlockItemContainer
|
|
10
|
+
from docx.enum.style import WD_STYLE_TYPE
|
|
11
|
+
from docx.enum.table import WD_CELL_VERTICAL_ALIGNMENT
|
|
12
|
+
from docx.oxml.simpletypes import ST_Merge
|
|
13
|
+
from docx.oxml.table import CT_TblGridCol
|
|
14
|
+
from docx.shared import Inches, Parented, StoryChild, lazyproperty
|
|
15
|
+
|
|
16
|
+
if TYPE_CHECKING:
|
|
17
|
+
import docx.types as t
|
|
18
|
+
from docx.enum.table import WD_ROW_HEIGHT_RULE, WD_TABLE_ALIGNMENT, WD_TABLE_DIRECTION
|
|
19
|
+
from docx.oxml.table import CT_Row, CT_Tbl, CT_TblPr, CT_Tc
|
|
20
|
+
from docx.shared import Length
|
|
21
|
+
from docx.styles.style import (
|
|
22
|
+
ParagraphStyle,
|
|
23
|
+
_TableStyle, # pyright: ignore[reportPrivateUsage]
|
|
24
|
+
)
|
|
25
|
+
|
|
26
|
+
TableParent: TypeAlias = "Table | _Columns | _Rows"
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class Table(StoryChild):
|
|
30
|
+
"""Proxy class for a WordprocessingML ``<w:tbl>`` element."""
|
|
31
|
+
|
|
32
|
+
def __init__(self, tbl: CT_Tbl, parent: t.ProvidesStoryPart):
|
|
33
|
+
super(Table, self).__init__(parent)
|
|
34
|
+
self._element = tbl
|
|
35
|
+
self._tbl = tbl
|
|
36
|
+
|
|
37
|
+
def add_column(self, width: Length):
|
|
38
|
+
"""Return a |_Column| object of `width`, newly added rightmost to the table."""
|
|
39
|
+
tblGrid = self._tbl.tblGrid
|
|
40
|
+
gridCol = tblGrid.add_gridCol()
|
|
41
|
+
gridCol.w = width
|
|
42
|
+
for tr in self._tbl.tr_lst:
|
|
43
|
+
tc = tr.add_tc()
|
|
44
|
+
tc.width = width
|
|
45
|
+
return _Column(gridCol, self)
|
|
46
|
+
|
|
47
|
+
def add_row(self):
|
|
48
|
+
"""Return a |_Row| instance, newly added bottom-most to the table."""
|
|
49
|
+
tbl = self._tbl
|
|
50
|
+
tr = tbl.add_tr()
|
|
51
|
+
for gridCol in tbl.tblGrid.gridCol_lst:
|
|
52
|
+
tc = tr.add_tc()
|
|
53
|
+
if gridCol.w is not None:
|
|
54
|
+
tc.width = gridCol.w
|
|
55
|
+
return _Row(tr, self)
|
|
56
|
+
|
|
57
|
+
@property
|
|
58
|
+
def alignment(self) -> WD_TABLE_ALIGNMENT | None:
|
|
59
|
+
"""Read/write.
|
|
60
|
+
|
|
61
|
+
A member of :ref:`WdRowAlignment` or None, specifying the positioning of this
|
|
62
|
+
table between the page margins. |None| if no setting is specified, causing the
|
|
63
|
+
effective value to be inherited from the style hierarchy.
|
|
64
|
+
"""
|
|
65
|
+
return self._tblPr.alignment
|
|
66
|
+
|
|
67
|
+
@alignment.setter
|
|
68
|
+
def alignment(self, value: WD_TABLE_ALIGNMENT | None):
|
|
69
|
+
self._tblPr.alignment = value
|
|
70
|
+
|
|
71
|
+
@property
|
|
72
|
+
def autofit(self) -> bool:
|
|
73
|
+
"""|True| if column widths can be automatically adjusted to improve the fit of
|
|
74
|
+
cell contents.
|
|
75
|
+
|
|
76
|
+
|False| if table layout is fixed. Column widths are adjusted in either case if
|
|
77
|
+
total column width exceeds page width. Read/write boolean.
|
|
78
|
+
"""
|
|
79
|
+
return self._tblPr.autofit
|
|
80
|
+
|
|
81
|
+
@autofit.setter
|
|
82
|
+
def autofit(self, value: bool):
|
|
83
|
+
self._tblPr.autofit = value
|
|
84
|
+
|
|
85
|
+
def cell(self, row_idx: int, col_idx: int) -> _Cell:
|
|
86
|
+
"""|_Cell| at `row_idx`, `col_idx` intersection.
|
|
87
|
+
|
|
88
|
+
(0, 0) is the top, left-most cell.
|
|
89
|
+
"""
|
|
90
|
+
cell_idx = col_idx + (row_idx * self._column_count)
|
|
91
|
+
return self._cells[cell_idx]
|
|
92
|
+
|
|
93
|
+
def column_cells(self, column_idx: int) -> list[_Cell]:
|
|
94
|
+
"""Sequence of cells in the column at `column_idx` in this table."""
|
|
95
|
+
cells = self._cells
|
|
96
|
+
idxs = range(column_idx, len(cells), self._column_count)
|
|
97
|
+
return [cells[idx] for idx in idxs]
|
|
98
|
+
|
|
99
|
+
@lazyproperty
|
|
100
|
+
def columns(self):
|
|
101
|
+
"""|_Columns| instance representing the sequence of columns in this table."""
|
|
102
|
+
return _Columns(self._tbl, self)
|
|
103
|
+
|
|
104
|
+
def row_cells(self, row_idx: int) -> list[_Cell]:
|
|
105
|
+
"""DEPRECATED: Use `table.rows[row_idx].cells` instead.
|
|
106
|
+
|
|
107
|
+
Sequence of cells in the row at `row_idx` in this table.
|
|
108
|
+
"""
|
|
109
|
+
column_count = self._column_count
|
|
110
|
+
start = row_idx * column_count
|
|
111
|
+
end = start + column_count
|
|
112
|
+
return self._cells[start:end]
|
|
113
|
+
|
|
114
|
+
@lazyproperty
|
|
115
|
+
def rows(self) -> _Rows:
|
|
116
|
+
"""|_Rows| instance containing the sequence of rows in this table."""
|
|
117
|
+
return _Rows(self._tbl, self)
|
|
118
|
+
|
|
119
|
+
@property
|
|
120
|
+
def style(self) -> _TableStyle | None:
|
|
121
|
+
"""|_TableStyle| object representing the style applied to this table.
|
|
122
|
+
|
|
123
|
+
Read/write. The default table style for the document (often `Normal Table`) is
|
|
124
|
+
returned if the table has no directly-applied style. Assigning |None| to this
|
|
125
|
+
property removes any directly-applied table style causing it to inherit the
|
|
126
|
+
default table style of the document.
|
|
127
|
+
|
|
128
|
+
Note that the style name of a table style differs slightly from that displayed
|
|
129
|
+
in the user interface; a hyphen, if it appears, must be removed. For example,
|
|
130
|
+
`Light Shading - Accent 1` becomes `Light Shading Accent 1`.
|
|
131
|
+
"""
|
|
132
|
+
style_id = self._tbl.tblStyle_val
|
|
133
|
+
return cast("_TableStyle | None", self.part.get_style(style_id, WD_STYLE_TYPE.TABLE))
|
|
134
|
+
|
|
135
|
+
@style.setter
|
|
136
|
+
def style(self, style_or_name: _TableStyle | str | None):
|
|
137
|
+
style_id = self.part.get_style_id(style_or_name, WD_STYLE_TYPE.TABLE)
|
|
138
|
+
self._tbl.tblStyle_val = style_id
|
|
139
|
+
|
|
140
|
+
@property
|
|
141
|
+
def table(self):
|
|
142
|
+
"""Provide child objects with reference to the |Table| object they belong to,
|
|
143
|
+
without them having to know their direct parent is a |Table| object.
|
|
144
|
+
|
|
145
|
+
This is the terminus of a series of `parent._table` calls from an arbitrary
|
|
146
|
+
child through its ancestors.
|
|
147
|
+
"""
|
|
148
|
+
return self
|
|
149
|
+
|
|
150
|
+
@property
|
|
151
|
+
def table_direction(self) -> WD_TABLE_DIRECTION | None:
|
|
152
|
+
"""Member of :ref:`WdTableDirection` indicating cell-ordering direction.
|
|
153
|
+
|
|
154
|
+
For example: `WD_TABLE_DIRECTION.LTR`. |None| indicates the value is inherited
|
|
155
|
+
from the style hierarchy.
|
|
156
|
+
"""
|
|
157
|
+
return cast("WD_TABLE_DIRECTION | None", self._tbl.bidiVisual_val)
|
|
158
|
+
|
|
159
|
+
@table_direction.setter
|
|
160
|
+
def table_direction(self, value: WD_TABLE_DIRECTION | None):
|
|
161
|
+
self._element.bidiVisual_val = value
|
|
162
|
+
|
|
163
|
+
@property
|
|
164
|
+
def _cells(self) -> list[_Cell]:
|
|
165
|
+
"""A sequence of |_Cell| objects, one for each cell of the layout grid.
|
|
166
|
+
|
|
167
|
+
If the table contains a span, one or more |_Cell| object references are
|
|
168
|
+
repeated.
|
|
169
|
+
"""
|
|
170
|
+
col_count = self._column_count
|
|
171
|
+
cells: list[_Cell] = []
|
|
172
|
+
for tc in self._tbl.iter_tcs():
|
|
173
|
+
for grid_span_idx in range(tc.grid_span):
|
|
174
|
+
if tc.vMerge == ST_Merge.CONTINUE:
|
|
175
|
+
cells.append(cells[-col_count])
|
|
176
|
+
elif grid_span_idx > 0:
|
|
177
|
+
cells.append(cells[-1])
|
|
178
|
+
else:
|
|
179
|
+
cells.append(_Cell(tc, self))
|
|
180
|
+
return cells
|
|
181
|
+
|
|
182
|
+
@property
|
|
183
|
+
def _column_count(self):
|
|
184
|
+
"""The number of grid columns in this table."""
|
|
185
|
+
return self._tbl.col_count
|
|
186
|
+
|
|
187
|
+
@property
|
|
188
|
+
def _tblPr(self) -> CT_TblPr:
|
|
189
|
+
return self._tbl.tblPr
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
class _Cell(BlockItemContainer):
|
|
193
|
+
"""Table cell."""
|
|
194
|
+
|
|
195
|
+
def __init__(self, tc: CT_Tc, parent: TableParent):
|
|
196
|
+
super(_Cell, self).__init__(tc, cast("t.ProvidesStoryPart", parent))
|
|
197
|
+
self._parent = parent
|
|
198
|
+
self._tc = self._element = tc
|
|
199
|
+
|
|
200
|
+
def add_paragraph(self, text: str = "", style: str | ParagraphStyle | None = None):
|
|
201
|
+
"""Return a paragraph newly added to the end of the content in this cell.
|
|
202
|
+
|
|
203
|
+
If present, `text` is added to the paragraph in a single run. If specified, the
|
|
204
|
+
paragraph style `style` is applied. If `style` is not specified or is |None|,
|
|
205
|
+
the result is as though the 'Normal' style was applied. Note that the formatting
|
|
206
|
+
of text in a cell can be influenced by the table style. `text` can contain tab
|
|
207
|
+
(``\\t``) characters, which are converted to the appropriate XML form for a tab.
|
|
208
|
+
`text` can also include newline (``\\n``) or carriage return (``\\r``)
|
|
209
|
+
characters, each of which is converted to a line break.
|
|
210
|
+
"""
|
|
211
|
+
return super(_Cell, self).add_paragraph(text, style)
|
|
212
|
+
|
|
213
|
+
def add_table( # pyright: ignore[reportIncompatibleMethodOverride]
|
|
214
|
+
self, rows: int, cols: int
|
|
215
|
+
) -> Table:
|
|
216
|
+
"""Return a table newly added to this cell after any existing cell content.
|
|
217
|
+
|
|
218
|
+
The new table will have `rows` rows and `cols` columns.
|
|
219
|
+
|
|
220
|
+
An empty paragraph is added after the table because Word requires a paragraph
|
|
221
|
+
element as the last element in every cell.
|
|
222
|
+
"""
|
|
223
|
+
width = self.width if self.width is not None else Inches(1)
|
|
224
|
+
table = super(_Cell, self).add_table(rows, cols, width)
|
|
225
|
+
self.add_paragraph()
|
|
226
|
+
return table
|
|
227
|
+
|
|
228
|
+
@property
|
|
229
|
+
def grid_span(self) -> int:
|
|
230
|
+
"""Number of layout-grid cells this cell spans horizontally.
|
|
231
|
+
|
|
232
|
+
A "normal" cell has a grid-span of 1. A horizontally merged cell has a grid-span of 2 or
|
|
233
|
+
more.
|
|
234
|
+
"""
|
|
235
|
+
return self._tc.grid_span
|
|
236
|
+
|
|
237
|
+
def merge(self, other_cell: _Cell):
|
|
238
|
+
"""Return a merged cell created by spanning the rectangular region having this
|
|
239
|
+
cell and `other_cell` as diagonal corners.
|
|
240
|
+
|
|
241
|
+
Raises |InvalidSpanError| if the cells do not define a rectangular region.
|
|
242
|
+
"""
|
|
243
|
+
tc, tc_2 = self._tc, other_cell._tc
|
|
244
|
+
merged_tc = tc.merge(tc_2)
|
|
245
|
+
return _Cell(merged_tc, self._parent)
|
|
246
|
+
|
|
247
|
+
@property
|
|
248
|
+
def paragraphs(self):
|
|
249
|
+
"""List of paragraphs in the cell.
|
|
250
|
+
|
|
251
|
+
A table cell is required to contain at least one block-level element and end
|
|
252
|
+
with a paragraph. By default, a new cell contains a single paragraph. Read-only
|
|
253
|
+
"""
|
|
254
|
+
return super(_Cell, self).paragraphs
|
|
255
|
+
|
|
256
|
+
@property
|
|
257
|
+
def tables(self):
|
|
258
|
+
"""List of tables in the cell, in the order they appear.
|
|
259
|
+
|
|
260
|
+
Read-only.
|
|
261
|
+
"""
|
|
262
|
+
return super(_Cell, self).tables
|
|
263
|
+
|
|
264
|
+
@property
|
|
265
|
+
def text(self) -> str:
|
|
266
|
+
"""The entire contents of this cell as a string of text.
|
|
267
|
+
|
|
268
|
+
Assigning a string to this property replaces all existing content with a single
|
|
269
|
+
paragraph containing the assigned text in a single run.
|
|
270
|
+
"""
|
|
271
|
+
return "\n".join(p.text for p in self.paragraphs)
|
|
272
|
+
|
|
273
|
+
@text.setter
|
|
274
|
+
def text(self, text: str):
|
|
275
|
+
"""Write-only.
|
|
276
|
+
|
|
277
|
+
Set entire contents of cell to the string `text`. Any existing content or
|
|
278
|
+
revisions are replaced.
|
|
279
|
+
"""
|
|
280
|
+
tc = self._tc
|
|
281
|
+
tc.clear_content()
|
|
282
|
+
p = tc.add_p()
|
|
283
|
+
r = p.add_r()
|
|
284
|
+
r.text = text
|
|
285
|
+
|
|
286
|
+
@property
|
|
287
|
+
def vertical_alignment(self):
|
|
288
|
+
"""Member of :ref:`WdCellVerticalAlignment` or None.
|
|
289
|
+
|
|
290
|
+
A value of |None| indicates vertical alignment for this cell is inherited.
|
|
291
|
+
Assigning |None| causes any explicitly defined vertical alignment to be removed,
|
|
292
|
+
restoring inheritance.
|
|
293
|
+
"""
|
|
294
|
+
tcPr = self._element.tcPr
|
|
295
|
+
if tcPr is None:
|
|
296
|
+
return None
|
|
297
|
+
return tcPr.vAlign_val
|
|
298
|
+
|
|
299
|
+
@vertical_alignment.setter
|
|
300
|
+
def vertical_alignment(self, value: WD_CELL_VERTICAL_ALIGNMENT | None):
|
|
301
|
+
tcPr = self._element.get_or_add_tcPr()
|
|
302
|
+
tcPr.vAlign_val = value
|
|
303
|
+
|
|
304
|
+
@property
|
|
305
|
+
def width(self):
|
|
306
|
+
"""The width of this cell in EMU, or |None| if no explicit width is set."""
|
|
307
|
+
return self._tc.width
|
|
308
|
+
|
|
309
|
+
@width.setter
|
|
310
|
+
def width(self, value: Length):
|
|
311
|
+
self._tc.width = value
|
|
312
|
+
|
|
313
|
+
|
|
314
|
+
class _Column(Parented):
|
|
315
|
+
"""Table column."""
|
|
316
|
+
|
|
317
|
+
def __init__(self, gridCol: CT_TblGridCol, parent: TableParent):
|
|
318
|
+
super(_Column, self).__init__(parent)
|
|
319
|
+
self._parent = parent
|
|
320
|
+
self._gridCol = gridCol
|
|
321
|
+
|
|
322
|
+
@property
|
|
323
|
+
def cells(self) -> tuple[_Cell, ...]:
|
|
324
|
+
"""Sequence of |_Cell| instances corresponding to cells in this column."""
|
|
325
|
+
return tuple(self.table.column_cells(self._index))
|
|
326
|
+
|
|
327
|
+
@property
|
|
328
|
+
def table(self) -> Table:
|
|
329
|
+
"""Reference to the |Table| object this column belongs to."""
|
|
330
|
+
return self._parent.table
|
|
331
|
+
|
|
332
|
+
@property
|
|
333
|
+
def width(self) -> Length | None:
|
|
334
|
+
"""The width of this column in EMU, or |None| if no explicit width is set."""
|
|
335
|
+
return self._gridCol.w
|
|
336
|
+
|
|
337
|
+
@width.setter
|
|
338
|
+
def width(self, value: Length | None):
|
|
339
|
+
self._gridCol.w = value
|
|
340
|
+
|
|
341
|
+
@property
|
|
342
|
+
def _index(self):
|
|
343
|
+
"""Index of this column in its table, starting from zero."""
|
|
344
|
+
return self._gridCol.gridCol_idx
|
|
345
|
+
|
|
346
|
+
|
|
347
|
+
class _Columns(Parented):
|
|
348
|
+
"""Sequence of |_Column| instances corresponding to the columns in a table.
|
|
349
|
+
|
|
350
|
+
Supports ``len()``, iteration and indexed access.
|
|
351
|
+
"""
|
|
352
|
+
|
|
353
|
+
def __init__(self, tbl: CT_Tbl, parent: TableParent):
|
|
354
|
+
super(_Columns, self).__init__(parent)
|
|
355
|
+
self._parent = parent
|
|
356
|
+
self._tbl = tbl
|
|
357
|
+
|
|
358
|
+
def __getitem__(self, idx: int):
|
|
359
|
+
"""Provide indexed access, e.g. 'columns[0]'."""
|
|
360
|
+
try:
|
|
361
|
+
gridCol = self._gridCol_lst[idx]
|
|
362
|
+
except IndexError:
|
|
363
|
+
msg = "column index [%d] is out of range" % idx
|
|
364
|
+
raise IndexError(msg)
|
|
365
|
+
return _Column(gridCol, self)
|
|
366
|
+
|
|
367
|
+
def __iter__(self):
|
|
368
|
+
for gridCol in self._gridCol_lst:
|
|
369
|
+
yield _Column(gridCol, self)
|
|
370
|
+
|
|
371
|
+
def __len__(self):
|
|
372
|
+
return len(self._gridCol_lst)
|
|
373
|
+
|
|
374
|
+
@property
|
|
375
|
+
def table(self) -> Table:
|
|
376
|
+
"""Reference to the |Table| object this column collection belongs to."""
|
|
377
|
+
return self._parent.table
|
|
378
|
+
|
|
379
|
+
@property
|
|
380
|
+
def _gridCol_lst(self):
|
|
381
|
+
"""Sequence containing ``<w:gridCol>`` elements for this table, each
|
|
382
|
+
representing a table column."""
|
|
383
|
+
tblGrid = self._tbl.tblGrid
|
|
384
|
+
return tblGrid.gridCol_lst
|
|
385
|
+
|
|
386
|
+
|
|
387
|
+
class _Row(Parented):
|
|
388
|
+
"""Table row."""
|
|
389
|
+
|
|
390
|
+
def __init__(self, tr: CT_Row, parent: TableParent):
|
|
391
|
+
super(_Row, self).__init__(parent)
|
|
392
|
+
self._parent = parent
|
|
393
|
+
self._tr = self._element = tr
|
|
394
|
+
|
|
395
|
+
@property
|
|
396
|
+
def cells(self) -> tuple[_Cell, ...]:
|
|
397
|
+
"""Sequence of |_Cell| instances corresponding to cells in this row.
|
|
398
|
+
|
|
399
|
+
Note that Word allows table rows to start later than the first column and end before the
|
|
400
|
+
last column.
|
|
401
|
+
|
|
402
|
+
- Only cells actually present are included in the return value.
|
|
403
|
+
- This implies the length of this cell sequence may differ between rows of the same table.
|
|
404
|
+
- If you are reading the cells from each row to form a rectangular "matrix" data structure
|
|
405
|
+
of the table cell values, you will need to account for empty leading and/or trailing
|
|
406
|
+
layout-grid positions using `.grid_cols_before` and `.grid_cols_after`.
|
|
407
|
+
|
|
408
|
+
"""
|
|
409
|
+
|
|
410
|
+
def iter_tc_cells(tc: CT_Tc) -> Iterator[_Cell]:
|
|
411
|
+
"""Generate a cell object for each layout-grid cell in `tc`.
|
|
412
|
+
|
|
413
|
+
In particular, a `<w:tc>` element with a horizontal "span" with generate the same cell
|
|
414
|
+
multiple times, one for each grid-cell being spanned. This approximates a row in a
|
|
415
|
+
"uniform" table, where each row has a cell for each column in the table.
|
|
416
|
+
"""
|
|
417
|
+
# -- a cell comprising the second or later row of a vertical span is indicated by
|
|
418
|
+
# -- tc.vMerge="continue" (the default value of the `w:vMerge` attribute, when it is
|
|
419
|
+
# -- present in the XML). The `w:tc` element at the same grid-offset in the prior row
|
|
420
|
+
# -- is guaranteed to be the same width (gridSpan). So we can delegate content
|
|
421
|
+
# -- discovery to that prior-row `w:tc` element (recursively) until we arrive at the
|
|
422
|
+
# -- "root" cell -- for the vertical span.
|
|
423
|
+
if tc.vMerge == "continue":
|
|
424
|
+
yield from iter_tc_cells(tc._tc_above) # pyright: ignore[reportPrivateUsage]
|
|
425
|
+
return
|
|
426
|
+
|
|
427
|
+
# -- Otherwise, vMerge is either "restart" or None, meaning this `tc` holds the actual
|
|
428
|
+
# -- content of the cell (whether it is vertically merged or not).
|
|
429
|
+
cell = _Cell(tc, self.table)
|
|
430
|
+
for _ in range(tc.grid_span):
|
|
431
|
+
yield cell
|
|
432
|
+
|
|
433
|
+
def _iter_row_cells() -> Iterator[_Cell]:
|
|
434
|
+
"""Generate `_Cell` instance for each populated layout-grid cell in this row."""
|
|
435
|
+
for tc in self._tr.tc_lst:
|
|
436
|
+
yield from iter_tc_cells(tc)
|
|
437
|
+
|
|
438
|
+
return tuple(_iter_row_cells())
|
|
439
|
+
|
|
440
|
+
@property
|
|
441
|
+
def grid_cols_after(self) -> int:
|
|
442
|
+
"""Count of unpopulated grid-columns after the last cell in this row.
|
|
443
|
+
|
|
444
|
+
Word allows a row to "end early", meaning that one or more cells are not present at the
|
|
445
|
+
end of that row.
|
|
446
|
+
|
|
447
|
+
Note these are not simply "empty" cells. The renderer reads this value and "skips" this
|
|
448
|
+
many columns after drawing the last cell.
|
|
449
|
+
|
|
450
|
+
Note this also implies that not all rows are guaranteed to have the same number of cells,
|
|
451
|
+
e.g. `_Row.cells` could have length `n` for one row and `n - m` for the next row in the same
|
|
452
|
+
table. Visually this appears as a column (at the beginning or end, not in the middle) with
|
|
453
|
+
one or more cells missing.
|
|
454
|
+
"""
|
|
455
|
+
return self._tr.grid_after
|
|
456
|
+
|
|
457
|
+
@property
|
|
458
|
+
def grid_cols_before(self) -> int:
|
|
459
|
+
"""Count of unpopulated grid-columns before the first cell in this row.
|
|
460
|
+
|
|
461
|
+
Word allows a row to "start late", meaning that one or more cells are not present at the
|
|
462
|
+
beginning of that row.
|
|
463
|
+
|
|
464
|
+
Note these are not simply "empty" cells. The renderer reads this value and skips forward to
|
|
465
|
+
the table layout-grid position of the first cell in this row; the renderer "skips" this many
|
|
466
|
+
columns before drawing the first cell.
|
|
467
|
+
|
|
468
|
+
Note this also implies that not all rows are guaranteed to have the same number of cells,
|
|
469
|
+
e.g. `_Row.cells` could have length `n` for one row and `n - m` for the next row in the same
|
|
470
|
+
table.
|
|
471
|
+
"""
|
|
472
|
+
return self._tr.grid_before
|
|
473
|
+
|
|
474
|
+
@property
|
|
475
|
+
def height(self) -> Length | None:
|
|
476
|
+
"""Return a |Length| object representing the height of this cell, or |None| if
|
|
477
|
+
no explicit height is set."""
|
|
478
|
+
return self._tr.trHeight_val
|
|
479
|
+
|
|
480
|
+
@height.setter
|
|
481
|
+
def height(self, value: Length | None):
|
|
482
|
+
self._tr.trHeight_val = value
|
|
483
|
+
|
|
484
|
+
@property
|
|
485
|
+
def height_rule(self) -> WD_ROW_HEIGHT_RULE | None:
|
|
486
|
+
"""Return the height rule of this cell as a member of the :ref:`WdRowHeightRule`.
|
|
487
|
+
|
|
488
|
+
This value is |None| if no explicit height_rule is set.
|
|
489
|
+
"""
|
|
490
|
+
return self._tr.trHeight_hRule
|
|
491
|
+
|
|
492
|
+
@height_rule.setter
|
|
493
|
+
def height_rule(self, value: WD_ROW_HEIGHT_RULE | None):
|
|
494
|
+
self._tr.trHeight_hRule = value
|
|
495
|
+
|
|
496
|
+
@property
|
|
497
|
+
def table(self) -> Table:
|
|
498
|
+
"""Reference to the |Table| object this row belongs to."""
|
|
499
|
+
return self._parent.table
|
|
500
|
+
|
|
501
|
+
@property
|
|
502
|
+
def _index(self) -> int:
|
|
503
|
+
"""Index of this row in its table, starting from zero."""
|
|
504
|
+
return self._tr.tr_idx
|
|
505
|
+
|
|
506
|
+
|
|
507
|
+
class _Rows(Parented):
|
|
508
|
+
"""Sequence of |_Row| objects corresponding to the rows in a table.
|
|
509
|
+
|
|
510
|
+
Supports ``len()``, iteration, indexed access, and slicing.
|
|
511
|
+
"""
|
|
512
|
+
|
|
513
|
+
def __init__(self, tbl: CT_Tbl, parent: TableParent):
|
|
514
|
+
super(_Rows, self).__init__(parent)
|
|
515
|
+
self._parent = parent
|
|
516
|
+
self._tbl = tbl
|
|
517
|
+
|
|
518
|
+
@overload
|
|
519
|
+
def __getitem__(self, idx: int) -> _Row: ...
|
|
520
|
+
|
|
521
|
+
@overload
|
|
522
|
+
def __getitem__(self, idx: slice) -> list[_Row]: ...
|
|
523
|
+
|
|
524
|
+
def __getitem__(self, idx: int | slice) -> _Row | list[_Row]:
|
|
525
|
+
"""Provide indexed access, (e.g. `rows[0]` or `rows[1:3]`)"""
|
|
526
|
+
return list(self)[idx]
|
|
527
|
+
|
|
528
|
+
def __iter__(self):
|
|
529
|
+
return (_Row(tr, self) for tr in self._tbl.tr_lst)
|
|
530
|
+
|
|
531
|
+
def __len__(self):
|
|
532
|
+
return len(self._tbl.tr_lst)
|
|
533
|
+
|
|
534
|
+
@property
|
|
535
|
+
def table(self) -> Table:
|
|
536
|
+
"""Reference to the |Table| object this row collection belongs to."""
|
|
537
|
+
return self._parent.table
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
|
2
|
+
<w:comments
|
|
3
|
+
xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"
|
|
4
|
+
xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture"
|
|
5
|
+
xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"
|
|
6
|
+
xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
|
|
7
|
+
xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing"
|
|
8
|
+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
|
9
|
+
xmlns:a14="http://schemas.microsoft.com/office/drawing/2010/main"
|
|
10
|
+
xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml"
|
|
11
|
+
xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas"
|
|
12
|
+
/>
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
<?xml version='1.0' encoding='UTF-8' standalone='yes'?>
|
|
2
|
+
<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">
|
|
3
|
+
<Default Extension="xml" ContentType="application/xml"/>
|
|
4
|
+
<Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/>
|
|
5
|
+
<Default Extension="jpeg" ContentType="image/jpeg"/>
|
|
6
|
+
<Override PartName="/word/document.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml"/>
|
|
7
|
+
<Override PartName="/customXml/itemProps1.xml" ContentType="application/vnd.openxmlformats-officedocument.customXmlProperties+xml"/>
|
|
8
|
+
<Override PartName="/word/numbering.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.numbering+xml"/>
|
|
9
|
+
<Override PartName="/word/styles.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.styles+xml"/>
|
|
10
|
+
<Override PartName="/word/stylesWithEffects.xml" ContentType="application/vnd.ms-word.stylesWithEffects+xml"/>
|
|
11
|
+
<Override PartName="/word/settings.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.settings+xml"/>
|
|
12
|
+
<Override PartName="/word/webSettings.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.webSettings+xml"/>
|
|
13
|
+
<Override PartName="/word/fontTable.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.fontTable+xml"/>
|
|
14
|
+
<Override PartName="/word/theme/theme1.xml" ContentType="application/vnd.openxmlformats-officedocument.theme+xml"/>
|
|
15
|
+
<Override PartName="/docProps/core.xml" ContentType="application/vnd.openxmlformats-package.core-properties+xml"/>
|
|
16
|
+
<Override PartName="/docProps/app.xml" ContentType="application/vnd.openxmlformats-officedocument.extended-properties+xml"/>
|
|
17
|
+
</Types>
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
<?xml version='1.0' encoding='UTF-8' standalone='yes'?>
|
|
2
|
+
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
|
|
3
|
+
<Relationship Id="rId3" Type="http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties" Target="docProps/core.xml"/>
|
|
4
|
+
<Relationship Id="rId4" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties" Target="docProps/app.xml"/>
|
|
5
|
+
<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="word/document.xml"/>
|
|
6
|
+
<Relationship Id="rId2" Type="http://schemas.openxmlformats.org/package/2006/relationships/metadata/thumbnail" Target="docProps/thumbnail.jpeg"/>
|
|
7
|
+
</Relationships>
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
<?xml version='1.0' encoding='UTF-8' standalone='yes'?>
|
|
2
|
+
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
|
|
3
|
+
<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/customXmlProps" Target="itemProps1.xml"/>
|
|
4
|
+
</Relationships>
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
<?xml version='1.0' encoding='UTF-8' standalone='yes'?>
|
|
2
|
+
<ds:datastoreItem xmlns:ds="http://schemas.openxmlformats.org/officeDocument/2006/customXml" ds:itemID="{EF278816-EC6F-A645-907D-7F25AECB1D4A}">
|
|
3
|
+
<ds:schemaRefs>
|
|
4
|
+
<ds:schemaRef ds:uri="http://schemas.openxmlformats.org/officeDocument/2006/bibliography"/>
|
|
5
|
+
</ds:schemaRefs>
|
|
6
|
+
</ds:datastoreItem>
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
<?xml version='1.0' encoding='UTF-8' standalone='yes'?>
|
|
2
|
+
<Properties xmlns="http://schemas.openxmlformats.org/officeDocument/2006/extended-properties" xmlns:vt="http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes">
|
|
3
|
+
<Template>Normal.dotm</Template>
|
|
4
|
+
<TotalTime>0</TotalTime>
|
|
5
|
+
<Pages>1</Pages>
|
|
6
|
+
<Words>0</Words>
|
|
7
|
+
<Characters>0</Characters>
|
|
8
|
+
<Application>Microsoft Macintosh Word</Application>
|
|
9
|
+
<DocSecurity>0</DocSecurity>
|
|
10
|
+
<Lines>0</Lines>
|
|
11
|
+
<Paragraphs>0</Paragraphs>
|
|
12
|
+
<ScaleCrop>false</ScaleCrop>
|
|
13
|
+
<HeadingPairs>
|
|
14
|
+
<vt:vector size="2" baseType="variant">
|
|
15
|
+
<vt:variant>
|
|
16
|
+
<vt:lpstr>Title</vt:lpstr>
|
|
17
|
+
</vt:variant>
|
|
18
|
+
<vt:variant>
|
|
19
|
+
<vt:i4>1</vt:i4>
|
|
20
|
+
</vt:variant>
|
|
21
|
+
</vt:vector>
|
|
22
|
+
</HeadingPairs>
|
|
23
|
+
<TitlesOfParts>
|
|
24
|
+
<vt:vector size="1" baseType="lpstr">
|
|
25
|
+
<vt:lpstr/>
|
|
26
|
+
</vt:vector>
|
|
27
|
+
</TitlesOfParts>
|
|
28
|
+
<Manager/>
|
|
29
|
+
<Company/>
|
|
30
|
+
<LinksUpToDate>false</LinksUpToDate>
|
|
31
|
+
<CharactersWithSpaces>0</CharactersWithSpaces>
|
|
32
|
+
<SharedDoc>false</SharedDoc>
|
|
33
|
+
<HyperlinkBase/>
|
|
34
|
+
<HyperlinksChanged>false</HyperlinksChanged>
|
|
35
|
+
<AppVersion>14.0000</AppVersion>
|
|
36
|
+
</Properties>
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
<?xml version='1.0' encoding='UTF-8' standalone='yes'?>
|
|
2
|
+
<cp:coreProperties xmlns:cp="http://schemas.openxmlformats.org/package/2006/metadata/core-properties" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:dcmitype="http://purl.org/dc/dcmitype/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
|
3
|
+
<dc:title/>
|
|
4
|
+
<dc:subject/>
|
|
5
|
+
<dc:creator>python-docx</dc:creator>
|
|
6
|
+
<cp:keywords/>
|
|
7
|
+
<dc:description>generated by python-docx</dc:description>
|
|
8
|
+
<cp:lastModifiedBy/>
|
|
9
|
+
<cp:revision>1</cp:revision>
|
|
10
|
+
<dcterms:created xsi:type="dcterms:W3CDTF">2013-12-23T23:15:00Z</dcterms:created>
|
|
11
|
+
<dcterms:modified xsi:type="dcterms:W3CDTF">2013-12-23T23:15:00Z</dcterms:modified>
|
|
12
|
+
<cp:category/>
|
|
13
|
+
</cp:coreProperties>
|
|
Binary file
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
<?xml version='1.0' encoding='UTF-8' standalone='yes'?>
|
|
2
|
+
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
|
|
3
|
+
<Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="styles.xml"/>
|
|
4
|
+
<Relationship Id="rId4" Type="http://schemas.microsoft.com/office/2007/relationships/stylesWithEffects" Target="stylesWithEffects.xml"/>
|
|
5
|
+
<Relationship Id="rId5" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings" Target="settings.xml"/>
|
|
6
|
+
<Relationship Id="rId6" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/webSettings" Target="webSettings.xml"/>
|
|
7
|
+
<Relationship Id="rId7" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/fontTable" Target="fontTable.xml"/>
|
|
8
|
+
<Relationship Id="rId8" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme" Target="theme/theme1.xml"/>
|
|
9
|
+
<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/customXml" Target="../customXml/item1.xml"/>
|
|
10
|
+
<Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/numbering" Target="numbering.xml"/>
|
|
11
|
+
</Relationships>
|