docx_plus.publishing.captions¶
Figure / table captions: a literal label run ("Figure ",
"Table ", etc.) followed by a SEQ complex field that
auto-numbers items sharing the same caption type. The caption type
is the same name a downstream Table of Figures uses to find
captions (see
docx_plus.publishing.figures).
Making a caption referenceable¶
A REF field cannot point at a SEQ field — only at a bookmark. So
"see Figure 3" is not expressible against a bare caption: there is
nothing for the reference to target. bookmark_name (v0.5) closes that
gap by bracketing the label and number in a bookmark.
from docx_plus.bookmarks import add_cross_reference
from docx_plus.publishing import add_caption
cap = doc.add_paragraph()
add_caption(cap, caption_type="Figure", bookmark_name="fig_arch")
cap.add_run(": Architecture overview") # stays outside the bookmark
body = doc.add_paragraph("As shown in ")
add_cross_reference(body, bookmark="fig_arch") # -> "Figure 1"
The bookmark spans exactly the label run plus the SEQ field — the same
extent as Word's own "Only label and number" option — so the reference
resolves to Figure 1, not to the description. Anything added with
add_run after the call falls outside it.
For an anchor the reader never sees, mint a hidden name with
BookmarkNameRegistry.next_ref_name.
Architecture walkthrough: Publishing.
docx_plus.publishing.captions ¶
Figure / table captions — leading text + SEQ complex field.
A Word caption is a paragraph that opens with a label run (e.g.
"Figure ") followed by a SEQ complex field that auto-numbers
items of the same caption type. The Table of Figures (see
docx_plus.publishing.figures) picks up captions whose SEQ name
matches its \c switch.
Making a caption referenceable ("see Figure 3") needs one more thing,
and it is not obvious: a REF field cannot point at a SEQ field.
It can only point at a bookmark. So the caption has to be bracketed
by one, which is what bookmark_name does — see
:func:add_caption.
This module imports only from docx_plus.core (SPEC §9.1).
add_caption ¶
add_caption(
paragraph: Paragraph,
label: str | None = None,
*,
caption_type: str = "Figure",
numbering: str = "ARABIC",
bookmark_name: str | None = None,
bookmark_id_registry: BookmarkIdRegistry | None = None,
) -> etree._Element
Append a caption (label run + auto-numbered SEQ field).
The label is emitted as a literal text run; the number is a
SEQ complex field that Word re-numbers on open. After all
captions are inserted, call
:func:docx_plus.fields.mark_fields_dirty so Word recalculates
the SEQ values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
paragraph
|
Paragraph
|
A python-docx :class: |
required |
label
|
str | None
|
Leading text shown before the number, including any
trailing whitespace. When omitted ( |
None
|
caption_type
|
str
|
The |
'Figure'
|
numbering
|
str
|
Word numbering format token for the |
'ARABIC'
|
bookmark_name
|
str | None
|
Bracket the label and number in a bookmark of this name, making the caption referenceable. Needed because a The bookmark spans exactly the label run plus the For an anchor the reader never sees, mint a hidden Word-style
name with
:meth: |
None
|
bookmark_id_registry
|
BookmarkIdRegistry | None
|
Pre-existing bookmark-id allocator to share
across an editing session. Only consulted when
|
None
|
Returns:
| Type | Description |
|---|---|
_Element
|
The |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Note
The caption's paragraph is not automatically restyled to
Word's built-in Caption paragraph style. Apply it yourself
if you want the conventional italic-grey rendering:
paragraph.style = doc.styles["Caption"].
Example
from docx import Document from docx_plus.publishing import add_caption doc = Document() p = doc.add_paragraph() add_caption(p, caption_type="Figure") # label defaults to "Figure " p.add_run(": Architecture overview")
Example
A referenceable caption, and the reference to it::
>>> from docx_plus.bookmarks import add_cross_reference
>>> from docx_plus.publishing import add_caption
>>> doc = Document()
>>> cap = doc.add_paragraph()
>>> _ = add_caption(cap, bookmark_name="fig_arch")
>>> cap.add_run(": Architecture overview")
<docx.text.run.Run object at 0x...>
>>> body = doc.add_paragraph("As shown in ")
>>> _ = add_cross_reference(body, bookmark="fig_arch")
Source code in docx_plus/publishing/captions.py
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 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 | |