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.
Architecture walkthrough: ARCHITECTURE.md §7.
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_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)")