Set a run's typeface to a specific font family (ASCII script slot).
Library verdicts: python-docx: — docxjs: pass
| Feature id | docx/font-family |
|---|---|
| Format | docx |
| Category | text-formatting |
| Spec | ecma-376-5-part-1 § 17.3.2.25 w:rFonts |
| ID / part | Predicate | XPath | python-docx | docxjs |
|---|---|---|---|---|
rfonts-element-presentword/document.xml | existThe 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-arialword/document.xml | equal = ArialThe ASCII-script font slot must be exactly 'Arial'. | //w:r[w:t[normalize-space()='Arial font text']]/w:rPr/w:rFonts/@w:ascii | — | — |
| ID | Predicate | Selector | python-docx | docxjs |
|---|---|---|---|---|
font-family-text-present | css_selector/ match-text = Arial font textThe rendered DOM must contain the fixture text. | .docx-wrapper span, .docx-wrapper p | — | pass |
font-family-is-arial | computed_style = Arial style=font-familyThe computed font-family on the run must include Arial. | .docx-wrapper span:not(:has(*)) | — | pass |
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())
{
"$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."
}
]
}
