docx/table-alignment

A 1-row, 2-column table whose tblPr/jc is set to 'center' to center the whole table between the page margins.

Library verdicts: python-docx: — docxjs: pass

Metadata

Feature iddocx/table-alignment
Formatdocx
Categorytables
Spececma-376-5-part-1 § 17.4.39 w:jc

XPath assertions

ID / partPredicateXPathpython-docxdocxjs
tbl-jc-center
word/document.xml
equal = center
The table must declare <w:jc w:val="center"/> inside its <w:tblPr>.
//w:tbl/w:tblPr/w:jc/@w:val
tbl-centered-text
word/document.xml
exist
A cell containing the literal text 'Centered table A' must be present in the table.
//w:tbl//w:tc//w:t[normalize-space()='Centered table A']

Render assertions

IDPredicateSelectorpython-docxdocxjs
table-alignment-smokecss_selector/ match-text = Centered table A
Smoke check: at least one rendered table cell under .docx-wrapper must contain 'Centered table A'.
.docx-wrapper table td, .docx-wrapper table thpass
table-alignment-cell-b-renderedcss_selector/ match-text = ^Centered table B$
The second cell must be exactly 'Centered table B'. Catches content leaking from cell A into cell B (or vice versa).
.docx-wrapper table tr:nth-of-type(1) td:nth-of-type(2), .docx-wrapper table tr:nth-of-type(1) th:nth-of-type(2)

Generator source

scripts/gen_table_alignment.py

#!/usr/bin/env python3
"""Generate ``fixtures/docx/table-alignment.docx``.

A 1x2 table aligned to the center of the page
(``w:tblPr/w:jc w:val=\"center\"``). Satisfies the assertions in
``features/docx/table-alignment.json``.

Uses the standard ``Table.alignment`` property.
"""

from __future__ import annotations

from pathlib import Path

from docx import Document
from docx.enum.table import WD_TABLE_ALIGNMENT

_REPO_ROOT = Path(__file__).resolve().parent.parent
_OUT = _REPO_ROOT / "fixtures" / "docx" / "table-alignment.docx"


def main() -> int:
    doc = Document()
    table = doc.add_table(rows=1, cols=2)
    table.alignment = WD_TABLE_ALIGNMENT.CENTER
    table.rows[0].cells[0].text = "Centered table A"
    table.rows[0].cells[1].text = "Centered table B"

    _OUT.parent.mkdir(parents=True, exist_ok=True)
    doc.save(_OUT, reproducible=True)
    print(_OUT)
    return 0


if __name__ == "__main__":
    raise SystemExit(main())

Manifest (expanded)

{
  "$schema": "../manifest.schema.json",
  "id": "docx/table-alignment",
  "title": "Table alignment centered (tblPr/jc)",
  "format": "docx",
  "category": "tables",
  "summary": "A 1-row, 2-column table whose tblPr/jc is set to 'center' to center the whole table between the page margins.",
  "spec": {
    "source": "ecma-376-5-part-1",
    "clause": "17.4.39",
    "element": "w:jc",
    "rnc_reference": "spec/ecma-376-5/part-1/rnc/WordprocessingML.rnc",
    "xsd_reference": "spec/ecma-376-5/part-1/xsd/wml.xsd",
    "notes": "<w:tblPr><w:jc w:val=\"center\"/></w:tblPr> centers the whole table between the page margins. Valid values are 'start'/'left', 'center', 'end'/'right' (plus the Strict-equivalent spellings)."
  },
  "fixtures": {
    "machine": "docx/table-alignment",
    "office": "docx/table-alignment"
  },
  "generator": {
    "python": "scripts/gen_table_alignment.py"
  },
  "assertions": [
    {
      "id": "tbl-jc-center",
      "part": "word/document.xml",
      "namespaces": {
        "w": "http://schemas.openxmlformats.org/wordprocessingml/2006/main"
      },
      "xpath": "//w:tbl/w:tblPr/w:jc/@w:val",
      "must": "equal",
      "value": "center",
      "description": "The table must declare <w:jc w:val=\"center\"/> inside its <w:tblPr>."
    },
    {
      "id": "tbl-centered-text",
      "part": "word/document.xml",
      "namespaces": {
        "w": "http://schemas.openxmlformats.org/wordprocessingml/2006/main"
      },
      "xpath": "//w:tbl//w:tc//w:t[normalize-space()='Centered table A']",
      "must": "exist",
      "description": "A cell containing the literal text 'Centered table A' must be present in the table."
    }
  ],
  "render_assertions": [
    {
      "id": "table-alignment-smoke",
      "kind": "css_selector",
      "selector": ".docx-wrapper table td, .docx-wrapper table th",
      "must": "match-text",
      "value": "Centered table A",
      "description": "Smoke check: at least one rendered table cell under .docx-wrapper must contain 'Centered table A'."
    },
    {
      "id": "table-alignment-cell-b-rendered",
      "kind": "css_selector",
      "selector": ".docx-wrapper table tr:nth-of-type(1) td:nth-of-type(2), .docx-wrapper table tr:nth-of-type(1) th:nth-of-type(2)",
      "must": "match-text",
      "value": "^Centered table B$",
      "description": "The second cell must be exactly 'Centered table B'. Catches content leaking from cell A into cell B (or vice versa)."
    }
  ]
}

Fixture

Download table-alignment.docx (18.7 KB)

Reference preview

Reference (machine, page 1 PNG)

docx/table-alignment page 1 reference

Reference PDF


Download PDF Open in PDF.js

Spec notes

<w:tblPr><w:jc w:val="center"/></w:tblPr> centers the whole table between the page margins. Valid values are 'start'/'left', 'center', 'end'/'right' (plus the Strict-equivalent spellings).