Skip to content

Commit b50dc0e

Browse files
author
Steve Canny
committed
docs: document Font feature analysis
1 parent b595e44 commit b50dc0e

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

docs/user/styles-using.rst

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,74 @@ A style can be removed from the document simply by calling its
131131
'Normal' in the case of a paragraph.
132132

133133

134+
Define character formatting
135+
---------------------------
136+
137+
Character, paragraph, and table styles can all specify character formatting
138+
to be applied to content with that style. All the character formatting that
139+
can be applied directly to text can be specified in a style. Examples include
140+
font typeface and size, bold, italic, and underline.
141+
142+
Each of these three style types have a :attr:`~._CharacterStyle.font`
143+
attribute providing access to a |Font| object. A style's |Font| object
144+
provides properties for getting and setting the character formatting for that
145+
style.
146+
147+
Several examples are provided here. For a complete set of the available
148+
properties, see the |Font| API documentation.
149+
150+
The font for a style can be accessed like this::
151+
152+
>>> from docx import Document
153+
>>> document = Document()
154+
>>> style = document.styles['Normal']
155+
>>> font = style.font
156+
157+
Typeface and size are set like this::
158+
159+
>>> from docx.shared import Pt
160+
>>> font.name = 'Calibri'
161+
>>> font.size = Pt(12)
162+
163+
Many font properties are *tri-state*, meaning they can take the values
164+
|True|, |False|, and |None|. |True| means the property is "on", |False| means
165+
it is "off". Conceptually, the |None| value means "inherit". Because a style
166+
exists in an inheritance hierarchy, it is important to have the ability to
167+
specify a property at the right place in the hierarchy, generally as far up
168+
the hierarchy as possible. For example, if all headings should be in the
169+
Arial typeface, it makes more sense to set that property on the `Heading 1`
170+
style and have `Heading 2` inherit from `Heading 1`.
171+
172+
Bold and italic are tri-state properties, as are all-caps, strikethrough,
173+
superscript, and many others. See the |Font| API documentation for a full
174+
list::
175+
176+
>>> font.bold, font.italic
177+
(None, None)
178+
>>> font.italic = True
179+
>>> font.italic
180+
True
181+
>>> font.italic = False
182+
>>> font.italic
183+
False
184+
>>> font.italic = None
185+
>>> font.italic
186+
None
187+
188+
Underline is a bit of a special case. It is a hybrid of a tri-state property
189+
and an enumerated value property. |True| means single underline, by far the
190+
most common. |False| means no underline, but more often |None| is the right
191+
choice if no underlining is wanted since it is rare to inherit it from a base
192+
style. The other forms of underlining, such as double or dashed, are
193+
specified with a member of the :ref:`WdUnderline` enumeration::
194+
195+
>>> font.underline
196+
None
197+
>>> font.underline = True
198+
>>> # or perhaps
199+
>>> font.underline = WD_UNDERLINE.DOT_DASH
200+
201+
134202
Control how a style appears in the Word UI
135203
------------------------------------------
136204

0 commit comments

Comments
 (0)