docx/italic-text

Apply italic formatting to a run of text.

Library verdicts: python-docx: — docxjs: pass

Metadata

Feature iddocx/italic-text
Formatdocx
Categorytext-formatting
Spececma-376-5-part-1 § 17.3.2.16 w:i

XPath assertions

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

Render assertions

IDPredicateSelectorpython-docxdocxjs
italic-text-renderedcss_selector/ match-text = Hello italic world
A DOM node marked italic (via <i>/<em> or font-style:italic) must contain the fixture text.
.docx-wrapper :is(i, em), .docx-wrapper [style*='font-style: italic'], .docx-wrapper [style*='font-style:italic']pass
italic-text-visiblecomputed_style = ^italic$ style=font-style
The computed font-style on the italic run must be italic.
.docx-wrapper :is(i, em, 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_italic_text.py

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

Satisfies the assertions in ``features/docx/italic-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" / "italic-text.docx"


def main() -> int:
    doc = Document()
    paragraph = doc.add_paragraph()
    run = paragraph.add_run("Hello italic world")
    run.italic = 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/italic-text",
  "title": "Italic text",
  "format": "docx",
  "category": "text-formatting",
  "summary": "Apply italic formatting to a run of text.",
  "spec": {
    "source": "ecma-376-5-part-1",
    "clause": "17.3.2.16",
    "element": "w:i",
    "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:i/> for italic, usually paired with <w:iCs/> for italic complex-script. Empty element means true. w:val is optional; if present, it must not explicitly disable italic (0/false/off)."
  },
  "fixtures": {
    "machine": "docx/italic-text",
    "office": "docx/italic-text"
  },
  "generator": {
    "python": "scripts/gen_italic_text.py"
  },
  "assertions": [
    {
      "id": "italic-element-present",
      "part": "word/document.xml",
      "namespaces": {
        "w": "http://schemas.openxmlformats.org/wordprocessingml/2006/main"
      },
      "xpath": "//w:r[w:t[normalize-space()='Hello italic world']]/w:rPr/w:i",
      "must": "exist",
      "description": "The run carrying the italic text must have a <w:i> child inside <w:rPr>."
    },
    {
      "id": "italic-val-not-false",
      "part": "word/document.xml",
      "namespaces": {
        "w": "http://schemas.openxmlformats.org/wordprocessingml/2006/main"
      },
      "xpath": "//w:r[w:t[normalize-space()='Hello italic world']]/w:rPr/w:i/@w:val",
      "must": "not-match",
      "value": "^(0|false|off)$",
      "description": "If w:val is present it must not explicitly disable italic."
    }
  ],
  "render_assertions": [
    {
      "id": "italic-text-rendered",
      "kind": "css_selector",
      "selector": ".docx-wrapper :is(i, em), .docx-wrapper [style*='font-style: italic'], .docx-wrapper [style*='font-style:italic']",
      "must": "match-text",
      "value": "Hello italic world",
      "description": "A DOM node marked italic (via <i>/<em> or font-style:italic) must contain the fixture text."
    },
    {
      "id": "italic-text-visible",
      "kind": "computed_style",
      "selector": ".docx-wrapper :is(i, em, span):not(:has(*))",
      "style_property": "font-style",
      "value": "^italic$",
      "description": "The computed font-style on the italic run must be italic."
    },
    {
      "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 italic-text.docx (18.6 KB)

Reference preview

Reference (machine, page 1 PNG)

docx/italic-text page 1 reference

Reference PDF


Download PDF Open in PDF.js

Spec notes

Word emits <w:i/> for italic, usually paired with <w:iCs/> for italic complex-script. Empty element means true. w:val is optional; if present, it must not explicitly disable italic (0/false/off).