table
table
¶
Custom element classes for tables.
CT_Border
¶
Bases: BaseOxmlElement
A single border edge, e.g. w:tblBorders/w:top.
The same complex type serves every edge of w:tblBorders, w:tcBorders and
w:pBdr.
_CT_BordersBase
¶
Bases: BaseOxmlElement
Common behavior of the w:tblBorders and w:tcBorders elements.
Each holds an optional CT_Border child per edge and they differ only in which
edges they admit; edges names those, in schema order.
get_or_add_border
¶
get_or_add_border(edge: str) -> CT_Border
The w:{edge} child element, newly added in schema order if not present.
A newly added border is given w:val="single", since w:val is required and a
border element without a line style is not valid.
Source code in src/docx/oxml/table.py
remove_border
¶
Remove the w:{edge} child element; does nothing when it is not present.
CT_TblBorders
¶
CT_TcBorders
¶
Bases: _CT_BordersBase
w:tcBorders element, the set of border edges of a table cell.
Adds the two diagonals to the edges a table admits.
CT_Height
¶
CT_Row
¶
Bases: BaseOxmlElement
<w:tr> element.
grid_after
property
¶
The number of unpopulated layout-grid cells at the end of this row.
grid_before
property
¶
The number of unpopulated layout-grid cells at the start of this row.
trHeight_hRule
property
writable
¶
trHeight_hRule: WD_ROW_HEIGHT_RULE | None
The value of ./w:trPr/w:trHeight/@w:hRule, or None if not present.
trHeight_val
property
writable
¶
Return the value of w:trPr/w:trHeight@w:val, or None if not present.
cantSplit_val
property
writable
¶
Value of w:trPr/w:cantSplit@w:val, or None if not present.
tblHeader_val
property
writable
¶
Value of w:trPr/w:tblHeader/@w:val, or None if not present.
hidden_val
property
writable
¶
Value of w:trPr/w:hidden/@w:val, or None if not present.
alignment
property
writable
¶
alignment: WD_TABLE_ALIGNMENT | None
Value of w:trPr/w:jc/@w:val, or None if not present.
cell_spacing
property
writable
¶
cell_spacing: Length | None
Value of w:trPr/w:tblCellSpacing, or None if not present.
width_after
property
writable
¶
width_after: Length | None
Value of w:trPr/w:wAfter, or None if not present.
width_before
property
writable
¶
width_before: Length | None
Value of w:trPr/w:wBefore, or None if not present.
grid_width
¶
The count of layout-grid columns this row occupies.
Includes the grid positions this row leaves unpopulated at either end.
Source code in src/docx/oxml/table.py
delete_grid_column
¶
Remove this row's occupancy of layout-grid column grid_offset.
A cell that starts at grid_offset and spans no further is removed; one that
spans this column and others is narrowed by one, so the rest of its span
survives. A row that does not populate the column is left alone, and its
w:gridBefore or w:gridAfter adjusted when the removed column falls inside
the unpopulated run.
Source code in src/docx/oxml/table.py
transfer_vertical_spans_to_row_below
¶
Make the row below own any vertical span that starts in this row.
Called before deleting this row: a continuation cell whose origin disappears would otherwise be left referring to nothing. The cell below becomes the origin, keeping the content and the remainder of the span, which is what Word does.
Source code in src/docx/oxml/table.py
tc_covering_grid_offset
¶
tc_covering_grid_offset(grid_offset: int) -> CT_Tc
The w:tc element in this tr occupying layout-grid column grid_offset.
Unlike .tc_at_grid_offset(), a horizontally merged cell is returned for every
grid column it spans, not only for the one it starts at.
Raises ValueError when this row does not populate grid_offset, which happens
when the row starts late or ends early.
Source code in src/docx/oxml/table.py
tc_at_grid_offset
¶
tc_at_grid_offset(grid_offset: int) -> CT_Tc
The tc element in this tr at exact grid offset.
Raises ValueError when this w:tr contains no w:tc with exact starting grid_offset.
Source code in src/docx/oxml/table.py
CT_Tbl
¶
Bases: BaseOxmlElement
<w:tbl> element.
tblGrid
property
¶
tblGrid: CT_TblGrid
The w:tblGrid child of this table, synthesized when absent.
w:tblGrid is required by the schema, but Word opens a table without one by
reconstructing the grid from the row contents, and enough generators emit such a
table that refusing to read one is harsher than the situation warrants.
Note the synthesized element is inserted into the tree, so saving a document
read this way repairs the table. This is deliberate; the alternative is an
add_column() that silently does nothing and a save that writes the invalid
table straight back out.
A w:tblGrid that is present but has fewer w:gridCol children than the widest
row is left alone. Nothing in this library depends on the grid to locate a cell,
so the short grid affects only len(table.columns), which reports what the
document actually says.
bidiVisual_val
property
writable
¶
Value of ./w:tblPr/w:bidiVisual/@w:val or None if not present.
Controls whether table cells are displayed right-to-left or left-to-right.
tblStyle_val
property
writable
¶
w:tblPr/w:tblStyle/@w:val (a table style id) or None if not present.
tr_at_idx
¶
tr_at_idx(idx: int) -> CT_Row
The w:tr child of this table at idx, counting from zero.
Raises IndexError when idx is out of range. Locating the row this way avoids
materializing the full row list, which is what makes reading a table row by row
cost time proportional to its size rather than to its square.
Source code in src/docx/oxml/table.py
iter_tcs
¶
Generate each of the w:tc elements in this table, left to right and top to
bottom.
Each cell in the first row is generated, followed by each cell in the second row, etc.
Source code in src/docx/oxml/table.py
new_tbl
classmethod
¶
Return a new w:tbl element having rows rows and cols columns.
width is distributed evenly between the columns.
Source code in src/docx/oxml/table.py
CT_TblGrid
¶
Bases: BaseOxmlElement
w:tblGrid element.
Child of w:tbl, holds `w:gridCol> elements that define column count, width, etc.
CT_TblGridCol
¶
Bases: BaseOxmlElement
w:gridCol element, child of w:tblGrid, defines a table column.
gridCol_idx
property
¶
Index of this w:gridCol element within its parent w:tblGrid element.
CT_TblLayoutType
¶
Bases: BaseOxmlElement
w:tblLayout element.
Specifies whether column widths are fixed or can be automatically adjusted based on content.
CT_TblPr
¶
Bases: BaseOxmlElement
<w:tblPr> element, child of <w:tbl>, holds child elements that define
table properties such as style and borders.
alignment
property
writable
¶
alignment: WD_TABLE_ALIGNMENT | None
Horizontal alignment of table, None if ./w:jc is not present.
autofit
property
writable
¶
False when there is a w:tblLayout child with @w:type="fixed".
Otherwise True.
style
property
writable
¶
Return the value of the val attribute of the <w:tblStyle> child or
None if not present.
CT_TblPrEx
¶
Bases: BaseOxmlElement
w:tblPrEx element, exceptions to table-properties.
Applied at a lower level, like a w:tr to modify the appearance. Possibly used when
two tables are merged. For more see:
http://officeopenxml.com/WPtablePropertyExceptions.php
CT_TblWidth
¶
Bases: BaseOxmlElement
Used for w:tblW and w:tcW and others, specifies a table-related width.
CT_TblCellMar
¶
Bases: BaseOxmlElement
w:tblCellMar element, the default cell margins for a whole table.
An absent edge means the value is inherited from the table style.
Each edge is a CT_TblWidth in the schema, but the edge tag names are shared with
w:tblBorders and lxml resolves an element class by tag name alone, so these
children arrive typed as CT_Border — see the note above the registrations in
oxml/__init__.py. The w:w and w:type attributes are therefore read and written
directly here rather than through element-class attributes.
get_margin
¶
get_margin(edge: str) -> Length | None
The width of the w:{edge} child, or None when that edge is absent.
Also None when the edge is present but expressed in a unit other than dxa,
which is the only one Word writes here.
Source code in src/docx/oxml/table.py
set_margin
¶
set_margin(edge: str, value: Length | None) -> None
Set the w:{edge} child to value, removing it when value is None.
Source code in src/docx/oxml/table.py
CT_TblLook
¶
Bases: BaseOxmlElement
w:tblLook element, selecting which parts of the table style apply.
All attributes, no children. w:val is the legacy bitmask carrying the same six
flags as the named attributes; Word writes both and keeps them in step, so
docx.table._TableLook rewrites it whenever a flag changes.
update_val
¶
Rewrite @w:val from the six named attributes.
Word reads the named attributes, but some older consumers read only the bitmask,
so the two are kept in step rather than letting @w:val go stale.
Source code in src/docx/oxml/table.py
CT_Tc
¶
Bases: BaseOxmlElement
w:tc table cell element.
bottom
property
¶
The row index that marks the bottom extent of the vertical span of this cell.
This is one greater than the index of the bottom-most row of the span, similar to how a slice of the cell's rows would be specified.
The span is measured by following continuation cells downward, without requiring
this cell to carry w:vMerge of "restart". That is what the schema calls for,
but a merge whose origin cell simply omits w:vMerge is common from other
generators and renders as a merge in Word.
grid_offset
property
¶
Starting offset of tc in the layout-grid columns of its table.
A cell in the leftmost grid-column has offset 0.
grid_span
property
writable
¶
The integer number of columns this cell spans.
Determined by ./w:tcPr/w:gridSpan/@val, it defaults to 1.
inner_content_elements
property
¶
Generate all w:p and w:tbl elements in this table cell.
Elements appear in document order. Content inside a w:sdt (content control)
wrapper is included; content shaded by nesting in a w:ins or other wrapper is
not.
right
property
¶
The grid column index that marks the right-side extent of the horizontal span of this cell.
This is one greater than the index of the right-most column of the span, similar to how a slice of the cell's columns would be specified.
top_tc
property
¶
top_tc: CT_Tc
The w:tc element holding the content of this cell's vertical span.
This is this element itself unless it is a continuation cell (w:vMerge of
"continue"), in which case it is the cell the span starts at.
vMerge
property
writable
¶
Value of ./w:tcPr/w:vMerge/@val, None if w:vMerge is not present.
width
property
writable
¶
width: Length | None
EMU length represented in ./w:tcPr/w:tcW or None if not present.
clear_content
¶
Remove all content elements, preserving w:tcPr element if present.
Note that this leaves the w:tc element in an invalid state because it doesn't
contain at least one block-level element. It's up to the caller to add a
w:pchild element as the last content element.
Source code in src/docx/oxml/table.py
iter_block_items
¶
Generate a reference to each of the block-level content elements in this cell, in the order they appear.
Source code in src/docx/oxml/table.py
merge
¶
Return top-left w:tc element of a new span.
Span is formed by merging the rectangular region defined by using this tc
element and other_tc as diagonal corners.
Source code in src/docx/oxml/table.py
CT_TcPr
¶
Bases: BaseOxmlElement
<w:tcPr> element, defining table cell properties.
textDirection_val
property
writable
¶
textDirection_val: WD_TEXT_DIRECTION | None
Value of ./w:textDirection/@w:val, or None if the element is absent.
grid_span
property
writable
¶
The integer number of columns this cell spans.
Determined by ./w:gridSpan/@val, it defaults to 1.
vAlign_val
property
writable
¶
Value of w:val attribute on w:vAlign child.
Value is None if w:vAlign child is not present. The w:val attribute on
w:vAlign is required.
vMerge_val
property
writable
¶
The value of the ./w:vMerge/@val attribute, or None if the w:vMerge element
is not present.
CT_TrPr
¶
Bases: BaseOxmlElement
<w:trPr> element, defining table row properties.
alignment
property
writable
¶
alignment: WD_TABLE_ALIGNMENT | None
Value of ./w:jc/@w:val, or None if the element is absent.
cantSplit_val
property
writable
¶
Value of ./w:cantSplit/@w:val, or None if the element is absent.
hidden_val
property
writable
¶
Value of ./w:hidden/@w:val, or None if the element is absent.
tblHeader_val
property
writable
¶
Value of ./w:tblHeader/@w:val, or None if the element is absent.
cell_spacing
property
writable
¶
cell_spacing: Length | None
Value of ./w:tblCellSpacing, or None if the element is absent.
width_after
property
writable
¶
width_after: Length | None
Value of ./w:wAfter, or None if the element is absent.
width_before
property
writable
¶
width_before: Length | None
Value of ./w:wBefore, or None if the element is absent.
grid_after
property
¶
The number of unpopulated layout-grid cells at the end of this row.
grid_before
property
¶
The number of unpopulated layout-grid cells at the start of this row.
trHeight_hRule
property
writable
¶
trHeight_hRule: WD_ROW_HEIGHT_RULE | None
Return the value of w:trHeight@w:hRule, or None if not present.
trHeight_val
property
writable
¶
Return the value of w:trHeight@w:val, or None if not present.