docx_plus.bookmarks.crossref¶
Cross-references to bookmarks via REF / PAGEREF complex fields.
Both are built on top of the same core.build_complex_field plumbing
that fields/simple.py uses for page numbers and dates. Pass
kind="text" for REF (resolves to the bookmark's text content) or
kind="page" for PAGEREF (resolves to the page number). The \h
flag is appended by default so Word renders the cross-reference as a
clickable link to the bookmark.
The switches carry more weight than they look. Verified against Word 2016, the same bookmark yields:
| Call | Resolves to |
|---|---|
add_cross_reference(p, bookmark=fig) |
Figure 1 |
..., kind="page" |
1 |
..., position=True |
above |
..., number="relative" |
the target's paragraph number, e.g. 2.3 |
The first row is only useful if something bookmarked the caption —
a REF cannot point at a SEQ field. See bookmark_name on
add_caption.
For a cross-reference that needs no bookmark at all, see
add_style_reference (STYLEREF).
Pair calls with docx_plus.fields.mark_fields_dirty so Word
recalculates the cached results on first open.
docx_plus.bookmarks.crossref ¶
Cross-references to bookmarks via REF / PAGEREF fields.
REF bookmark_name inserts the text Word reads from the bookmark's
range; PAGEREF bookmark_name inserts the page number Word renders
for the bookmark. Both are complex fields built on top of the same
plumbing :mod:docx_plus.fields uses for page numbers and dates.
The switches matter more than they look. A bare REF yields the
bookmark's text, which is what "see Figure 3" wants when the bookmark
brackets a caption's label and number — see
:func:docx_plus.publishing.add_caption's bookmark_name. Add \p
and the same reference yields "above" or "below" instead; add \r and
it yields the target paragraph's number, which is what a numbered
heading or a list built with :mod:docx_plus.numbering gives you.
This module imports only from docx_plus.core (SPEC §9.1).
add_cross_reference ¶
add_cross_reference(
paragraph: Paragraph,
*,
bookmark: str,
kind: CrossReferenceKind = "text",
hyperlink: bool = True,
number: NumberContext | None = None,
position: bool = False,
suppress_non_delimiters: bool = False,
numeric_format: str | None = None,
preserve_formatting: bool = False,
) -> etree._Element
Append a cross-reference complex field to paragraph.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
paragraph
|
Paragraph
|
A python-docx :class: |
required |
bookmark
|
str
|
The target bookmark's |
required |
kind
|
CrossReferenceKind
|
|
'text'
|
hyperlink
|
bool
|
|
True
|
number
|
NumberContext | None
|
Resolve to the target 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
|
numeric_format
|
str | None
|
A |
None
|
preserve_formatting
|
bool
|
Append |
False
|
Returns:
| Type | Description |
|---|---|
_Element
|
The begin |
_Element
|
contract as :func: |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Example
from docx import Document from docx_plus.bookmarks import add_bookmark, add_cross_reference doc = Document() p1 = doc.add_paragraph("Section 1") _ = add_bookmark(p1, "sec_1") p2 = doc.add_paragraph("See ") _ = add_cross_reference(p2, bookmark="sec_1", kind="text") p2.add_run(" on page ")
_ = add_cross_reference(p2, bookmark="sec_1", kind="page")
Notes
Fields are cached: Word displays the previously-computed result
until w:updateFields="true" triggers recalculation on open.
Pair calls to :func:add_cross_reference with
:func:docx_plus.fields.mark_fields_dirty so the new
cross-references resolve on first open.
Source code in docx_plus/bookmarks/crossref.py
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 | |