docx.fields
paper-docx 0.2.0 API reference
Field authoring — formulas, never values.
A field is a formula; static text is a pasted value. This module authors PAGE/NUMPAGES/DATE simple fields, REF/PAGEREF cross-references, and the TOC complex field. Every inserted field carries placeholder result text and sets the document's update-fields-on-open flag: this package never computes a field's value — pagination and evaluation belong to a renderer (Word, or headless LibreOffice in the harness).
The in_field guard recognizes everything authored here, so a span
landing inside one of our own fields refuses exactly like one landing in
Word's (self-consistency, tested).
add_caption
funcadd_caption(paragraph: 'Paragraph', *, label: str = 'Figure', description: str = '') -> NoneAppend a SEQ caption (Figure 1, Table 1) to paragraph.
Writes the field rather than the displayed integer, so Word recomputes the number on open. Sets the paragraph style to "Caption" without checking that the style exists. Refuses a protected document and an unknown label.
paramparagraph'Paragraph'paramlabelstr= 'Figure'paramdescriptionstr= ''Returns
Noneadd_date_field
funcadd_date_field(paragraph: 'Paragraph', *, date_format: Optional[str] = None) -> NoneAppend a DATE field; date_format is Word's @ picture, for example 'MMMM d, yyyy'.
The result stays a placeholder until a renderer opens the file; this package never computes a date into the result. Refuses a protected document.
paramparagraph'Paragraph'paramdate_formatOptional[str]= NoneReturns
Noneadd_page_count_field
funcadd_page_count_field(paragraph: 'Paragraph') -> NoneAppend a NUMPAGES field, typically to a footer paragraph.
Writes the field, not a number, so Word computes the count on open. Refuses a protected document.
paramparagraph'Paragraph'Returns
Noneadd_page_number_field
funcadd_page_number_field(paragraph: 'Paragraph') -> NoneAppend a PAGE field, typically to a footer paragraph.
Writes the field, not a number, so Word computes the page on open. Refuses a protected document.
paramparagraph'Paragraph'Returns
Noneadd_reference_field
funcadd_reference_field(paragraph: 'Paragraph', *, bookmark: str, kind: str = 'text') -> NoneAppend a cross-reference to bookmark: its text (kind="text"), page ("page"), or
paragraph number ("number").
Writes a REF field, so Word recomputes it on open. Refuses a protected document and an unknown bookmark name.
paramparagraph'Paragraph'parambookmarkstrparamkindstr= 'text'Returns
Nonecheck_install
funccheck_install() -> NoneRefuse when paper-docx and python-docx are both installed.
Returns
Noneinsert_toc_after
funcinsert_toc_after(document: 'Document', anchor, *, levels: Tuple[int, int] = (1, 3)) -> NoneInsert a TOC field in a new paragraph after anchor.
Marked dirty so the renderer builds the real table on open; heading levels maps to the
\o "1-3" switch. Refuses a protected document, and an anchor that is missing, ambiguous
foreign, or spans more than one paragraph.
paramdocument'Document'paramanchorparamlevelsTuple[int, int]= (1, 3)Returns
Nonerollback_on_error
funcrollback_on_error(document: 'Document', *participants: Any) -> Generator[None, None, None]Restore the live package and named mutable proxies after an error.
paramdocument'Document'paramparticipantsAny= ()Returns
typing.Generator[None, None, None]