docx/font-family

Set a run's typeface to a specific font family (ASCII script slot).

Library verdicts: python-docx: — docxjs: pass

Metadata

Feature iddocx/font-family
Formatdocx
Categorytext-formatting
Spececma-376-5-part-1 § 17.3.2.25 w:rFonts

XPath assertions

ID / partPredicateXPathpython-docxdocxjs
rfonts-element-present
word/document.xml
exist
The run must have a <w:rFonts> child carrying the font slot attributes.
//w:r[w:t[normalize-space()='Arial font text']]/w:rPr/w:rFonts
rfonts-ascii-is-arial
word/document.xml
equal = Arial
The ASCII-script font slot must be exactly 'Arial'.
//w:r[w:t[normalize-space()='Arial font text']]/w:rPr/w:rFonts/@w:ascii

Render assertions

IDPredicateSelectorpython-docxdocxjs
font-family-text-presentcss_selector/ match-text = Arial font text
The rendered DOM must contain the fixture text.
.docx-wrapper span, .docx-wrapper ppass
font-family-is-arialcomputed_style = Arial style=font-family
The computed font-family on the run must include Arial.
.docx-wrapper span:not(:has(*))pass

Generator source

scripts/gen_font_family.py

#!/usr/bin/env python3
"""Generate ``fixtures/docx/font-family.docx``.

A minimal document exercising the ``w:rFonts`` element. One paragraph
containing one run whose typeface is explicitly set to Arial, which
encodes as ``<w:rFonts w:ascii="Arial" w:hAnsi="Arial"/>``.
Satisfies the assertions in ``features/docx/font-family.json``.
"""

from __future__ import annotations

from pathlib import Path

from docx import Document

_REPO_ROOT = Path(__file__).resolve().parent.parent
_OUT = _REPO_ROOT / "fixtures" / "docx" / "font-family.docx"


def main() -> int:
    doc = Document()
    paragraph = doc.add_paragraph()
    run = paragraph.add_run("Arial font text")
    run.font.name = "Arial"
    _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/font-family",
  "title": "Font family",
  "format": "docx",
  "category": "text-formatting",
  "summary": "Set a run's typeface to a specific font family (ASCII script slot).",
  "spec": {
    "source": "ecma-376-5-part-1",
    "clause": "17.3.2.25",
    "element": "w:rFonts",
    "rnc_reference": "spec/ecma-376-5/part-1/rnc/WordprocessingML.rnc",
    "xsd_reference": "spec/ecma-376-5/part-1/xsd/wml.xsd",
    "notes": "<w:rFonts/> carries per-script font slot attributes: w:ascii, w:hAnsi, w:cs, w:eastAsia (plus theme-named w:asciiTheme, etc.). Setting a single font name via python-docx's run.font.name populates w:ascii and w:hAnsi with the same value; this fixture asserts on the authoritative ASCII slot."
  },
  "fixtures": {
    "machine": "docx/font-family",
    "office": "docx/font-family"
  },
  "generator": {
    "python": "scripts/gen_font_family.py"
  },
  "assertions": [
    {
      "id": "rfonts-element-present",
      "part": "word/document.xml",
      "namespaces": {
        "w": "http://schemas.openxmlformats.org/wordprocessingml/2006/main"
      },
      "xpath": "//w:r[w:t[normalize-space()='Arial font text']]/w:rPr/w:rFonts",
      "must": "exist",
      "description": "The run must have a <w:rFonts> child carrying the font slot attributes."
    },
    {
      "id": "rfonts-ascii-is-arial",
      "part": "word/document.xml",
      "namespaces": {
        "w": "http://schemas.openxmlformats.org/wordprocessingml/2006/main"
      },
      "xpath": "//w:r[w:t[normalize-space()='Arial font text']]/w:rPr/w:rFonts/@w:ascii",
      "must": "equal",
      "value": "Arial",
      "description": "The ASCII-script font slot must be exactly 'Arial'."
    }
  ],
  "render_assertions": [
    {
      "id": "font-family-text-present",
      "kind": "css_selector",
      "selector": ".docx-wrapper span, .docx-wrapper p",
      "must": "match-text",
      "value": "Arial font text",
      "description": "The rendered DOM must contain the fixture text."
    },
    {
      "id": "font-family-is-arial",
      "kind": "computed_style",
      "selector": ".docx-wrapper span:not(:has(*))",
      "style_property": "font-family",
      "value": "Arial",
      "description": "The computed font-family on the run must include Arial."
    }
  ]
}

Fixture

Download font-family.docx (18.6 KB)

Reference preview

Reference (machine, page 1 PNG)

docx/font-family page 1 reference

Reference PDF


Download PDF Open in PDF.js

Spec notes

<w:rFonts/> carries per-script font slot attributes: w:ascii, w:hAnsi, w:cs, w:eastAsia (plus theme-named w:asciiTheme, etc.). Setting a single font name via python-docx's run.font.name populates w:ascii and w:hAnsi with the same value; this fixture asserts on the authoritative ASCII slot.