docx/alignment-center

Paragraph horizontal alignment set to center.

Library verdicts: python-docx: — docxjs: pass

Metadata

Feature iddocx/alignment-center
Formatdocx
Categoryparagraph-layout
Spececma-376-5-part-1 § 17.3.1.13 w:jc

XPath assertions

ID / partPredicateXPathpython-docxdocxjs
alignment-center-jc-present
word/document.xml
exist
The paragraph must have a <w:jc> child inside <w:pPr>.
//w:p[w:r/w:t[normalize-space()='Center aligned paragraph']]/w:pPr/w:jc
alignment-center-val
word/document.xml
equal = center
w:jc@w:val must be exactly 'center'.
//w:p[w:r/w:t[normalize-space()='Center aligned paragraph']]/w:pPr/w:jc/@w:val

Render assertions

IDPredicateSelectorpython-docxdocxjs
alignment-center-text-presentcss_selector/ match-text = Center aligned paragraph
The rendered DOM must contain the fixture text.
.docx-wrapper ppass
alignment-center-computedcomputed_style = ^center$ style=text-align
The computed text-align on the paragraph must be 'center'.
.docx-wrapper ppass

Generator source

scripts/gen_alignment_center.py

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

A minimal document exercising ``w:jc`` with center alignment. One
paragraph containing a center-aligned run. Designed to satisfy the
assertions in ``features/docx/alignment-center.json``.
"""

from __future__ import annotations

from pathlib import Path

from docx import Document
from docx.enum.text import WD_ALIGN_PARAGRAPH

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


def main() -> int:
    doc = Document()
    paragraph = doc.add_paragraph("Center aligned paragraph")
    paragraph.alignment = WD_ALIGN_PARAGRAPH.CENTER
    _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/alignment-center",
  "title": "Paragraph alignment — center",
  "format": "docx",
  "category": "paragraph-layout",
  "summary": "Paragraph horizontal alignment set to center.",
  "spec": {
    "source": "ecma-376-5-part-1",
    "clause": "17.3.1.13",
    "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": "Horizontal alignment is carried by <w:jc w:val=\"center\"/> inside <w:pPr>. See also 17.3.1.1 for pPr."
  },
  "fixtures": {
    "machine": "docx/alignment-center",
    "office": "docx/alignment-center"
  },
  "generator": {
    "python": "scripts/gen_alignment_center.py"
  },
  "assertions": [
    {
      "id": "alignment-center-jc-present",
      "part": "word/document.xml",
      "namespaces": {
        "w": "http://schemas.openxmlformats.org/wordprocessingml/2006/main"
      },
      "xpath": "//w:p[w:r/w:t[normalize-space()='Center aligned paragraph']]/w:pPr/w:jc",
      "must": "exist",
      "description": "The paragraph must have a <w:jc> child inside <w:pPr>."
    },
    {
      "id": "alignment-center-val",
      "part": "word/document.xml",
      "namespaces": {
        "w": "http://schemas.openxmlformats.org/wordprocessingml/2006/main"
      },
      "xpath": "//w:p[w:r/w:t[normalize-space()='Center aligned paragraph']]/w:pPr/w:jc/@w:val",
      "must": "equal",
      "value": "center",
      "description": "w:jc@w:val must be exactly 'center'."
    }
  ],
  "render_assertions": [
    {
      "id": "alignment-center-text-present",
      "kind": "css_selector",
      "selector": ".docx-wrapper p",
      "must": "match-text",
      "value": "Center aligned paragraph",
      "description": "The rendered DOM must contain the fixture text."
    },
    {
      "id": "alignment-center-computed",
      "kind": "computed_style",
      "selector": ".docx-wrapper p",
      "style_property": "text-align",
      "value": "^center$",
      "description": "The computed text-align on the paragraph must be 'center'."
    }
  ]
}

Fixture

Download alignment-center.docx (18.6 KB)

Reference preview

Reference (machine, page 1 PNG)

docx/alignment-center page 1 reference

Reference PDF


Download PDF Open in PDF.js

Spec notes

Horizontal alignment is carried by <w:jc w:val="center"/> inside <w:pPr>. See also 17.3.1.1 for pPr.