table
table
¶
The Table object and related proxy classes.
_TableBorders
¶
_TableBorders(tbl: CT_Tbl)
_TableLook
¶
_TableLook(tbl: CT_Tbl)
Which parts of the table style apply to a table, table.look.
w:tblLook is what tells Word whether the first row is a header row, whether the
first or last column is emphasised, and whether row or column banding is on. Without
it a styled table looks nothing like the style preview in Word.
Each flag is tri-state: None means the attribute is absent and Word applies its own
default (off for every flag). The two banding flags are stored inverted in the XML,
as w:noHBand and w:noVBand; that inversion lives here so the oxml layer stays
faithful to the attribute names.
Word writes the six named attributes and the equivalent legacy bitmask in
@w:val, and keeps them in step. Setting any flag through this proxy rewrites
@w:val to match, because some older consumers read only the bitmask.
first_row
property
writable
¶
True when the table style's first-row (header) formatting applies.
last_row
property
writable
¶
True when the table style's last-row (total) formatting applies.
first_column
property
writable
¶
True when the table style's first-column formatting applies.
last_column
property
writable
¶
True when the table style's last-column formatting applies.
horizontal_banding
property
writable
¶
True when the table style's row banding applies.
Stored inverted, as w:noHBand.
vertical_banding
property
writable
¶
True when the table style's column banding applies.
Stored inverted, as w:noVBand.
_TableCellMargins
¶
_TableCellMargins(tbl: CT_Tbl)
The default cell margins of a table, table.cell_margins.
These are the padding Word applies inside every cell of the table that does not
override them. An edge reads None when the table sets no value for it, in which
case the table style's value applies.
The start and end edges are the logical (writing-direction) synonyms of left
and right. Word writes left and right; both are exposed because documents from
other producers use the newer pair.
clear
¶
Table
¶
Table(tbl: CT_Tbl, parent: ProvidesStoryPart)
Bases: StoryChild
Proxy class for a WordprocessingML <w:tbl> element.
Source code in src/docx/table.py
alignment
property
writable
¶
alignment: WD_TABLE_ALIGNMENT | None
Read/write.
A member of WdRowAlignment or None, specifying the positioning of this
table between the page margins. None if no setting is specified, causing the
effective value to be inherited from the style hierarchy.
autofit
property
writable
¶
True if column widths can be automatically adjusted to improve the fit of
cell contents.
False if table layout is fixed. Column widths are adjusted in either case if
total column width exceeds page width. Read/write boolean.
description
property
writable
¶
Alternative-text description for this table, or None if not set.
Assigning None removes the description. This value is stored in the
w:tblDescription table-property element and is used by assistive
technologies.
indent
property
writable
¶
indent: Length | None
Indentation of this table from the margin, or None if not set.
This is w:tblInd. Assigning None removes it.
width
property
writable
¶
The preferred width of this table.
A Length for an absolute width, a Pct for a percentage of the text column,
and None when the width is auto — Word sizing the table to its content —
or no w:tblW is present at all.
A percentage table reflows with the page margins where one built from absolute
column widths does not, so table.width = Pct(100) is not the same as setting
the column widths to add up:
Note this is the preferred width: Word may widen a table whose content does
not fit, and a table with autofit on will do so routinely.
style
property
writable
¶
style: _TableStyle | None
_TableStyle object representing the style applied to this table.
Read/write. The default table style for the document (often Normal Table) is
returned if the table has no directly-applied style. Assigning None to this
property removes any directly-applied table style causing it to inherit the
default table style of the document.
Note that the style name of a table style differs slightly from that displayed
in the user interface; a hyphen, if it appears, must be removed. For example,
Light Shading - Accent 1 becomes Light Shading Accent 1.
table
property
¶
table_direction
property
writable
¶
table_direction: WD_TABLE_DIRECTION | None
Member of WdTableDirection indicating cell-ordering direction.
For example: WD_TABLE_DIRECTION.LTR. None indicates the value is inherited
from the style hierarchy.
title
property
writable
¶
Alternative-text title for this table, or None if not set.
Assigning None removes the title. This value is stored in the
w:tblCaption table-property element and is used by assistive technologies.
add_column
¶
add_column(width: Length)
Return a _Column object of width, newly added rightmost to the table.
Source code in src/docx/table.py
add_row
¶
Return a _Row instance, newly added bottom-most to the table.
Source code in src/docx/table.py
borders
¶
borders() -> _TableBorders
The border edges of this table, as a mapping keyed by edge name:
These are the borders applied to the table as a whole; insideH and insideV
set the horizontal and vertical borders between its cells. A border set on an
individual cell through cell.borders takes precedence over the table border
at that edge.
Source code in src/docx/table.py
cell
¶
cell(row_idx: int, col_idx: int) -> _Cell
_Cell at row_idx, col_idx intersection.
(0, 0) is the top, left-most cell. Negative indices count back from the end, as for a sequence.
Raises IndexError if row_idx is out of range, or if the row does not occupy
layout-grid column col_idx — Word allows a row to start late or end early.
The target cell is located directly, without materializing the whole layout grid, so reading a table cell-by-cell costs time proportional to the number of cells rather than to its square.
Source code in src/docx/table.py
column_cells
¶
column_cells(column_idx: int) -> list[_Cell]
Sequence of cells in the column at column_idx in this table.
A row that does not occupy column_idx, because it starts late or ends early,
contributes no cell.
Source code in src/docx/table.py
cell_margins
¶
cell_margins() -> _TableCellMargins
The default cell margins for every cell of this table:
An edge reads None when the table sets no value for it, in which case the
table style's margin applies. Assigning None removes the override.
Source code in src/docx/table.py
look
¶
look() -> _TableLook
Which parts of the table style apply to this table:
Applying a table style without setting these produces a table that looks nothing like the style preview in Word.
Source code in src/docx/table.py
copy_to
¶
copy_to(
container: BlockItemContainer | Document,
*,
before: Paragraph | Table | None = None,
after: Paragraph | Table | None = None,
missing_style: str = "copy",
) -> Table
Return a copy of this table, newly placed in container:
See Paragraph.copy_to for what is repaired on the way — relationships, drawing ids, bookmarks, and, for a copy into another document, styles and numbering.
Source code in src/docx/table.py
delete
¶
Remove this table from the document.
Relationships referenced only from inside the table are dropped, and any range
marker left unmatched is removed, as for Paragraph.delete().
Source code in src/docx/table.py
columns
¶
_Cell
¶
_Cell(tc: CT_Tc, parent: TableParent)
Bases: BlockItemContainer
Table cell.
Source code in src/docx/table.py
column_index
property
¶
Index of the left-most layout-grid column this cell occupies.
Together with .row_index this gives the origin of the cell, which is what
tells a repeat of a merged cell apart from a cell in its own right:
for row_idx, row in enumerate(table.rows):
for col_idx, cell in enumerate(row.cells):
if (cell.row_index, cell.column_index) != (row_idx, col_idx):
continue # -- already emitted, this is part of a merged cell --
emit(cell.text, rowspan=cell.span_height, colspan=cell.grid_span)
Note this is a layout-grid column index, so it accounts for the grid positions a
row leaves unpopulated at its start; see _Row.grid_cols_before.
grid_span
property
¶
Number of layout-grid cells this cell spans horizontally.
A "normal" cell has a grid-span of 1. A horizontally merged cell has a grid-span of 2 or more.
is_merged
property
¶
True when this cell spans more than one layout-grid cell.
Horizontally, vertically, or both.
paragraphs
property
¶
List of paragraphs in the cell.
A table cell is required to contain at least one block-level element and end with a paragraph. By default, a new cell contains a single paragraph. Read-only
row_index
property
¶
Index of the top-most row this cell occupies.
For a vertically merged cell this is the row the merge starts at, not the row
the cell was reached through. See .column_index for how the pair is used.
span
property
¶
The extent of this cell as (rows, columns).
(1, 1) for an unmerged cell.
span_height
property
¶
Number of rows this cell spans vertically.
An unmerged cell has a span-height of 1; a vertically merged cell has 2 or more.
This is the read-side counterpart of .grid_span, and the two together describe
a merge completely, including the combined case of a cell that is merged in both
directions.
A merge is measured by following its continuation cells, so a document whose
origin cell omits w:vMerge — legal in practice and common from generators
other than Word — reports the same extent Word renders.
text
property
writable
¶
The entire contents of this cell as a string of text.
Assigning a string to this property replaces all existing content with a single paragraph containing the assigned text in a single run.
text_direction
property
writable
¶
text_direction: WD_TEXT_DIRECTION | None
vertical_alignment
property
writable
¶
Member of WdCellVerticalAlignment or None.
A value of None indicates vertical alignment for this cell is inherited.
Assigning None causes any explicitly defined vertical alignment to be removed,
restoring inheritance.
add_paragraph
¶
add_paragraph(
text: str = "",
style: str | ParagraphStyle | None = None,
)
Return a paragraph newly added to the end of the content in this cell.
If present, text is added to the paragraph in a single run. If specified, the
paragraph style style is applied. If style is not specified or is None,
the result is as though the 'Normal' style was applied. Note that the formatting
of text in a cell can be influenced by the table style. text can contain tab
(\t) characters, which are converted to the appropriate XML form for a tab.
text can also include newline (\n) or carriage return (\r)
characters, each of which is converted to a line break.
Source code in src/docx/table.py
add_table
¶
add_table(
rows: int,
cols: int,
*,
title: str | None = None,
description: str | None = None,
) -> Table
Return a table newly added to this cell after any existing cell content.
The new table will have rows rows and cols columns.
An empty paragraph is added after the table because Word requires a paragraph element as the last element in every cell.
description is the table's alternative text and title the separate,
caption-like field Word writes alongside it. Both are omitted from the XML when
None.
Source code in src/docx/table.py
add_caption
¶
add_caption(
label: str,
text: str = "",
*,
style: str | None = "Caption",
separator: str = " ",
restart_at_heading_level: int | None = None,
before: Paragraph | None = None,
) -> Caption
Add a numbered, cross-referenceable caption to this cell and return it.
See Document.add_caption.
Source code in src/docx/table.py
borders
¶
borders() -> _CellBorders
The border edges of this cell, as a mapping keyed by edge name:
A cell adds the two diagonal edges tl2br and tr2bl to the edges a table
admits. A border set here takes precedence over the table border at the same
edge.
Source code in src/docx/table.py
merge
¶
merge(other_cell: _Cell)
Return a merged cell created by spanning the rectangular region having this
cell and other_cell as diagonal corners.
Raises InvalidSpanError if the cells do not define a rectangular region.
Source code in src/docx/table.py
_Column
¶
_Column(gridCol: CT_TblGridCol, parent: TableParent)
Bases: Parented
Table column.
Source code in src/docx/table.py
cells
property
¶
cells: tuple[_Cell, ...]
Sequence of _Cell instances corresponding to cells in this column.
width
property
writable
¶
width: Length | None
The width of this column in EMU, or None if no explicit width is set.
delete
¶
Remove this column from its table.
Removes the w:gridCol and the cell occupying this layout-grid column in every
row. A cell that spans this column and others is narrowed by one rather than
removed, so the rest of its span survives.
Source code in src/docx/table.py
_Columns
¶
_Columns(tbl: CT_Tbl, parent: TableParent)
_Row
¶
_Row(tr: CT_Row, parent: TableParent)
Bases: Parented
Table row.
Source code in src/docx/table.py
cells
property
¶
cells: tuple[_Cell, ...]
Sequence of _Cell instances corresponding to cells in this row.
Note that Word allows table rows to start later than the first column and end before the last column.
- Only cells actually present are included in the return value.
- This implies the length of this cell sequence may differ between rows of the same table.
- If you are reading the cells from each row to form a rectangular "matrix" data structure
of the table cell values, you will need to account for empty leading and/or trailing
layout-grid positions using
.grid_cols_beforeand.grid_cols_after.
grid_cols_after
property
¶
Count of unpopulated grid-columns after the last cell in this row.
Word allows a row to "end early", meaning that one or more cells are not present at the end of that row.
Note these are not simply "empty" cells. The renderer reads this value and "skips" this many columns after drawing the last cell.
Note this also implies that not all rows are guaranteed to have the same number of cells,
e.g. _Row.cells could have length n for one row and n - m for the next row in the same
table. Visually this appears as a column (at the beginning or end, not in the middle) with
one or more cells missing.
grid_cols_before
property
¶
Count of unpopulated grid-columns before the first cell in this row.
Word allows a row to "start late", meaning that one or more cells are not present at the beginning of that row.
Note these are not simply "empty" cells. The renderer reads this value and skips forward to the table layout-grid position of the first cell in this row; the renderer "skips" this many columns before drawing the first cell.
Note this also implies that not all rows are guaranteed to have the same number of cells,
e.g. _Row.cells could have length n for one row and n - m for the next row in the same
table.
height
property
writable
¶
height: Length | None
Return a Length object representing the height of this cell, or None if
no explicit height is set.
dont_split
property
writable
¶
True if this row is kept on a single page rather than broken across pages.
Corresponds to unchecking "Allow row to break across pages" in Word. None
indicates no explicit setting, which Word treats as allowing the break.
repeat_as_header
property
writable
¶
True when this row repeats at the top of each page the table spans.
Corresponds to "Repeat Header Rows" in Word. None indicates no explicit
setting, which Word treats as off.
Word only honours this on a contiguous run of rows starting at the first row of the table. Setting it on row 3 alone is legal XML that has no visible effect.
hidden
property
writable
¶
True when this row is not displayed.
None indicates no explicit setting, which Word treats as visible.
alignment
property
writable
¶
alignment: WD_TABLE_ALIGNMENT | None
Horizontal alignment of this row within the table, or None if not set.
This overrides the table's own alignment for this row alone.
cell_spacing
property
writable
¶
cell_spacing: Length | None
Spacing between the cells of this row, or None if not set.
width_before
property
writable
¶
width_before: Length | None
Width of the grid positions this row leaves unpopulated at its start.
Pairs with .grid_cols_before, which counts them. None if not set.
width_after
property
writable
¶
width_after: Length | None
Width of the grid positions this row leaves unpopulated at its end.
Pairs with .grid_cols_after, which counts them. None if not set.
height_rule
property
writable
¶
height_rule: WD_ROW_HEIGHT_RULE | None
Return the height rule of this cell as a member of the WdRowHeightRule.
This value is None if no explicit height_rule is set.
copy_to
¶
copy_to(
table: Table,
*,
before: _Row | None = None,
after: _Row | None = None,
missing_style: str = "copy",
) -> _Row
Return a copy of this row, newly placed in table.
"Duplicate this table row N times" is the other most-written-by-hand operation:
before and after place the copy relative to an existing row; with neither it
is appended.
The copy keeps this row's own cell widths and spans. It is not adjusted to
table's grid, so copying a row into a table of a different column count
produces a row that does not line up — which is what the XML says and what Word
will render.
See Paragraph.copy_to for what is repaired on the way — relationships, drawing ids, bookmarks, and, for a copy into another document, styles and numbering.
Source code in src/docx/table.py
delete
¶
Remove this row from its table.
A vertically merged cell whose span started in this row is not dropped: the row below inherits it, so the merge continues to render, which is what Word does when a row is deleted.