@@ -228,3 +228,115 @@ from the style hierarchy. |True| means "on" and |False| means "off"::
228228 >>> paragraph_format.page_break_before = False
229229 >>> paragraph_format.page_break_before
230230 False
231+
232+
233+ Apply character formatting
234+ --------------------------
235+
236+ Character formatting is applied at the Run level. Examples include font
237+ typeface and size, bold, italic, and underline.
238+
239+ A |Run | object has a read-only :attr: `~.Run.font ` property providing access
240+ to a |Font | object. A run's |Font | object provides properties for getting
241+ and setting the character formatting for that run.
242+
243+ Several examples are provided here. For a complete set of the available
244+ properties, see the |Font | API documentation.
245+
246+ The font for a run can be accessed like this::
247+
248+ >>> from docx import Document
249+ >>> document = Document()
250+ >>> run = document.add_paragraph().add_run()
251+ >>> font = run.font
252+
253+ Typeface and size are set like this::
254+
255+ >>> from docx.shared import Pt
256+ >>> font.name = 'Calibri'
257+ >>> font.size = Pt(12)
258+
259+ Many font properties are *tri-state *, meaning they can take the values
260+ |True |, |False |, and |None |. |True | means the property is "on", |False | means
261+ it is "off". Conceptually, the |None | value means "inherit". A run exists in
262+ the style inheritance hierarchy and by default inherits its character
263+ formatting from that hierarchy. Any character formatting directly applied
264+ using the |Font | object overrides the inherited values.
265+
266+ Bold and italic are tri-state properties, as are all-caps, strikethrough,
267+ superscript, and many others. See the |Font | API documentation for a full
268+ list::
269+
270+ >>> font.bold, font.italic
271+ (None, None)
272+ >>> font.italic = True
273+ >>> font.italic
274+ True
275+ >>> font.italic = False
276+ >>> font.italic
277+ False
278+ >>> font.italic = None
279+ >>> font.italic
280+ None
281+
282+ Underline is a bit of a special case. It is a hybrid of a tri-state property
283+ and an enumerated value property. |True | means single underline, by far the
284+ most common. |False | means no underline, but more often |None | is the right
285+ choice if no underlining is wanted. The other forms of underlining, such as
286+ double or dashed, are specified with a member of the :ref: `WdUnderline `
287+ enumeration::
288+
289+ >>> font.underline
290+ None
291+ >>> font.underline = True
292+ >>> # or perhaps
293+ >>> font.underline = WD_UNDERLINE.DOT_DASH
294+
295+ Font color
296+ ~~~~~~~~~~
297+
298+ Each |Font | object has a |ColorFormat | object that provides access to its
299+ color, accessed via its read-only :attr: `~.Font.color ` property.
300+
301+ Apply a specific RGB color to a font::
302+
303+ >>> from docx.shared import RGBColor
304+ >>> font.color.rgb = RGBColor(0x42, 0x24, 0xE9)
305+
306+ A font can also be set to a theme color by assigning a member of the
307+ :ref: `MsoThemeColorIndex ` enumeration::
308+
309+ >>> from docx.enum.dml import MSO_THEME_COLOR
310+ >>> font.color.theme_color = MSO_THEME_COLOR.ACCENT_1
311+
312+ A font's color can be restored to its default (inherited) value by assigning
313+ |None | to either the :attr: `~.ColorFormat.rgb ` or
314+ :attr: `~.ColorFormat.theme_color ` attribute of |ColorFormat |::
315+
316+ >>> font.color.rgb = None
317+
318+ Determining the color of a font begins with determining its color type::
319+
320+ >>> font.color.type
321+ RGB (1)
322+
323+ The value of the :attr: `~.ColorFormat.type ` property can be a member of the
324+ :ref: `MsoColorType ` enumeration or None. `MSO_COLOR_TYPE.RGB ` indicates it is
325+ an RGB color. `MSO_COLOR_TYPE.THEME ` indicates a theme color.
326+ `MSO_COLOR_TYPE.AUTO ` indicates its value is determined automatically by the
327+ application, usually set to black. (This value is relatively rare.) |None |
328+ indicates no color is applied and the color is inherited from the style
329+ hierarchy; this is the most common case.
330+
331+ When the color type is `MSO_COLOR_TYPE.RGB `, the :attr: `~.ColorFormat.rgb `
332+ property will be an |RGBColor | value indicating the RGB color::
333+
334+ >>> font.color.rgb
335+ RGBColor(0x42, 0x24, 0xe9)
336+
337+ When the color type is `MSO_COLOR_TYPE.THEME `, the
338+ :attr: `~.ColorFormat.theme_color ` property will be a member of
339+ :ref: `MsoThemeColorIndex ` indicating the theme color::
340+
341+ >>> font.color.theme_color
342+ ACCENT_1 (5)
0 commit comments