docx_plus.fields.simple¶
Insert OOXML complex fields (PAGE / NUMPAGES / SECTIONPAGES, DATE /
CREATEDATE, plus a generic passthrough for everything else). Each
function emits the canonical 5-run sequence
(begin / instrText / separate / result-text / end) with
xml:space="preserve" on the instruction and the cached result so
Word does not collapse field-syntax whitespace.
add_style_reference (v0.5) emits STYLEREF, the one cross-reference
that needs no bookmark: it resolves to the text of the nearest paragraph
carrying a given style, re-evaluated per page. That is what makes a
running header show the current chapter — verified against Word 2016,
where the same field renders Chapter: Architecture on page 1 and
Chapter: Operations on page 2.
Note its style argument takes the style name as Word shows it
("Heading 1", with the space), not the w:styleId — unlike most of
this library, because that is what the field instruction accepts. An
int is an outline level instead.
Architecture walkthrough: Fields and protection.
docx_plus.fields.simple ¶
Insert OOXML complex fields (PAGE, DATE, generic) into paragraphs.
Word fields use the complex field syntax: a sequence of runs containing
w:fldChar markers (begin/separate/end) bracketing the field
instruction text (w:instrText) and the result text (w:t). This
module emits that five-run sequence and appends it to a paragraph.
Word recalculates field results on open only if w:updateFields is set in
settings.xml — see :func:docx_plus.fields.update.mark_fields_dirty.
Initial text supplied here is what Word shows before it recalculates, so
the value is meaningful for offline viewers (e.g. "1" for a PAGE field).
This module imports only from docx_plus.core (SPEC §9.1).
add_page_number_field ¶
add_page_number_field(
paragraph: Paragraph,
*,
field: PageFieldName = "PAGE",
format: str | None = None,
) -> etree._Element
Append a page-number complex field to paragraph.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
paragraph
|
Paragraph
|
A python-docx :class: |
required |
field
|
PageFieldName
|
Which page-number variant. |
'PAGE'
|
format
|
str | None
|
Optional field switches appended to the instruction. Example:
|
None
|
Returns:
| Type | Description |
|---|---|
_Element
|
The begin |
Example
from docx import Document from docx_plus.fields import add_page_number_field doc = Document() p = doc.add_paragraph("Page ") _ = add_page_number_field(p)
Source code in docx_plus/fields/simple.py
add_date_field ¶
add_date_field(
paragraph: Paragraph,
*,
format: str = "MMMM d, yyyy",
auto_update: bool = True,
) -> etree._Element
Append a date complex field to paragraph.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
paragraph
|
Paragraph
|
A python-docx :class: |
required |
format
|
str
|
A Word date-format string. Common values:
|
'MMMM d, yyyy'
|
auto_update
|
bool
|
|
True
|
Returns:
| Type | Description |
|---|---|
_Element
|
The begin |
Example
from docx import Document from docx_plus.fields import add_date_field doc = Document() p = doc.add_paragraph("Today: ") _ = add_date_field(p, format="M/d/yyyy")
Source code in docx_plus/fields/simple.py
add_style_reference ¶
add_style_reference(
paragraph: Paragraph,
*,
style: str | int,
search_from_bottom: bool = False,
number: StyleRefNumber | None = None,
position: bool = False,
suppress_non_delimiters: bool = False,
preserve_formatting: bool = True,
) -> etree._Element
Append a STYLEREF complex field to paragraph.
STYLEREF resolves to the text of the nearest paragraph carrying a
given style — the field behind a running header that shows the
current chapter title, and the one cross-reference kind that needs no
bookmark at all. Word re-resolves it per page, so the same field in a
header renders differently on every page.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
paragraph
|
Paragraph
|
A python-docx :class: |
required |
style
|
str | int
|
The style to search for. A string is the style's name
as Word shows it ( |
required |
search_from_bottom
|
bool
|
Append |
False
|
number
|
StyleRefNumber | None
|
Resolve to the matched paragraph's number rather than
its text, with the given amount of context — see
:data: |
None
|
position
|
bool
|
Append |
False
|
suppress_non_delimiters
|
bool
|
Append |
False
|
preserve_formatting
|
bool
|
Append |
True
|
Returns:
| Type | Description |
|---|---|
_Element
|
The begin |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Example
from docx import Document from docx_plus.fields import add_style_reference, mark_fields_dirty doc = Document() header = doc.sections[0].header.paragraphs[0] _ = add_style_reference(header, style="Heading 1") mark_fields_dirty(doc)
Notes
Like every field, this renders blank until Word recalculates.
Pair it with :func:docx_plus.fields.mark_fields_dirty.
Source code in docx_plus/fields/simple.py
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 | |
add_field ¶
Append a generic complex field to paragraph.
Use this for fields without a dedicated helper (TOC, REF,
HYPERLINK, MERGEFIELD, etc.). The instruction is wrapped in
leading/trailing spaces if you don't supply them, since Word's field
parser requires them.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
paragraph
|
Paragraph
|
A python-docx :class: |
required |
instruction
|
str
|
The raw field instruction text without the surrounding
|
required |
initial_text
|
str
|
Optional placeholder shown before Word recalculates. |
''
|
Returns:
| Type | Description |
|---|---|
_Element
|
The begin |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Example
from docx import Document from docx_plus.fields import add_field doc = Document() p = doc.add_paragraph() _ = add_field(p, instruction='TOC \o "1-3" \h', initial_text="(TOC)")