docx/font-size

Set a run's font size to a specific point value, for Latin (w:sz) and complex-script (w:szCs) scripts.

Cases (20)

Feature IDAxis bindingspython-docxdocxjs
docx/font-size--cs--eightscript=cs, size=eightpass
docx/font-size--cs--eighteenscript=cs, size=eighteenpass
docx/font-size--cs--elevenscript=cs, size=elevenpass
docx/font-size--cs--fourteenscript=cs, size=fourteenpass
docx/font-size--cs--sixscript=cs, size=sixpass
docx/font-size--cs--sixteenscript=cs, size=sixteenpass
docx/font-size--cs--tenscript=cs, size=tenpass
docx/font-size--cs--thirtysixscript=cs, size=thirtysixpass
docx/font-size--cs--twelvescript=cs, size=twelvepass
docx/font-size--cs--twentyfourscript=cs, size=twentyfourpass
docx/font-size--latin--eightscript=latin, size=eightpass
docx/font-size--latin--eighteenscript=latin, size=eighteenpass
docx/font-size--latin--elevenscript=latin, size=elevenpass
docx/font-size--latin--fourteenscript=latin, size=fourteenpass
docx/font-size--latin--sixscript=latin, size=sixpass
docx/font-size--latin--sixteenscript=latin, size=sixteenpass
docx/font-size--latin--tenscript=latin, size=tenpass
docx/font-size--latin--thirtysixscript=latin, size=thirtysixpass
docx/font-size--latin--twelvescript=latin, size=twelvepass
docx/font-size--latin--twentyfourscript=latin, size=twentyfourpass

Aggregate

LibraryPassFailPending
python-docx0020
docxjs2000

Parameter axes

Spec 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.

Generator source

scripts/gen_font_size.py — runs with --arg_template --pt {size.pt} --script {script.id} --out fixtures/docx/font-size--{script.id}--{size.id}.docx

#!/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())

Manifest (parent, unexpanded)

{
  "$schema": "../manifest.schema.json",
  "id": "docx/font-size",
  "kind": "parameterised",
  "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",
    "office": "docx/font-size"
  },
  "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"
  },
  "parameters": {
    "size": [
      {
        "id": "six",
        "pt": 6,
        "halfpt": 12
      },
      {
        "id": "eight",
        "pt": 8,
        "halfpt": 16
      },
      {
        "id": "ten",
        "pt": 10,
        "halfpt": 20
      },
      {
        "id": "eleven",
        "pt": 11,
        "halfpt": 22
      },
      {
        "id": "twelve",
        "pt": 12,
        "halfpt": 24
      },
      {
        "id": "fourteen",
        "pt": 14,
        "halfpt": 28
      },
      {
        "id": "sixteen",
        "pt": 16,
        "halfpt": 32
      },
      {
        "id": "eighteen",
        "pt": 18,
        "halfpt": 36
      },
      {
        "id": "twentyfour",
        "pt": 24,
        "halfpt": 48
      },
      {
        "id": "thirtysix",
        "pt": 36,
        "halfpt": 72
      }
    ],
    "script": [
      {
        "id": "latin",
        "element": "w:sz",
        "api": "size"
      },
      {
        "id": "cs",
        "element": "w:szCs",
        "api": "cs_size"
      }
    ]
  },
  "assertions_template": [
    {
      "id": "sz-val-is-{size.halfpt}-halfpt-{script.id}",
      "part": "word/document.xml",
      "namespaces": {
        "w": "http://schemas.openxmlformats.org/wordprocessingml/2006/main"
      },
      "xpath": "//w:r[w:t[normalize-space()='Size {size.pt} pt']]/w:rPr/{script.element}/@w:val",
      "must": "equal",
      "value": "{size.halfpt}",
      "description": "Size is encoded in half-points; {size.pt} pt = {size.halfpt}. Carried on <{script.element}>."
    }
  ],
  "render_assertions_template": [
    {
      "id": "font-size-{size.id}-{script.id}-text-present",
      "kind": "css_selector",
      "selector": ".docx-wrapper span, .docx-wrapper p",
      "must": "match-text",
      "value": "Size {size.pt} pt",
      "description": "The rendered DOM must contain the fixture text."
    },
    {
      "id": "font-size-{size.id}-{script.id}-is-correct",
      "kind": "computed_style",
      "selector": ".docx-wrapper span:not(:has(*))",
      "style_property": "font-size",
      "value": "^{size.pt}pt$|^{size.pt}\\.0pt$|^\\d+(\\.\\d+)?px$",
      "description": "The computed font-size on the sized run must be {size.pt}pt (or its px equivalent, since renderers vary)."
    }
  ]
}