Skip to content

Commit 5d97877

Browse files
Benjamin Toornstrascanny
authored andcommitted
docs: document Bookmarks analysis
1 parent 36cac78 commit 5d97877

2 files changed

Lines changed: 348 additions & 0 deletions

File tree

Lines changed: 347 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,347 @@
1+
2+
Bookmarks
3+
=========
4+
5+
WordprocessingML allows zero or more *bookmark* items to be specified at an
6+
arbitrary location in a document.
7+
8+
A bookmark consists of a `w:bookmarkStart` element identified with both
9+
a `w:id` and `w:name` attribute, and a matching `w:bookmarkEnd` element
10+
having the same `w:id` value.
11+
12+
Taken as a whole (matching element pair) the bookmark has both an id and
13+
a name. The bookmark appears in the Word UI by its name; the presence and
14+
uniqueness of the name are both required. While used to match starts and
15+
ends, the id value is not stable across saves in the Word UI. The bookmark
16+
name should be used as the key value for lookups.
17+
18+
A bookmark delimits an arbitrary contiguous sequence of text in a document.
19+
It's start and end can be at either the block level (between paragraphs
20+
and/or tables) or in-between runs (between individual characters). A bookmark
21+
can also appear in a table.
22+
23+
Among the applications of bookmarks in Word is their use in captions and
24+
cross-references.
25+
26+
27+
Protocol
28+
--------
29+
30+
.. highlight:: python
31+
32+
Adding a bookmark::
33+
34+
>>> bookmarks = document.bookmarks
35+
>>> bookmarks
36+
<docx.text.bookmark.Bookmarks object at 0x...>
37+
>>> len(bookmarks)
38+
0
39+
>>> bookmark = document.start_bookmark('Target')
40+
>>> bookmark.name
41+
'Target'
42+
>>> bookmark.id
43+
1
44+
>>> len(bookmarks) # doesn't count until it's closed
45+
0
46+
47+
>>> document.add_paragraph() # etc. ...
48+
49+
>>> document.end_bookmark(bookmark)
50+
>>> len(bookmarks)
51+
1
52+
>>> bookmarks.get('Target')
53+
<docx.bookmark._Bookmark at 0x00fa1afe1>
54+
>>> bookmarks.get_by_id(1)
55+
<docx.bookmark._Bookmark at 0x00fa1afe1>
56+
>>> bookmarks[0]
57+
<docx.bookmark._Bookmark at 0x00fa1afe1>
58+
59+
# A bookmark can be deleted:
60+
>>> len(bookmarks)
61+
>>> 2
62+
>>> bookmark = bookmarks[0]
63+
>>> bookmark.delete()
64+
>>> len(bookmarks)
65+
>>> 1
66+
67+
68+
Word Behavior
69+
-------------
70+
71+
* The Word UI enforces the uniqueness of bookmark names.
72+
73+
* A bookmark having the same name as a prior bookmark (in document order) is
74+
ignored by Word.
75+
76+
* An unclosed bookmark (`w:bookmarkStart` without matching `w:bookmarkEnd`)
77+
is ignored by Word.
78+
79+
* A "reversed" bookmark (`w:bookmarkEnd` appears before matching
80+
`w:bookmarkStart`) is ignored by Word and removed on the next save (by
81+
Word).
82+
83+
* Word will change bookmark ids (while keeping start and end consistent) at
84+
its convenience. A bookmark id is not a stable key across document saves
85+
(in Word).
86+
87+
* In general, referents to a bookmark use the bookmark *name* as the key.
88+
This makes sense as the id is not a durable key.
89+
90+
* A bookmark can be *hidden*, which occurs for example when cross-references
91+
are inserted into the document.
92+
93+
* As bookmarks need to be unique over all document stories, a check should
94+
be done for uniqueness. (The word API replaces the bookmark by a new one
95+
when a duplicate bookmarkname is used to insert a new bookmark.
96+
The word editor removes duplicate bookmarks.)
97+
98+
* Bookmarks may overlap i.e. A new bookmark is started as the previous
99+
one is not yet ended.
100+
101+
* Bookmarks may be nested i.e. a bookmark may exists within the limits
102+
of another bookmark.
103+
104+
* A bookmark can be added in five different document parts: Body, Header,
105+
Footer, Footnote and Endnote.
106+
107+
* As bookmarks can be added in at different locations as well as different
108+
document parts, the bookmarkStart and bookmarkEnd elements should be added
109+
to different complex types: CT_Body, CT_P and CT_Tbl, as well as CT_HdrFtr
110+
and CT_FtnEdn.
111+
112+
113+
XML Semantics
114+
-------------
115+
116+
* The `w:bookmarkStart` element can use optional `w:colFirst` and `w:colLast`
117+
elements to bookmark specific parts of a table. If used, both should appear.
118+
119+
120+
Specimen XML
121+
------------
122+
123+
.. highlight:: xml
124+
125+
::
126+
127+
<w:p>
128+
<w:r>
129+
<w:t>Foo</w:t>
130+
</w:r>
131+
<w:bookmarkStart w:id="0" w:name="sampleBookmark" />
132+
<w:r>
133+
<w:t>bar</w:t>
134+
</w:r>
135+
</w:p>
136+
<w:p>
137+
<w:r>
138+
<w:t>Bar</w:t>
139+
</w:r>
140+
<w:bookmarkEnd w:id="0" />
141+
<w:r>
142+
<w:t>foo</w:t>
143+
</w:r>
144+
</w:p>
145+
146+
147+
MS API Protocol
148+
---------------
149+
150+
The MS API defines a `Bookmarks` object which is a collection of
151+
`Bookmark objects`
152+
153+
Bookmarks object:
154+
155+
https://msdn.microsoft.com/en-us/vba/word-vba/articles/bookmarks-object-word
156+
157+
Methods:
158+
* Bookmarks.Exists(name) - Checks if bookmark name exists in document.
159+
* Bookmarks.Item(index) - Returns bookmark based on id or name.
160+
161+
Properties:
162+
* Bookmarks.Count - Number of bookmarks
163+
164+
Bookmark objects:
165+
https://msdn.microsoft.com/en-us/vba/word-vba/articles/bookmark-object-word
166+
167+
Methods:
168+
* Bookmark.Delete() - Removing the two elements from the document
169+
170+
Properties:
171+
* Bookmark.Column (boolean) - True if bookmark is inside a table Column
172+
* Bookmark.Empty (boolean) - True if the specified bookmark is Empty
173+
* Bookmark.Name - Return name of bookmark.
174+
175+
Schema excerpt
176+
--------------
177+
178+
::
179+
180+
<xsd:complexType name="CT_Body"> <!-- denormalized -->
181+
<xsd:sequence>
182+
<xsd:element name="customXml" type="CT_CustomXmlBlock"/>
183+
<xsd:element name="sdt" type="CT_SdtBlock"/>
184+
<xsd:element name="p" type="CT_P" minOccurs="0" maxOccurs="unbounded"/>
185+
<xsd:element name="tbl" type="CT_Tbl" minOccurs="0" maxOccurs="unbounded"/>
186+
<xsd:element name="proofErr" minOccurs="0" type="CT_ProofErr"/>
187+
<xsd:element name="permStart" minOccurs="0" type="CT_PermStart"/>
188+
<xsd:element name="permEnd" minOccurs="0" type="CT_Perm"/>
189+
<xsd:group ref="EG_RangeMarkupElements" minOccurs="0" maxOccurs="unbounded"/>
190+
<xsd:element name="ins" type="CT_RunTrackChange" minOccurs="0"/>
191+
<xsd:element name="del" type="CT_RunTrackChange" minOccurs="0"/>
192+
<xsd:element name="moveFrom" type="CT_RunTrackChange"/>
193+
<xsd:element name="moveTo" type="CT_RunTrackChange"/>
194+
<xsd:choice>
195+
<xsd:element ref="m:oMathPara"/>
196+
<xsd:element ref="m:oMath"/>
197+
</xsd:choice>
198+
<xsd:element name="altChunk" type="CT_AltChunk" minOccurs="0" maxOccurs="unbounded"/>
199+
<xsd:element name="sectPr" minOccurs="0" maxOccurs="1" type="CT_SectPr"/>
200+
</xsd:sequence>
201+
</xsd:complexType>
202+
203+
<xsd:complexType name="CT_P"> <!-- denormalized -->
204+
<xsd:sequence>
205+
<xsd:element name="pPr" type="CT_PPr" minOccurs="0"/>
206+
<xsd:element name="customXml" type="CT_CustomXmlRun"/>
207+
<xsd:element name="smartTag" type="CT_SmartTagRun"/>
208+
<xsd:element name="sdt" type="CT_SdtRun"/>
209+
<xsd:element name="dir" type="CT_DirContentRun"/>
210+
<xsd:element name="bdo" type="CT_BdoContentRun"/>
211+
<xsd:element name="r" type="CT_R"/>
212+
<xsd:element name="proofErr" minOccurs="0" type="CT_ProofErr"/>
213+
<xsd:element name="permStart" minOccurs="0" type="CT_PermStart"/>
214+
<xsd:element name="permEnd" minOccurs="0" type="CT_Perm"/>
215+
<xsd:group ref="EG_RangeMarkupElements" minOccurs="0" maxOccurs="unbounded"/>
216+
<xsd:element name="ins" type="CT_RunTrackChange" minOccurs="0"/>
217+
<xsd:element name="del" type="CT_RunTrackChange" minOccurs="0"/>
218+
<xsd:element name="moveFrom" type="CT_RunTrackChange"/>
219+
<xsd:element name="moveTo" type="CT_RunTrackChange"/>
220+
<xsd:choice>
221+
<xsd:element ref="m:oMathPara"/>
222+
<xsd:element ref="m:oMath"/>
223+
</xsd:choice>
224+
<xsd:element name="fldSimple" type="CT_SimpleField" minOccurs="0" maxOccurs="unbounded"/>
225+
<xsd:element name="hyperlink" type="CT_Hyperlink"/>
226+
<xsd:element name="subDoc" type="CT_Rel"/>
227+
</xsd:sequence>
228+
<xsd:attribute name="rsidRPr" type="ST_LongHexNumber"/>
229+
<xsd:attribute name="rsidR" type="ST_LongHexNumber"/>
230+
<xsd:attribute name="rsidDel" type="ST_LongHexNumber"/>
231+
<xsd:attribute name="rsidP" type="ST_LongHexNumber"/>
232+
<xsd:attribute name="rsidRDefault" type="ST_LongHexNumber"/>
233+
</xsd:complexType>
234+
235+
<xsd:complexType name="CT_Tbl"> <!-- denormalized -->
236+
<xsd:sequence>
237+
<xsd:group ref="EG_RangeMarkupElements" minOccurs="0" maxOccurs="unbounded"/>
238+
<xsd:element name="tblPr" type="CT_TblPr"/>
239+
<xsd:element name="tblGrid" type="CT_TblGrid"/>
240+
<xsd:group ref="EG_ContentRowContent" minOccurs="0" maxOccurs="unbounded"/>
241+
</xsd:sequence>
242+
</xsd:complexType>
243+
244+
<xsd:complexType name="CT_HdrFtr"> <!-- denormalized -->
245+
<xsd:element name="customXml" type="CT_CustomXmlBlock"/>
246+
<xsd:element name="sdt" type="CT_SdtBlock"/>
247+
<xsd:element name="p" type="CT_P" minOccurs="0" maxOccurs="unbounded"/>
248+
<xsd:element name="tbl" type="CT_Tbl" minOccurs="0" maxOccurs="unbounded"/>
249+
<xsd:element name="proofErr" minOccurs="0" type="CT_ProofErr"/>
250+
<xsd:element name="permStart" minOccurs="0" type="CT_PermStart"/>
251+
<xsd:element name="permEnd" minOccurs="0" type="CT_Perm"/>
252+
<xsd:group ref="EG_RangeMarkupElements" minOccurs="0" maxOccurs="unbounded"/>
253+
<xsd:element name="ins" type="CT_RunTrackChange" minOccurs="0"/>
254+
<xsd:element name="del" type="CT_RunTrackChange" minOccurs="0"/>
255+
<xsd:element name="moveFrom" type="CT_RunTrackChange"/>
256+
<xsd:element name="moveTo" type="CT_RunTrackChange"/>
257+
<xsd:choice>
258+
<xsd:element ref="m:oMathPara"/>
259+
<xsd:element ref="m:oMath"/>
260+
</xsd:choice>
261+
<xsd:element name="altChunk" type="CT_AltChunk" minOccurs="0" maxOccurs="unbounded"/>
262+
</xsd:complexType>
263+
264+
<xsd:complexType name="CT_FtnEdn"> <!-- denormalized -->
265+
<xsd:sequence>
266+
<xsd:element name="customXml" type="CT_CustomXmlBlock"/>
267+
<xsd:element name="sdt" type="CT_SdtBlock"/>
268+
<xsd:element name="p" type="CT_P" minOccurs="0" maxOccurs="unbounded"/>
269+
<xsd:element name="tbl" type="CT_Tbl" minOccurs="0" maxOccurs="unbounded"/>
270+
<xsd:element name="proofErr" minOccurs="0" type="CT_ProofErr"/>
271+
<xsd:element name="permStart" minOccurs="0" type="CT_PermStart"/>
272+
<xsd:element name="permEnd" minOccurs="0" type="CT_Perm"/>
273+
<xsd:group ref="EG_RangeMarkupElements" minOccurs="0" maxOccurs="unbounded"/>
274+
<xsd:element name="ins" type="CT_RunTrackChange" minOccurs="0"/>
275+
<xsd:element name="del" type="CT_RunTrackChange" minOccurs="0"/>
276+
<xsd:element name="moveFrom" type="CT_RunTrackChange"/>
277+
<xsd:element name="moveTo" type="CT_RunTrackChange"/>
278+
<xsd:choice>
279+
<xsd:element ref="m:oMathPara"/>
280+
<xsd:element ref="m:oMath"/>
281+
</xsd:choice>
282+
<xsd:element name="altChunk" type="CT_AltChunk" minOccurs="0" maxOccurs="unbounded"/>
283+
</xsd:sequence>
284+
<xsd:attribute name="type" type="ST_FtnEdn" use="optional"/>
285+
<xsd:attribute name="id" type="ST_DecimalNumber" use="required"/>
286+
</xsd:complexType>
287+
288+
<xsd:group name="EG_RangeMarkupElements">
289+
<xsd:choice>
290+
<xsd:element name="bookmarkStart" type="CT_Bookmark"/>
291+
<xsd:element name="bookmarkEnd" type="CT_MarkupRange"/>
292+
<xsd:element name="moveFromRangeStart" type="CT_MoveBookmark"/>
293+
<xsd:element name="moveFromRangeEnd" type="CT_MarkupRange"/>
294+
<xsd:element name="moveToRangeStart" type="CT_MoveBookmark"/>
295+
<xsd:element name="moveToRangeEnd" type="CT_MarkupRange"/>
296+
<xsd:element name="commentRangeStart" type="CT_MarkupRange"/>
297+
<xsd:element name="commentRangeEnd" type="CT_MarkupRange"/>
298+
<xsd:element name="customXmlInsRangeStart" type="CT_TrackChange"/>
299+
<xsd:element name="customXmlInsRangeEnd" type="CT_Markup"/>
300+
<xsd:element name="customXmlDelRangeStart" type="CT_TrackChange"/>
301+
<xsd:element name="customXmlDelRangeEnd" type="CT_Markup"/>
302+
<xsd:element name="customXmlMoveFromRangeStart" type="CT_TrackChange"/>
303+
<xsd:element name="customXmlMoveFromRangeEnd" type="CT_Markup"/>
304+
<xsd:element name="customXmlMoveToRangeStart" type="CT_TrackChange"/>
305+
<xsd:element name="customXmlMoveToRangeEnd" type="CT_Markup"/>
306+
</xsd:choice>
307+
</xsd:group>
308+
309+
<xsd:complexType name="CT_Bookmark"> <!-- denormalized -->
310+
<xsd:attribute name="id" type="ST_DecimalNumber" use="required"/>
311+
<xsd:attribute name="name" type="s:ST_String" use="required"/>
312+
<xsd:attribute name="displacedByCustomXml" type="ST_DisplacedByCustomXml"/>
313+
<xsd:attribute name="colFirst" type="ST_DecimalNumber"/>
314+
<xsd:attribute name="colLast" type="ST_DecimalNumber"/>
315+
</xsd:complexType>
316+
317+
<xsd:complexType name="CT_MarkupRange"> <!-- denormalized -->
318+
<xsd:attribute name="id" type="ST_DecimalNumber" use="required"/>
319+
<xsd:attribute name="displacedByCustomXml" type="ST_DisplacedByCustomXml"/>
320+
</xsd:complexType>
321+
322+
<xsd:complexType name="CT_Endnotes">
323+
<xsd:sequence maxOccurs="unbounded">
324+
<xsd:element name="endnote" type="CT_FtnEdn" minOccurs="0"/>
325+
</xsd:sequence>
326+
</xsd:complexType>
327+
328+
<xsd:complexType name="CT_Footnotes">
329+
<xsd:sequence maxOccurs="unbounded">
330+
<xsd:element name="footnote" type="CT_FtnEdn" minOccurs="0"/>
331+
</xsd:sequence>
332+
</xsd:complexType>
333+
334+
<xsd:complexType name="CT_HdrFtr">
335+
<xsd:group ref="EG_BlockLevelElts" minOccurs="1" maxOccurs="unbounded"/>
336+
</xsd:complexType>
337+
338+
<xsd:simpleType name="ST_DecimalNumber">
339+
<xsd:restriction base="xsd:integer"/>
340+
</xsd:simpleType>
341+
342+
<xsd:simpleType name="ST_DisplacedByCustomXml">
343+
<xsd:restriction base="xsd:string">
344+
<xsd:enumeration value="next"/>
345+
<xsd:enumeration value="prev"/>
346+
</xsd:restriction>
347+
</xsd:simpleType>

‎docs/dev/analysis/index.rst‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Feature Analysis
1010
.. toctree::
1111
:titlesonly:
1212

13+
features/bookmarks
1314
features/header
1415
features/settings
1516
features/text/index

0 commit comments

Comments
 (0)