A table on a slide is a DrawingML table, not a PresentationML element. It sits in a
p:graphicFrame on the slide: the frame holds the position and size
(p:xfrm), and inside a:graphic a a:graphicData with the
uri http://schemas.openxmlformats.org/drawingml/2006/table holds one
a:tbl. The rules below are from ECMA-376 Part 1, 21.1.3
(research/ooxml/ecma376/p1/part1.txt, lines 121596 to 122503).
The table
<a:tbl>
<a:tblPr firstRow="1" bandRow="1">
<a:tableStyleId>{5C22544A-7EE6-4342-B048-85BDC9FD1C3A}</a:tableStyleId>
</a:tblPr>
<a:tblGrid>
<a:gridCol w="2286000"/>
<a:gridCol w="2286000"/>
</a:tblGrid>
<a:tr h="370840">
<a:tc>
<a:txBody><a:bodyPr/><a:lstStyle/><a:p><a:r><a:t>Name</a:t></a:r></a:p></a:txBody>
<a:tcPr/>
</a:tc>
…
</a:tr>
</a:tbl>
| Element | Meaning (Part 1) |
|---|---|
a:tbl (21.1.3.13) | The root of a table: tblPr, tblGrid, then any number of tr. |
a:tblPr (21.1.3.15) | Whole-table properties. Boolean attributes firstRow, lastRow, firstCol, lastCol, bandRow, bandCol, each off when absent, switch the matching section of the table style on; rtl lays the table out from right to left. Its child a:tableStyleId (21.1.3.12) is a GUID that names a style. |
a:tblGrid (21.1.3.14), a:gridCol (21.1.3.2) | One column element for every column; w is the width. |
a:tr (21.1.3.18) | A row; h is the height. There should be one a:tc for every grid column. |
a:tc (21.1.3.16) | A cell: a text body (a:txBody) and its properties (a:tcPr). Attributes gridSpan, rowSpan, hMerge, vMerge and id. |
a:tcPr (21.1.3.17) | Cell formatting. Attributes marL, marR, marT, marB (the margins), anchor (vertical alignment of the text), anchorCtr (centres the text box itself so left-aligned text sits along the middle), horzOverflow (clip or overflow) and vert (text direction). Children: the borders lnL, lnR, lnT, lnB, the diagonals lnTlToBr and lnBlToTr (21.1.3.5 to 21.1.3.10, each an a:ln form with w, cap, cmpd, algn), a fill, cell3D (21.1.3.1) and headers (21.1.3.4). |
Merged cells
A merged cell is written as the cell that starts the merge, with gridSpan (columns) or
rowSpan (rows), followed by the cells it covers, which stay in the row and carry
hMerge="1" or vMerge="1". The covered cells are still written; they
are not drawn. For a block that is two columns and two rows, the first cell has
gridSpan="2" rowSpan="2", the next cell in that row hMerge="1", and in the row below
the first cell has vMerge="1" and the next hMerge="1" vMerge="1".
The spec (21.1.3.16) gives a span of 3 with two following covered cells as its example, and
types gridSpan and rowSpan as int.
Table styles
The styles live in ppt/tableStyles.xml (root a:tblStyleLst, its def
attribute the style given to new tables), reached from the presentation by a relationship of type
tableStyles. A table names one by a:tableStyleId. A style
(a:tblStyle, 21.1.3.11) has a styleId (a GUID) and a styleName, and the
fourteen elements the spec names: tblBg (the table background) and thirteen sections,
wholeTbl, band1H, band2H, band1V, band2V, lastCol,
firstCol, lastRow, seCell, swCell, firstRow,
neCell and nwCell, in that order (Annex A, CT_TableStyle, line 163999). Every one is optional.
| Section | Applies when |
|---|---|
wholeTbl | Always: the formatting when all options are off. |
firstRow, lastRow, firstCol, lastCol | The matching tblPr attribute is on. |
band1H, band2H | bandRow is on: odd and even rows. |
band1V, band2V | bandCol is on: odd and even columns. |
nwCell, neCell, swCell, seCell | The corner cell, when both the row option and the column option that meet in it are on (21.1.3.11). |
Each section holds a:tcTxStyle (text: b and i as on, off or def, a font reference and a colour) and
a:tcStyle (a:tcBdr with left, right, top, bottom,
insideH, insideV, tl2br, tr2bl, each an a:ln or an
a:lnRef into the theme; and a a:fill or a:fillRef). The spec does not give an order for laying
the sections over each other, and does not say which row counts as the first banded row when there is a header.
What the explorer does
- It reads
tableStyles.xmland finds the style bystyleId. Sections are laid over each other in this order:wholeTbl, the row bands, the column bands,firstCol,lastCol,firstRow,lastRow, then the four corner cells; a later section wins for the fill, the text colour, bold and each border. - Banding counts from the first row after the header (or the first row when
firstRowis off), and leaves out the last row whenlastRowis on; the first of those rows takesband1H. Columns count the same way. - An outer edge of the table takes the section's
left,right,toporbottom; an inner edge takesinsideVorinsideH. The edge of a first row, last row, first column or last column that faces the rest of the table also takes that section's ownbottom,top,rightorleft. - A border or fill given on the cell (
a:tcPr) wins over the style. The two diagonals of a cell are drawn fromlnTlToBrandlnBlToTr. - A table with
rtlon has its columns mirrored. - When the style id is not in the file, PowerPoint’s default look (the header in the first accent colour, white bold text, tinted bands) is used; a table with no
a:tableStyleIdhas no style.
Done: a:lnRef takes the width and dash of the theme line style it names (idx 1 to 3, idx 0 is no line), with the reference’s colour as phClr; a:fillRef takes the theme fill style it names (idx 1 to 999) with the reference’s colour as phClr, idx 0 is no fill. Background fill styles (idx 1001 and up) take the theme’s bgFillStyleLst entry (20.1.4.1.13) in section fills and in tblBg. Also done: tblBg (20.1.4.2.25) draws its fill as one rectangle the size of the whole table under all cells (its effectRef is not drawn); anchorCtr centres the text bounds box in the cell as a shape’s does; vert turns the cell text a quarter turn about the cell centre as a shape’s bodyPr vert does (vert270 the other way, every other value except horz as 90 degrees). A row is grown to the height its tallest single-row cell needs (text height plus the top and bottom margins); a cell with turned text makes the row as tall as its longest paragraph on one line. The specification gives a row only its height h (21.1.3.18) and no rule for text that does not fit, so growth is the viewer’s choice, made so that text is not drawn outside its cell; the row edges were compared with a LibreOffice export (they are equal; no PowerPoint output was available), and while measuring the style’s bold and colour are not applied and cells that span rows are left out. A column is at least as wide as marL + marR of its cells plus two points: [MS-OI29500] 2.1.1416 a says PowerPoint widens a narrower gridCol to that, and the viewer does the same (the cells that span columns are left out). Not done and why: cell3D (21.1.3.1) names only prstMaterial (and, in the schema, a bevel and a light rig), and the specification defines no formula for the look that material and light give a cell, so none is drawn; [MS-OI29500] 2.1.1415 adds only defaults (prstMaterial is dkEdge in PowerPoint, not plastic, and the light rig is flood when none is given), still with no drawing rule; horzOverflow (clip or overflow) has nothing to decide here because cell text always wraps inside the cell width (a word longer than the cell is broken); the headers references are not used; the font reference of a section is not used for the typeface.
Divergence from spec
Compared with python-pptx (research/pptx/src/pptx-oxml-table.py) and Apache POI
(research/pptx/src/poi-XSLFTable.java).
- Defaults. The spec gives off for every
tblPrboolean (21.1.3.15); python-pptx agrees (pptx-oxml-table.pylines 57 to 78) and writesfirstRow="1" bandRow="1"on creation (line 168). Annex A givesgridSpanandrowSpana default of 1 andhMergeandvMergefalse (part1.txtlines 163711 to 163714); python-pptx has the same (lines 191 to 201). - Margins. Annex A gives
CT_TableCellPropertiesthe defaults 91440 EMU formarLandmarRand 45720 formarTandmarB,anchort,anchorCtrfalse,horzOverflowclipandverthorz(lines 163679 to 163687; the prose of 21.1.3.17 gives none). python-pptx has the same margins (lines 263 to 300), and so does the explorer. - Default style. python-pptx gives a new table the style
{5C22544A-7EE6-4342-B048-85BDC9FD1C3A}(lines 109 to 112). The spec has no default style for a table that names none; POI looks the style up in the table styles part and returns none whentableStyleIdis not set (poi-XSLFTable.java335 to 353, with its own “TODO: support inline table style”). - Merge origin. python-pptx treats a cell as the origin of a merge when
gridSpan > 1and it is notvMerge, orrowSpan > 1and it is nothMerge, and as spanned whenhMergeorvMergeis set (lines 251 to 260). The spec only sayshMergemerges a cell with the previous cell in the row andvMergewith the one above; the explorer draws no cell that has either attribute and gives the origin the width and height of the cells it covers. - Writing a merge. POI’s
mergeCells(lines 280 to 326) setsrowSpanon every cell of the first row andvMergeon every cell of the other rows, andgridSpanon every cell of the first column andhMergeon every cell of the other columns. For a 2 by 2 block the cell at the top right getsrowSpan="2" hMerge="1"and the one at the bottom leftvMerge="1" gridSpan="2", where the convention above gives the top right onlyhMergeand the bottom left onlyvMerge. The spec’s examples (21.1.3.16) merge in one direction only. - Row height. The spec says
his the height of the row. POI’supdateCellAnchor(lines 390 to 404) raises each row to the height of its tallest text, with the comment that the written height might be too low or 0. The explorer does the same: it measures the text of each cell (the longest word against the cell width, the wrapped lines against the height, plus the cell margins) and grows the row to fit, then draws the rows at the grown height. The sizes come from the explorer’s own font metrics; the row edges equal those of a LibreOffice export of the same table (samples/pptx/tablewarp.pptx). - Table style sections. python-pptx does not read the style sections (it only writes the id). POI’s
XSLFTable.getTableStyle(line 335) finds a style bystyleIdin the presentation’s table styles. How the sections apply is inpoi-XSLFTableCell.java(every line read), lines 586 to 638: a cell takes one section, the first that fits offirstRow(first row),lastRow,firstCol,lastCol, elseband1Horband2HwhenbandRowis on (even or odd after counting the first row out), elseband1Vorband2VwhenbandColis on andbandRowis not, elsewholeTbl; a missing section falls back towholeTbl. The corner sections (nwCell,neCell,swCell,seCell) are never used, the last row is not left out of the banding, and a cell spanning columns is judged by its first column only. The explorer lays several sections on each other in a fixed order (wholeTbl, row bands, column bands,firstCol,lastCol,firstRow,lastRow, then the four corners), a span counted by its far edge. The spec does not give an order for either; the explorer’s order is its own choice and is checked only against its own tests. - Fill and text of a cell. POI’s cell fill is the
tcPrfill when it yields a paint, else the chosen section’stcStylefillorfillRef, else the one ofwholeTbl(lines 535 to 576). Text colour comes from the section’stcTxStyleunless the run sets its own solid, gradient or pattern fill; bold and italic from the section unless the run sets them (lines 829 to 927). The explorer takes the cell’s own fill first, then each active section in turn (last wins), and applies the section’s text colour and bold to the cell’s text; italic from the style is not applied. - Borders. POI’s file comment (lines 47 to 52) says PowerPoint draws an edge shared by two cells from the bottom or right border of the upper or left cell even when that border is unset, that it ignores the borders of merged-away cells, and that the first cell of a merged range gives the border of the whole range; POI therefore copies a border it sets to the neighbour (lines 162 to 238). The standard does not say which of two cells’ lines wins (21.1.3.5
lnB, 21.1.3.7lnL, 21.1.3.8lnRand 21.1.3.9lnTeach describe one cell’s line). The explorer draws the four sides of every cell it draws, in cell order, so where both neighbours give a line the right or lower cell is drawn last, and it has no rule for an unset border of the upper or left cell. This is a gap: no PowerPoint output was available to check the rule against. A border read by POI needs a solid fill (lines 419 to 495); a gradient or pattern line has no colour, dash, cap or compound there. The explorer draws the line with its paint (any a:ln fill) and a single stroke (compound lines are not drawn double). - Vertical text. POI maps
vertto four directions:vert,eaVert,mongolianVertto vertical;vert270;wordArtVertandwordArtVertRtlto stacked; everything else horizontal (lines 714 to 736). The explorer turns the text a quarter turn for any value other thanhorzandvert270, sowordArtVertstacked letters, and the upright East Asian glyphs ofeaVert, are drawn as ordinary turned text. Anchor defaults to top (lines 680 to 690), as in Annex A. - Spans. A cell’s
gridSpanandrowSpandefault to 1 (lines 644 to 658) andhMergeandvMergeto false (lines 240 to 248); the first cell of a range is found by walking left overhMergecells, then up overvMergecells (lines 221 to 238). - Rows.
poi-XSLFTableRow.java(every line read) readshas a length and the cells as thetcchildren in order (lines 43 to 65). Its row merge setsgridSpanon the first cell andhMergeon the others (lines 124 to 139), which is the convention described above for a merge in one direction. A new cell is created with all four borders set tonoFill(XSLFTableCell.prototype, lines 81 to 89), so a file written by POI shows no cell borders unless the table style or the cell sets them, and a table style border does not show through a cell that has its ownnoFillline; the explorer reads such anoFillline as “no line” in the same way (line 1500 ofpptx/render.js).