A table is w:tbl: its properties, then a grid of column widths, then rows.
Each row holds cells, and each cell holds block-level content, so a table can contain
paragraphs, lists and further tables.
An example
<w:tbl>
<w:tblPr>
<w:tblStyle w:val="TableGrid"/>
<w:tblW w:w="5000" w:type="pct"/>
</w:tblPr>
<w:tblGrid>
<w:gridCol w:w="2880"/>
<w:gridCol w:w="2880"/>
</w:tblGrid>
<w:tr>
<w:tc>
<w:tcPr><w:gridSpan w:val="2"/></w:tcPr>
<w:p><w:r><w:t>A heading across both columns</w:t></w:r></w:p>
</w:tc>
</w:tr>
<w:tr>
<w:tc><w:p><w:r><w:t>Left</w:t></w:r></w:p></w:tc>
<w:tc><w:p><w:r><w:t>Right</w:t></w:r></w:p></w:tc>
</w:tr>
</w:tbl>
A cell must end with a paragraph, even when it holds only a nested table.
Table properties: w:tblPr
| Element | Meaning |
|---|---|
w:tblStyle | A table style, by style ID. |
w:tblW | Preferred width: w:w and w:type (auto, dxa in twips, or pct). |
w:jc | Alignment of the whole table. |
w:tblInd | Indent from the leading margin. |
w:tblBorders | Borders: w:top, w:left, w:bottom, w:right, w:insideH, w:insideV. |
w:tblLayout | w:type: fixed or autofit. |
w:tblCellMar | Default cell margins. |
w:tblLook | Which parts of the table style apply: first row, last row, first column and so on. |
w:tblCaption, w:tblDescription | Alternative text for the table. |
w:tblPrChange | The previous properties, under tracked changes. |
Other children of w:tblPr: w:tblpPr (17.4.57, a floating table’s
position), w:tblOverlap (17.4.56), w:bidiVisual (17.4.1, cells laid out right
to left), w:tblStyleRowBandSize and w:tblStyleColBandSize (how many rows or
columns make one band; the page view also reads tblW, tblInd and a fixed tblLayout from the property exceptions (w:tblPrEx) of the first row only and applies them to the whole table, as MS-OI29500 2.1.156, 2.1.158 and 2.1.167 say Word does, test “table exceptions: tblInd in the first row moves the whole table”; the default is 1, Word allows 0 to 3 and a written 0 means it applies no banding of that kind, MS-OI29500 2.1.251 and 2.1.253, which the page view follows, test “table style: tblStyleRowBandSize 0 applies no row banding”; MS-OI29500 also says Word’s own default is 0 when the element is absent, which the viewer does not follow because the standard says 1 and no Word file was compared), w:tblCellSpacing (17.4.45) and w:shd (17.4.31).
Their order inside w:tblPr is: tblStyle, tblpPr, tblOverlap,
bidiVisual, tblStyleRowBandSize, tblStyleColBandSize, tblW,
jc, tblCellSpacing, tblInd, tblBorders, shd,
tblLayout, tblCellMar, tblLook, tblCaption,
tblDescription (python-docx CT_TblPr._tag_seq, docx/oxml/table.py lines 309 to 326;
the tail tblPrChange follows it in the schema).
w:tblLook: which style parts apply
A table style can define up to six conditional parts. w:tblLook chooses
which of them this table uses, with the attributes w:firstRow,
w:lastRow, w:firstColumn, w:lastColumn,
w:noHBand and w:noVBand (ECMA-376 Part 1, 17.4.55). The
Transitional schema adds w:val, a hexadecimal bitmask (Part 4, 14.4.12):
| Bit | Meaning |
|---|---|
0x0020 | First row |
0x0040 | Last row |
0x0080 | First column |
0x0100 | Last column |
0x0200 | No row banding |
0x0400 | No column banding |
Divergence from the standard. Part 1 says the default is to apply the
banding but not the first row, last row, first column or last column parts, and Part 4 says an
omitted bitmask is 0000. Word assumes 0x04A0 (first row, first column, no column
banding) when w:tblLook is absent (MS-OI29500, 2.1.160 a), and reads
w:val only when none of the six attributes is present (2.1.160 c). Corner cells where
a row part and a column part overlap (for example header row and first column) can be styled
on their own in Word (2.1.160 b). This site’s viewer follows Word on all three points.
Divergence from the standard. A margin or width with w:type="nil" means
“no width” in Part 1. For cell margins (w:tblCellMar, w:tcMar) Word ignores a
nil margin and keeps the inherited one, and counts pct and auto as 0
(MS-OI29500, 2.1.116, 2.1.124, 2.1.125, 2.1.146, 2.1.147, 2.1.152-154, 2.1.177). The viewer follows Word. LibreOffice
(checked with a three-table file, soffice to PDF) reads w:w whatever the type, so a nil
or pct margin of 1000 twips gives a 50 pt margin there; no Word was available to check the Word side.
The grid
w:tblGrid lists one w:gridCol for each column of the table, with
its width in twips. Cells line up against the grid: a cell that spans several columns
gives the number in w:gridSpan.
Rows and cells
w:tr can hold w:trPr, then w:tc cells. Rows and
cells can also be wrapped in a content control (w:sdt) or
w:customXml.
| Element | In | Meaning |
|---|---|---|
w:trHeight | w:trPr | Row height: w:val and w:hRule (auto, atLeast, exact). |
w:tblHeader | w:trPr | Repeat the row at the top of each page. |
w:cantSplit | w:trPr | Do not break the row across pages. |
w:ins, w:del | w:trPr | The row is a tracked insertion or deletion. |
w:tcW | w:tcPr | Preferred cell width. |
w:gridSpan | w:tcPr | The cell spans this many grid columns. |
w:vMerge | w:tcPr | Part of a vertically merged cell, below. |
w:tcBorders, w:shd, w:vAlign | w:tcPr | Borders, shading and vertical alignment. |
w:noWrap, w:textDirection | w:tcPr | No wrapping; the direction of the text. |
w:tcMar | w:tcPr | Margins of this cell (17.4.68); they win over the table’s w:tblCellMar (17.4.42) and over the table style. |
w:tcFitText, w:hideMark, w:headers | w:tcPr | Fit the text to the cell width (17.4.67); ignore the end-of-cell mark in the row height (17.4.21); the header cells associated with this cell (17.4.19). |
w:hMerge | w:tcPr | Horizontal merge, in the Transitional schema (CT_HMerge, wml.xsd line 2324); the usual way is w:gridSpan. |
w:cnfStyle | w:trPr, w:tcPr | Which conditional table-style formatting applies to the row (17.4.7) or cell (17.4.8). |
w:gridBefore, w:gridAfter, w:wBefore, w:wAfter | w:trPr | Grid columns left empty before the first cell (17.4.15) and after the last (17.4.14), and the preferred widths of those gaps (17.4.86, 17.4.85). |
w:divId, w:hidden, w:jc, w:tblCellSpacing | w:trPr | HTML div ID (17.4.9), hidden row marker (17.4.20), alignment of the row (17.4.27), spacing between its cells (17.4.43). |
Order of children, from python-docx (docx/oxml/table.py, read in this session):
w:tcPr is cnfStyle, tcW, gridSpan, hMerge, vMerge,
tcBorders, shd, noWrap, tcMar, textDirection,
tcFitText, vAlign, hideMark, headers, cellIns,
cellDel, cellMerge, tcPrChange (lines 795 to 814); w:trPr is
cnfStyle, divId, gridBefore, gridAfter, wBefore,
wAfter, cantSplit, trHeight, tblHeader, tblCellSpacing,
jc, hidden, ins, del, trPrChange (lines 897 to 913).
Where a cell sits in the grid: its offset is gridBefore of its row plus the gridSpan
of every cell before it, and its right edge is the offset plus its own span (python-docx grid_offset,
right, table.py 460 to 470 and 524 to 532). A missing w:gridSpan means 1 (table.py 829 to 836).
A new python-docx table is written with w:tblW w:type="auto" w:w="0" and w:tblLook w:val="04A0"
with firstRow, firstColumn and noVBand set (table.py 223 to 229), the same value
Word assumes (see w:tblLook).
Merged cells
Horizontal merges are w:gridSpan on one cell. Vertical merges use
w:vMerge: the top cell has w:val="restart", and the cells below
have the element with no w:val, meaning “continue”. A continued cell is still
written with its own content, which is usually one empty paragraph. A cell without
w:vMerge is in no merged group and closes any group above it; cells of one group must span the same grid
columns, or the document is non-conformant (17.4.84). An omitted w:val means continue
(python-docx CT_VMerge, table.py 972 to 977); the top of a group is found by walking up while the cell
is a continuation (table.py 534 to 539).
<w:tr>
<w:tc><w:tcPr><w:vMerge w:val="restart"/></w:tcPr>
<w:p><w:r><w:t>Spans two rows</w:t></w:r></w:p></w:tc>
<w:tc><w:p><w:r><w:t>Row one</w:t></w:r></w:p></w:tc>
</w:tr>
<w:tr>
<w:tc><w:tcPr><w:vMerge/></w:tcPr>
<w:p/></w:tc>
<w:tc><w:p><w:r><w:t>Row two</w:t></w:r></w:p></w:tc>
</w:tr>