MCPcopy Index your code
hub / github.com/python-openxml/python-docx / Table

Class Table

src/docx/table.py:29–189  ·  view source on GitHub ↗

Proxy class for a WordprocessingML `` `` element.

Source from the content-addressed store, hash-verified

27
28
29class 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.

Calls

no outgoing calls

Used in the wild real call sites across dependent graphs

searching dependent graphs…