fields
fields
¶
The Field object and the instruction builders that go with it.
A field is how Word represents anything it works out for itself: page numbers, a table of contents, cross-references, captions that renumber, dates, and references to document properties. Every one of those is the same feature with a different instruction string.
Word writes a field in one of two forms. A simple field is self-contained — the
instruction is an attribute of w:fldSimple and the cached result is its content:
A complex field is spread across sibling runs, delimited by field characters:
<w:r><w:fldChar w:fldCharType="begin"/></w:r>
<w:r><w:instrText xml:space="preserve"> PAGE </w:instrText></w:r>
<w:r><w:fldChar w:fldCharType="separate"/></w:r>
<w:r><w:t>7</w:t></w:r>
<w:r><w:fldChar w:fldCharType="end"/></w:r>
Both forms are read here and Field presents them the same way. Complex fields nest —
a TOC result is full of PAGEREF fields — and the nesting is tracked, so an inner
field is a field in its own right and its text also counts towards the outer field's
result.
This library cannot compute a field result. A table of contents added here is empty,
a PAGE field has no number, and a cross-reference shows nothing, because all three
depend on how Word lays the document out. Fields are written with w:dirty="true" so
Word refreshes them when it opens the document; setting
Settings.update_fields_on_open asks it to refresh every field in the document,
which is what a generated table of contents needs. No amount of API changes this.
Field
¶
Field(
element: CT_SimpleField | CT_FldChar,
parent: ProvidesStoryPart,
instruction: str = "",
result_text: str = "",
)
Bases: StoryChild
A field in a document — a page number, a table of contents, a cross-reference.
Not constructed directly; reached through Paragraph.fields, Document.fields or as the return value of Paragraph.add_field.
Source code in src/docx/fields.py
dirty
property
writable
¶
True when Word will refresh this field the next time it opens the document.
Read/write. A field written by this library is dirty by default, since its cached result is empty and only Word can fill it in.
instruction
property
¶
The field instruction, e.g. ' TOC \\o "1-3" \\h '.
This is the whole instruction including its switches, with the surrounding
spaces Word writes. It is the concatenation of every w:instrText of a complex
field, so an instruction Word split across runs reads as one string here.
result_text
property
¶
The result Word last rendered for this field, the empty string if none.
A field this library has just added has no result: only Word can compute one.
type
property
¶
The field type in upper case — "PAGE", "TOC", "REF" — or None.
This is the first token of the instruction. None when the instruction is empty
or begins with a switch, which is malformed but does occur.
A plain string rather than an enumeration: ISO/IEC 29500 defines around ninety field types and Word accepts more, so a closed set would reject valid documents.
_ComplexFieldBuilder
¶
_ComplexFieldBuilder(begin: CT_FldChar, position: int)
Accumulates the parts of one complex field while its subtree is walked.
Source code in src/docx/fields.py
iter_fields
¶
iter_fields(
element: _Element, parent: ProvidesStoryPart
) -> Iterator[Field]
Generate a Field for each field in the subtree of element.
Fields appear in document order, outermost first. A complex field nested inside
another — a PAGEREF in a table-of-contents entry — is generated in its own right,
after the field containing it.
A complex field with no "end" field-character is malformed and is skipped rather than raising: such a document exists in the wild and reading the fields that are well-formed is more useful than refusing the whole document.
Source code in src/docx/fields.py
new_complex_field
¶
The runs of a complex field for instruction, ready to append to a paragraph.
No "separate" field-character is written, because there is no cached result to put
after one; Word adds both when it computes the result. dirty sets w:dirty on the
"begin" field-character, asking Word to refresh the field when it opens the file.
Source code in src/docx/fields.py
page_number
¶
page_count
¶
table_of_contents
¶
table_of_contents(
levels: tuple[int, int] = (1, 3),
hyperlinks: bool = True,
use_outline_levels: bool = True,
hide_tab_and_page_numbers_in_web: bool = True,
) -> str
A TOC field instruction, the switches matching what Word's own dialog writes.
levels is the inclusive range of heading levels to include. hyperlinks makes
each entry a link to its heading (\h), use_outline_levels includes paragraphs
given an outline level without a heading style (\u), and
hide_tab_and_page_numbers_in_web is Word's \z, which suppresses the leader and
page number in web layout where there are no pages.
The table is empty until Word builds it; see the module docstring.
Source code in src/docx/fields.py
cross_reference
¶
cross_reference(
bookmark: str,
hyperlink: bool = True,
insert_paragraph_number: bool = False,
) -> str
A REF field instruction referring to bookmark.
hyperlink makes the reference clickable (\h), and insert_paragraph_number
shows the referenced paragraph's number rather than its text (\n).
The bookmark must exist in the document, or Word displays "Error! Bookmark not defined." See Paragraph.add_bookmark for creating one.
Source code in src/docx/fields.py
page_reference
¶
A PAGEREF field instruction — the page number bookmark appears on.
sequence
¶
A SEQ field instruction — the caption numbering of the name series.
name is the caption label, conventionally "Figure", "Table" or "Equation". Word
numbers each series independently and renumbers the whole series when one is
inserted, which is the point of using a field rather than a typed number.
restart_at_heading_level restarts numbering at each heading of that level (\s),
giving the "Figure 3-2" style of numbering.
Source code in src/docx/fields.py
date
¶
A DATE field instruction, or SAVEDATE when save_date is True.
format is a Word date-time picture such as "d MMMM yyyy"; the document's
default format is used when it is omitted. Note a DATE field shows the date the
document was opened, not the date it was generated — for a fixed date, write the
text rather than a field.
Source code in src/docx/fields.py
doc_property
¶
A DOCPROPERTY field instruction showing document property name.
name is a built-in property such as "Title" or "Author", or the name of a
custom property; see Document.core_properties and
Document.custom_properties.
Source code in src/docx/fields.py
styleref
¶
A STYLEREF field instruction showing the nearest text in style_name.
This is how a running header repeats the current chapter title.