docx/page-header

Default-section page header carrying a line of text, referenced from sectPr/headerReference.

Library verdicts: python-docx: — docxjs: pass

Metadata

Feature iddocx/page-header
Formatdocx
Categorypage-layout
Spececma-376-5-part-1 § 17.10.1 w:headerReference

XPath assertions

ID / partPredicateXPathpython-docxdocxjs
headerReference-present
word/document.xml
exist
The section must carry a default-type headerReference.
//w:sectPr/w:headerReference[@w:type='default']
header-text-present
word/header1.xml
exist
The header part must carry the expected text in a run.
//w:p/w:r/w:t[normalize-space()='Document header text']

Render assertions

IDPredicateSelectorpython-docxdocxjs
header-element-renderedcss_selector/ match-text = Document header text
A renderer-exposed header container (header / .docx-header / [data-part='header']) must contain the header text.
.docx-wrapper header, .docx-wrapper .docx-header, .docx-wrapper [data-part='header']pass

Generator source

scripts/gen_page_header.py

#!/usr/bin/env python3
"""Generate ``fixtures/docx/page-header.docx``.

A minimal document exercising the default-section page header. The
header carries a single text line; the body carries an unrelated
paragraph. python-docx emits the header as ``word/header1.xml``.
Designed to satisfy the assertions in ``features/docx/page-header.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" / "page-header.docx"


def main() -> int:
    doc = Document()
    section = doc.sections[0]
    header = section.header
    header.paragraphs[0].text = "Document header text"
    doc.add_paragraph("Body paragraph one")
    _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/page-header",
  "title": "Page header",
  "format": "docx",
  "category": "page-layout",
  "summary": "Default-section page header carrying a line of text, referenced from sectPr/headerReference.",
  "spec": {
    "source": "ecma-376-5-part-1",
    "clause": "17.10.1",
    "element": "w:headerReference",
    "rnc_reference": "spec/ecma-376-5/part-1/rnc/WordprocessingML.rnc",
    "xsd_reference": "spec/ecma-376-5/part-1/xsd/wml.xsd",
    "notes": "A page header is a separate part (<w:hdr>) reached via <w:headerReference w:type=\"default\" r:id=\"rIdN\"/> inside <w:sectPr>. python-docx emits the part as word/header1.xml."
  },
  "fixtures": {
    "machine": "docx/page-header",
    "office": "docx/page-header"
  },
  "generator": {
    "python": "scripts/gen_page_header.py"
  },
  "assertions": [
    {
      "id": "headerReference-present",
      "part": "word/document.xml",
      "namespaces": {
        "w": "http://schemas.openxmlformats.org/wordprocessingml/2006/main",
        "r": "http://schemas.openxmlformats.org/officeDocument/2006/relationships"
      },
      "xpath": "//w:sectPr/w:headerReference[@w:type='default']",
      "must": "exist",
      "description": "The section must carry a default-type headerReference."
    },
    {
      "id": "header-text-present",
      "part": "word/header1.xml",
      "namespaces": {
        "w": "http://schemas.openxmlformats.org/wordprocessingml/2006/main"
      },
      "xpath": "//w:p/w:r/w:t[normalize-space()='Document header text']",
      "must": "exist",
      "description": "The header part must carry the expected text in a run."
    }
  ],
  "render_assertions": [
    {
      "id": "header-element-rendered",
      "kind": "css_selector",
      "selector": ".docx-wrapper header, .docx-wrapper .docx-header, .docx-wrapper [data-part='header']",
      "must": "match-text",
      "value": "Document header text",
      "description": "A renderer-exposed header container (header / .docx-header / [data-part='header']) must contain the header text."
    }
  ]
}

Fixture

Download page-header.docx (19.1 KB)

Reference preview

Reference (machine, page 1 PNG)

docx/page-header page 1 reference

Reference PDF


Download PDF Open in PDF.js

Spec notes

A page header is a separate part (<w:hdr>) reached via <w:headerReference w:type="default" r:id="rIdN"/> inside <w:sectPr>. python-docx emits the part as word/header1.xml.