Set a run's font size to a specific point value, for Latin (w:sz) and complex-script (w:szCs) scripts.
Library verdicts: python-docx: — docxjs: pass
| Feature id | docx/font-size--cs--eighteen |
|---|---|
| Format | docx |
| Category | text-formatting |
| Family | docx/font-size |
| Axis values | script=cs, size=eighteen |
| Spec | ecma-376-5-part-1 § 17.3.2.38 w:sz |
| ID / part | Predicate | XPath | python-docx | docxjs |
|---|---|---|---|---|
sz-val-is-36-halfpt-csword/document.xml | equal = 36Size is encoded in half-points; 18 pt = 36. Carried on <w:szCs>. | //w:r[w:t[normalize-space()='Size 18 pt']]/w:rPr/w:szCs/@w:val | — | — |
| ID | Predicate | Selector | python-docx | docxjs |
|---|---|---|---|---|
font-size-eighteen-cs-text-present | css_selector/ match-text = Size 18 ptThe rendered DOM must contain the fixture text. | .docx-wrapper span, .docx-wrapper p | — | pass |
font-size-eighteen-cs-is-correct | computed_style = ^18pt$|^18\.0pt$|^\d+(\.\d+)?px$ style=font-sizeThe computed font-size on the sized run must be 18pt (or its px equivalent, since renderers vary). | .docx-wrapper span:not(:has(*)) | — | pass |
scripts/gen_font_size.py
#!/usr/bin/env python3
"""Generate a ``fixtures/docx/font-size*.docx`` fixture.
Parameterised: called with ``--pt <N> --script latin|cs --out <path>``.
Writes a one-run document containing the text ``Size <N> pt`` with the
font size set on either the Latin axis (``run.font.size``, emits
``<w:sz w:val="2N"/>`` plus a matching ``<w:szCs>``) or the
complex-script axis (``run.font.cs_size``, emits ``<w:szCs w:val="2N"/>``
only).
The ``features/docx/font-size.json`` manifest's ``generator.arg_template``
field drives these args at expansion time; the Cartesian product of
10 sizes x 2 scripts yields 20 fixtures.
"""
from __future__ import annotations
import argparse
from pathlib import Path
from docx import Document
from docx.shared import Pt
_REPO_ROOT = Path(__file__).resolve().parent.parent
def _write(pt: int, script: str, out: Path) -> None:
doc = Document()
paragraph = doc.add_paragraph()
run = paragraph.add_run(f"Size {pt} pt")
if script == "latin":
run.font.size = Pt(pt)
elif script == "cs":
run.font.cs_size = Pt(pt)
else:
raise ValueError(f"unknown script {script!r}; expected 'latin' or 'cs'")
out.parent.mkdir(parents=True, exist_ok=True)
doc.save(out, reproducible=True)
def main() -> int:
parser = argparse.ArgumentParser()
parser.add_argument("--pt", type=int, required=True, help="Font size in points")
parser.add_argument(
"--script",
choices=("latin", "cs"),
default="latin",
help="Which font-size axis to write: 'latin' -> w:sz, 'cs' -> w:szCs only",
)
parser.add_argument(
"--out",
required=True,
help="Output .docx path (absolute, or repo-relative).",
)
args = parser.parse_args()
out_path = Path(args.out)
if not out_path.is_absolute():
out_path = _REPO_ROOT / out_path
_write(args.pt, args.script, out_path)
print(out_path)
return 0
if __name__ == "__main__":
raise SystemExit(main())
{
"$schema": "../manifest.schema.json",
"id": "docx/font-size--cs--eighteen",
"kind": "literal",
"title": "Font size",
"format": "docx",
"category": "text-formatting",
"summary": "Set a run's font size to a specific point value, for Latin (w:sz) and complex-script (w:szCs) scripts.",
"spec": {
"source": "ecma-376-5-part-1",
"clause": "17.3.2.38",
"element": "w:sz",
"rnc_reference": "spec/ecma-376-5/part-1/rnc/WordprocessingML.rnc",
"xsd_reference": "spec/ecma-376-5/part-1/xsd/wml.xsd",
"notes": "<w:sz w:val=\"N\"/> sets size in HALF-points for the Latin / default script; <w:szCs w:val=\"N\"/> carries the matching complex-script (Arabic/Hebrew/Thai) value. A 24-point font is w:val=\"48\". This manifest is a parameterised family: 10 sizes x 2 scripts = 20 cases. Each expanded case targets a distinct fixture `docx/font-size--<size-id>--<script-id>.docx`. python-docx exposes `run.font.size` (writes w:sz, and also w:szCs for symmetry) and `run.font.cs_size` (writes w:szCs only). Other OOXML font axes (eastAsian, hAnsi, bidi) do not have their own size element -- only Latin vs complex-script vary."
},
"fixtures": {
"machine": "docx/font-size--cs--eighteen",
"office": "docx/font-size--cs--eighteen"
},
"generator": {
"python": "scripts/gen_font_size.py",
"arg_template": "--pt {size.pt} --script {script.id} --out fixtures/docx/font-size--{script.id}--{size.id}.docx"
},
"_expansion": {
"parent_id": "docx/font-size",
"bindings": {
"script": "cs",
"size": "eighteen"
}
},
"assertions": [
{
"id": "sz-val-is-36-halfpt-cs",
"part": "word/document.xml",
"namespaces": {
"w": "http://schemas.openxmlformats.org/wordprocessingml/2006/main"
},
"xpath": "//w:r[w:t[normalize-space()='Size 18 pt']]/w:rPr/w:szCs/@w:val",
"must": "equal",
"value": "36",
"description": "Size is encoded in half-points; 18 pt = 36. Carried on <w:szCs>."
}
],
"render_assertions": [
{
"id": "font-size-eighteen-cs-text-present",
"kind": "css_selector",
"selector": ".docx-wrapper span, .docx-wrapper p",
"must": "match-text",
"value": "Size 18 pt",
"description": "The rendered DOM must contain the fixture text."
},
{
"id": "font-size-eighteen-cs-is-correct",
"kind": "computed_style",
"selector": ".docx-wrapper span:not(:has(*))",
"style_property": "font-size",
"value": "^18pt$|^18\\.0pt$|^\\d+(\\.\\d+)?px$",
"description": "The computed font-size on the sized run must be 18pt (or its px equivalent, since renderers vary)."
}
]
}
