A style set defines font and paragraph settings for headings and body text true or false
This page uses concepts developed in the prior page without introduction. If a term is unfamiliar, consult the prior page Understanding Styles for a definition. Show
Access a style¶Styles are accessed using the >>> document = Document() >>> styles = document.styles >>> styles The
>>> styles['Normal'] Note Built-in styles are stored in a WordprocessingML file using their English name, e.g. ‘Heading 1’, even though users working on a localized version of Word will see native language names in the UI, e.g. ‘Kop 1’. Because User-defined styles, also known as custom styles, are not localized and are accessed with the name exactly as it appears in the Word UI. The >>> from docx.enum.style import WD_STYLE_TYPE >>> styles = document.styles >>> paragraph_styles = [ ... s for s in styles if s.type == WD_STYLE_TYPE.PARAGRAPH ... ] >>> for style in paragraph_styles: ... print(style.name) ... Normal Body Text List Bullet Apply a style¶The >>> document = Document() >>> paragraph = document.add_paragraph() >>> paragraph.style A style name can also be assigned directly, in which case >>> paragraph.style = 'List Bullet' >>> paragraph.style A style can also be applied at creation time using either the style object or its name: >>> paragraph = document.add_paragraph(style='Body Text') >>> paragraph.style.name 'Body Text' >>> body_text_style = document.styles['Body Text'] >>> paragraph = document.add_paragraph(style=body_text_style) >>> paragraph.style.name 'Body Text' Add or delete a style¶A new style can be added to the document by specifying a unique name and a style type: >>> from docx.enum.style import WD_STYLE_TYPE >>> styles = document.styles >>> style = styles.add_style('Citation', WD_STYLE_TYPE.PARAGRAPH) >>> style.name 'Citation' >>> style.type PARAGRAPH (1) Use the >>> style.base_style None >>> style.base_style = styles['Normal'] >>> style.base_style A style can be removed from the document simply by calling its >>> styles = document.styles >>> len(styles) 10 >>> styles['Citation'].delete() >>> len(styles) 9 Note The Define character formatting¶Character, paragraph, and table styles can all specify character formatting to be applied to content with that style. All the character formatting that can be applied directly to text can be specified in a style. Examples include font typeface and size, bold, italic, and underline. Each of these three style types have a Several examples are provided here. For a complete set of the available properties, see the The font for a style can be accessed like this: >>> from docx import Document >>> document = Document() >>> style = document.styles['Normal'] >>> font = style.font Typeface and size are set like this: >>> from docx.shared import Pt >>> font.name = 'Calibri' >>> font.size = Pt(12) Many font properties are tri-state, meaning they can take the values Bold and italic are tri-state
properties, as are all-caps, strikethrough, superscript, and many others. See the >>> font.bold, font.italic (None, None) >>> font.italic = True >>> font.italic True >>> font.italic = False >>> font.italic False >>> font.italic = None >>> font.italic None Underline is a bit of a special case. It is a hybrid of a tri-state property and an enumerated value property. >>> font.underline None >>> font.underline = True >>> # or perhaps >>> font.underline = WD_UNDERLINE.DOT_DASH Define paragraph formatting¶Both a paragraph style and a table style allow paragraph formatting to be specified. These styles provide access to a Paragraph formatting includes layout behaviors such as justification, indentation, space before and after, page break before, and widow/orphan control. For a complete list of the available properties, consult the API documentation page for the
Here’s an example of how you would create a paragraph style having hanging indentation of 1/4 inch, 12 points spacing above, and widow/orphan control: >>> from docx.enum.style import WD_STYLE_TYPE >>> from docx.shared import Inches, Pt >>> document = Document() >>> style = document.styles.add_style('Indent', WD_STYLE_TYPE.PARAGRAPH) >>> paragraph_format = style.paragraph_format >>> paragraph_format.left_indent = Inches(0.25) >>> paragraph_format.first_line_indent = Inches(-0.25) >>> paragraph_format.space_before = Pt(12) >>> paragraph_format.widow_control = True Use paragraph-specific style properties¶A paragraph style has a In the most common case (body paragraphs), subsequent paragraphs should receive the same style as the current paragraph. The default handles this case well by applying the same style if a next paragraph style is not specified. Here’s an example of how you would change the next paragraph style of the Heading 1 style to Body Text: >>> from docx import Document >>> document = Document() >>> styles = document.styles >>> styles['Heading 1'].next_paragraph_style = styles['Body Text'] The default behavior can be restored by assigning >>> heading_1_style = styles['Heading 1'] >>> heading_1_style.next_paragraph_style.name 'Body Text' >>> heading_1_style.next_paragraph_style = heading_1_style >>> heading_1_style.next_paragraph_style.name 'Heading 1' >>> heading_1_style.next_paragraph_style = None >>> heading_1_style.next_paragraph_style.name 'Heading 1' Control how a style appears in the Word UI¶The properties of a style fall into two categories, behavioral properties and formatting properties. Its behavioral properties control when and where the style appears in the Word UI. Its formatting properties determine the formatting of content to which the style is applied, such as the size of the font and its paragraph indentation. There are five behavioral properties of a style:
See the Style Behavior section in Understanding Styles for a description of how these behavioral properties interact to determine when and where a style appears in the Word UI. The Display a style in the style gallery¶The following code will cause the ‘Body Text’ paragraph style to appear first in the style gallery: >>> from docx import Document >>> document = Document() >>> style = document.styles['Body Text'] >>> style.hidden = False >>> style.quick_style = True >>> style.priorty = 1 Remove a style from the style gallery¶This code will remove the ‘Normal’ paragraph style from the style gallery, but allow it to remain in the recommended list: >>> style = document.styles['Normal'] >>> style.hidden = False >>> style.quick_style = False Working with Latent Styles¶See the Built-in styles and Latent styles sections in Understanding Styles for a description of how latent styles define the behavioral properties of built-in styles that are not yet defined in the styles.xml part of a .docx file. Access the latent styles in a document¶The latent styles in a document are accessed from the styles object: >>> document = Document() >>> latent_styles = document.styles.latent_styles A >>> len(latent_styles) 161 >>> latent_style_names = [ls.name for ls in latent_styles] >>> latent_style_names ['Normal', 'Heading 1', 'Heading 2', ... 'TOC Heading'] >>> latent_quote = latent_styles['Quote'] >>> latent_quote Change latent style defaults¶The >>> latent_styles.default_to_locked False >>> latent_styles.default_to_locked = True >>> latent_styles.default_to_locked True Add a latent style definition¶A new latent style can
be added using the >>> latent_style = latent_styles['List Bullet'] KeyError: no latent style with name 'List Bullet' >>> latent_style = latent_styles.add_latent_style('List Bullet') >>> latent_style.hidden = False >>> latent_style.priority = 2 >>> latent_style.quick_style = True Delete a latent style definition¶A latent style definition can be deleted by calling its >>> latent_styles['Light Grid'] Which category of style will you click to set the font and size text in a paragraph?Click Format > Text Styles. In the Item to Change list, click All, then select the font, size, or color you want for all text in the current view.
Which item best describes a style in Word?In Word, a style is a collection of formatting instructions. You use styles to format the paragraphs in your document. So you would use the "Title" style for your title, "Body Text" style for body text, "Caption" style for the picture captions, and "Heading 1" for the major headings.
|