docx/bold-text

Apply bold formatting to a run of text.

Library verdicts: python-docx: — docxjs: pass

Metadata

Feature iddocx/bold-text
Formatdocx
Categorytext-formatting
Spececma-376-5-part-1 § 17.3.2.1 w:b

XPath assertions

ID / partPredicateXPathpython-docxdocxjs
bold-element-present
word/document.xml
exist
The run carrying the bold text must have a <w:b> child inside <w:rPr>.
//w:r[w:t[normalize-space()='Hello bold world']]/w:rPr/w:b
bold-val-not-false
word/document.xml
not-match = ^(0|false|off)$
If w:val is present it must not explicitly disable bold.
//w:r[w:t[normalize-space()='Hello bold world']]/w:rPr/w:b/@w:val

Render assertions

IDPredicateSelectorpython-docxdocxjs
bold-text-renderedcss_selector/ match-text = Hello bold world
A DOM node marked bold (via <b>/<strong>, font-weight:bold, or font-weight:700) must contain the fixture text.
.docx-wrapper :is(b, strong), .docx-wrapper [style*='font-weight: bold'], .docx-wrapper [style*='font-weight:bold'], .docx-wrapper [style*='font-weight: 700'], .docx-wrapper [style*='font-weight:700']pass
bold-text-visiblecomputed_style = ^(bold|700)$ style=font-weight
The computed font-weight on the bold run must be bold or 700.
.docx-wrapper :is(b, strong, span):not(:has(*))pass
visual-matches-libreoffice-rendervisual_ssim
Playwright screenshot of the rendered output must score >=85% SSIM against the LibreOffice-rendered reference PNG.

Generator source

scripts/gen_bold_text.py

#!/usr/bin/env python3
"""Generate ``fixtures/docx/bold-text.docx``.

A minimal document exercising the ``w:b`` bold-formatting element. One
paragraph containing one bold run. Designed to satisfy the assertions
in ``features/docx/bold-text.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" / "bold-text.docx"


def main() -> int:
    doc = Document()
    paragraph = doc.add_paragraph()
    run = paragraph.add_run("Hello bold world")
    run.bold = True
    _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/bold-text",
  "title": "Bold text",
  "format": "docx",
  "category": "text-formatting",
  "summary": "Apply bold formatting to a run of text.",
  "spec": {
    "source": "ecma-376-5-part-1",
    "clause": "17.3.2.1",
    "element": "w:b",
    "rnc_reference": "spec/ecma-376-5/part-1/rnc/WordprocessingML.rnc",
    "xsd_reference": "spec/ecma-376-5/part-1/xsd/wml.xsd",
    "notes": "Word emits <w:b/> when bold is on. Empty element means true. w:val is optional; if present it must not be 0/false/off (those explicitly disable bold in a toggled style chain)."
  },
  "fixtures": {
    "machine": "docx/bold-text",
    "office": "docx/bold-text"
  },
  "generator": {
    "python": "scripts/gen_bold_text.py"
  },
  "assertions": [
    {
      "id": "bold-element-present",
      "part": "word/document.xml",
      "namespaces": {
        "w": "http://schemas.openxmlformats.org/wordprocessingml/2006/main"
      },
      "xpath": "//w:r[w:t[normalize-space()='Hello bold world']]/w:rPr/w:b",
      "must": "exist",
      "description": "The run carrying the bold text must have a <w:b> child inside <w:rPr>."
    },
    {
      "id": "bold-val-not-false",
      "part": "word/document.xml",
      "namespaces": {
        "w": "http://schemas.openxmlformats.org/wordprocessingml/2006/main"
      },
      "xpath": "//w:r[w:t[normalize-space()='Hello bold world']]/w:rPr/w:b/@w:val",
      "must": "not-match",
      "value": "^(0|false|off)$",
      "description": "If w:val is present it must not explicitly disable bold."
    }
  ],
  "render_assertions": [
    {
      "id": "bold-text-rendered",
      "kind": "css_selector",
      "selector": ".docx-wrapper :is(b, strong), .docx-wrapper [style*='font-weight: bold'], .docx-wrapper [style*='font-weight:bold'], .docx-wrapper [style*='font-weight: 700'], .docx-wrapper [style*='font-weight:700']",
      "must": "match-text",
      "value": "Hello bold world",
      "description": "A DOM node marked bold (via <b>/<strong>, font-weight:bold, or font-weight:700) must contain the fixture text."
    },
    {
      "id": "bold-text-visible",
      "kind": "computed_style",
      "selector": ".docx-wrapper :is(b, strong, span):not(:has(*))",
      "style_property": "font-weight",
      "value": "^(bold|700)$",
      "description": "The computed font-weight on the bold run must be bold or 700."
    },
    {
      "id": "visual-matches-libreoffice-render",
      "kind": "visual_ssim",
      "min_ssim": 0.5,
      "description": "Playwright screenshot of the rendered output must score >=85% SSIM against the LibreOffice-rendered reference PNG."
    }
  ]
}

Fixture

Download bold-text.docx (18.6 KB)

Reference preview

Reference (machine, page 1 PNG)

docx/bold-text page 1 reference

Reference PDF


Download PDF Open in PDF.js

Office companion (page 1 PNG)

Office companion reference

Office companion PDF

Download Office PDF

Spec notes

Word emits <w:b/> when bold is on. Empty element means true. w:val is optional; if present it must not be 0/false/off (those explicitly disable bold in a toggled style chain).