docx_plus.notes.write¶
Insert footnotes and endnotes. Both share the same shape: a reference
marker run in the body, plus a content entry in the corresponding
separate part (word/footnotes.xml or word/endnotes.xml, created on
first use via core.get_or_create_part). edit_footnote / edit_endnote
replace the body text of an existing note in place; reserved ids (-1
separator, 0 continuation-separator) are not editable.
Architecture walkthrough: ARCHITECTURE.md §7.9.
docx_plus.notes.write ¶
Insert footnotes and endnotes.
Footnotes and endnotes share the same shape: a reference marker
<w:r><w:rPr><w:rStyle val=".."/></w:rPr><w:footnoteReference|w:endnoteReference w:id=N/></w:r>
in the body, plus a content entry in the corresponding separate part
(word/footnotes.xml or word/endnotes.xml).
Insert-only is sufficient for v0.2 — in-place edits of existing notes are deferred to v0.3.
This module imports only from docx_plus.core and the sibling
docx_plus.notes.registry (SPEC §9.1).
FootnoteRef
dataclass
¶
Handle for an inserted footnote.
EndnoteRef
dataclass
¶
Handle for an inserted endnote.
NoteNotFoundError ¶
Bases: DocxPlusError, KeyError
Raised when no footnote / endnote with the requested id exists.
Subclasses :class:KeyError so existing except KeyError: clauses
still catch it; also :class:DocxPlusError per SPEC §9.7.
add_footnote ¶
add_footnote(
paragraph: Paragraph,
text: str,
*,
id_registry: FootnoteIdRegistry | None = None,
) -> FootnoteRef
Append a footnote reference to paragraph and a footnote body to footnotes.xml.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
paragraph
|
Paragraph
|
A python-docx :class: |
required |
text
|
str
|
Footnote body text. Whitespace is preserved. |
required |
id_registry
|
FootnoteIdRegistry | None
|
Pre-existing :class: |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
A |
FootnoteRef
|
class: |
FootnoteRef
|
to the note body element. |
Example
from docx import Document from docx_plus.notes import add_footnote doc = Document() p = doc.add_paragraph("See the footnote.") add_footnote(p, "Explanatory text.")
Source code in docx_plus/notes/write.py
add_endnote ¶
add_endnote(
paragraph: Paragraph,
text: str,
*,
id_registry: EndnoteIdRegistry | None = None,
) -> EndnoteRef
Append an endnote reference to paragraph and an endnote body to endnotes.xml.
Same shape as :func:add_footnote but writes the endnote variants
of the reference elements and the endnotes part.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
paragraph
|
Paragraph
|
A python-docx :class: |
required |
text
|
str
|
Endnote body text. |
required |
id_registry
|
EndnoteIdRegistry | None
|
Pre-existing :class: |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
A |
EndnoteRef
|
class: |
EndnoteRef
|
to the note body element. |
Source code in docx_plus/notes/write.py
edit_footnote ¶
Replace the body text of an existing footnote in place.
Removes all child block-level content of the matching <w:footnote>
element and appends a fresh paragraph with text. The reference
glyph run is rebuilt as part of the new paragraph; the body-side
reference marker run in the main document is untouched.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
doc
|
Document
|
The python-docx :class: |
required |
note_id
|
int
|
The |
required |
text
|
str
|
New footnote body text. Whitespace is preserved. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
NoteNotFoundError
|
If no footnote with |
Source code in docx_plus/notes/write.py
edit_endnote ¶
Replace the body text of an existing endnote in place.
Same shape as :func:edit_footnote but targets the endnotes part.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
doc
|
Document
|
The python-docx :class: |
required |
note_id
|
int
|
The |
required |
text
|
str
|
New endnote body text. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
NoteNotFoundError
|
If no endnote with |