Skip to content

Commit 3b3713c

Browse files
author
Steve Canny
committed
docs: add user documentation for latent styles
1 parent 67cfed6 commit 3b3713c

3 files changed

Lines changed: 115 additions & 7 deletions

File tree

docs/conf.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@
109109
110110
.. |int| replace:: :class:`.int`
111111
112+
.. |_LatentStyle| replace:: :class:`._LatentStyle`
113+
112114
.. |LatentStyles| replace:: :class:`.LatentStyles`
113115
114116
.. |Length| replace:: :class:`.Length`

docs/user/styles-understanding.rst

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,23 @@ the style id unless they are confident with the internals involved.
132132
A style's :attr:`type` is set at creation time and cannot be changed.
133133

134134

135+
.. _builtin_styles:
136+
137+
Built-in styles
138+
---------------
139+
140+
Word comes with almost 300 so-called *built-in* styles like `Normal`,
141+
`Heading 1`, and `List Bullet`. Style definitions are stored in the
142+
`styles.xml` part of a .docx package, but built-in style definitions are
143+
stored in the Word application itself and are not written to `styles.xml`
144+
until they are actually used. This is a sensible strategy because they take
145+
up considerable room and would be largely redundant and useless overhead in
146+
every .docx file otherwise.
147+
148+
The fact that built-in styles are not written to the .docx package until used
149+
gives rise to the need for *latent style* definitions, explained below.
150+
151+
135152
.. _style_behavior:
136153

137154
Style Behavior
@@ -160,6 +177,27 @@ will not appear in any list or the style gallery and cannot be applied to
160177
content.
161178

162179

180+
.. _latent_styles:
181+
182+
Latent styles
183+
-------------
184+
185+
The need to specify the UI behavior of built-in styles not defined in
186+
`styles.xml` gives rise to the need for *latent style* definitions. A latent
187+
style definition is basically a stub style definition that has at most the
188+
five behavior attributes in addition to the style name. Additional space is
189+
saved by defining defaults for each of the behavior attributes, so only those
190+
that differ from the default need be defined and styles that match all
191+
defaults need no latent style definition.
192+
193+
Latent style definitions are specified using the `w:latentStyles` and
194+
`w:lsdException` elements appearing in `styles.xml`.
195+
196+
A latent style definition is only required for a built-in style because only
197+
a built-in style can appear in the UI without a style definition in
198+
`styles.xml`.
199+
200+
163201
Style inheritance
164202
-----------------
165203

docs/user/styles-using.rst

Lines changed: 75 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -236,11 +236,11 @@ font and its paragraph indentation.
236236

237237
There are five behavioral properties of a style:
238238

239-
* :attr:`~.Style.hidden`
240-
* :attr:`~.Style.unhide_when_used`
241-
* :attr:`~.Style.priority`
242-
* :attr:`~.Style.quick_style`
243-
* :attr:`~.Style.locked`
239+
* :attr:`~.BaseStyle.hidden`
240+
* :attr:`~.BaseStyle.unhide_when_used`
241+
* :attr:`~.BaseStyle.priority`
242+
* :attr:`~.BaseStyle.quick_style`
243+
* :attr:`~.BaseStyle.locked`
244244

245245
See the :ref:`style_behavior` section in :ref:`understanding_styles` for
246246
a description of how these behavioral properties interact to determine when
@@ -279,6 +279,74 @@ but allow it to remain in the recommended list::
279279
Working with Latent Styles
280280
--------------------------
281281

282-
... describe latent styles in Understanding page ...
282+
See the :ref:`builtin_styles` and :ref:`latent_styles` sections in
283+
:ref:`understanding_styles` for a description of how latent styles define the
284+
behavioral properties of built-in styles that are not yet defined in the
285+
`styles.xml` part of a .docx file.
283286

284-
Let's illustrate these behaviors with a few examples.
287+
Access the latent styles in a document
288+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
289+
290+
The latent styles in a document are accessed from the styles object::
291+
292+
>>> document = Document()
293+
>>> latent_styles = document.styles.latent_styles
294+
295+
A |LatentStyles| object supports :meth:`len`, iteration, and dictionary-style
296+
access by style name::
297+
298+
>>> len(latent_styles)
299+
161
300+
301+
>>> latent_style_names = [ls.name for ls in latent_styles]
302+
>>> latent_style_names
303+
['Normal', 'Heading 1', 'Heading 2', ... 'TOC Heading']
304+
305+
>>> latent_quote = latent_styles['Quote']
306+
>>> latent_quote
307+
<docx.styles.latent.LatentStyle object at 0x10a7c4f50>
308+
>>> latent_quote.priority
309+
29
310+
311+
Change latent style defaults
312+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
313+
314+
The |LatentStyles| object also provides access to the default behavioral
315+
properties for built-in styles in the current document. These defaults
316+
provide the value for any undefined attributes of the |_LatentStyle|
317+
definitions and to all behavioral properties of built-in styles having no
318+
explicit latent style definition. See the API documentation for the
319+
|LatentStyles| object for the complete set of available properties::
320+
321+
>>> latent_styles.default_to_locked
322+
False
323+
>>> latent_styles.default_to_locked = True
324+
>>> latent_styles.default_to_locked
325+
True
326+
327+
Add a latent style definition
328+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
329+
330+
A new latent style can be added using the
331+
:meth:`~.LatentStyles.add_latent_style` method on |LatentStyles|. This code
332+
adds a new latent style for the builtin style 'List Bullet', setting it to
333+
appear in the style gallery::
334+
335+
>>> latent_style = latent_styles['List Bullet']
336+
KeyError: no latent style with name 'List Bullet'
337+
>>> latent_style = latent_styles.add_latent_style('List Bullet')
338+
>>> latent_style.hidden = False
339+
>>> latent_style.priority = 2
340+
>>> latent_style.quick_style = True
341+
342+
Delete a latent style definition
343+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
344+
345+
A latent style definition can be deleted by calling its
346+
:meth:`~.LatentStyle.delete` method::
347+
348+
>>> latent_styles['Light Grid']
349+
<docx.styles.latent.LatentStyle object at 0x10a7c4f50>
350+
>>> latent_styles['Light Grid'].delete()
351+
>>> latent_styles['Light Grid']
352+
KeyError: no latent style with name 'Light Grid'

0 commit comments

Comments
 (0)