"""Style object hierarchy."""from__future__importannotationsfromtypingimportTypefromdocx.enum.styleimportWD_STYLE_TYPEfromdocx.oxml.stylesimportCT_Stylefromdocx.sharedimportElementProxyfromdocx.stylesimportBabelFishfromdocx.text.fontimportFontfromdocx.text.parfmtimportParagraphFormatdefStyleFactory(style_elm:CT_Style)->BaseStyle:"""Return `Style` object of appropriate |BaseStyle| subclass for `style_elm`."""style_cls:Type[BaseStyle]={WD_STYLE_TYPE.PARAGRAPH:ParagraphStyle,WD_STYLE_TYPE.CHARACTER:CharacterStyle,WD_STYLE_TYPE.TABLE:_TableStyle,WD_STYLE_TYPE.LIST:_NumberingStyle,}[style_elm.type]returnstyle_cls(style_elm)
[文档]classBaseStyle(ElementProxy):"""Base class for the various types of style object, paragraph, character, table, and numbering. These properties and methods are inherited by all style objects. """def__init__(self,style_elm:CT_Style):super().__init__(style_elm)self._style_elm=style_elm@propertydefbuiltin(self):"""Read-only. |True| if this style is a built-in style. |False| indicates it is a custom (user-defined) style. Note this value is based on the presence of a `customStyle` attribute in the XML, not on specific knowledge of which styles are built into Word. """returnnotself._element.customStyle
[文档]defdelete(self):"""Remove this style definition from the document. Note that calling this method does not remove or change the style applied to any document content. Content items having the deleted style will be rendered using the default style, as is any content with a style not defined in the document. """self._element.delete()self._element=None
@propertydefhidden(self):"""|True| if display of this style in the style gallery and list of recommended styles is suppressed. |False| otherwise. In order to be shown in the style gallery, this value must be |False| and :attr:`.quick_style` must be |True|. """returnself._element.semiHidden_val@hidden.setterdefhidden(self,value):self._element.semiHidden_val=value@propertydeflocked(self):"""Read/write Boolean. |True| if this style is locked. A locked style does not appear in the styles panel or the style gallery and cannot be applied to document content. This behavior is only active when formatting protection is turned on for the document (via the Developer menu). """returnself._element.locked_val@locked.setterdeflocked(self,value):self._element.locked_val=value@propertydefname(self):"""The UI name of this style."""name=self._element.name_valifnameisNone:returnNonereturnBabelFish.internal2ui(name)@name.setterdefname(self,value):self._element.name_val=value@propertydefpriority(self):"""The integer sort key governing display sequence of this style in the Word UI. |None| indicates no setting is defined, causing Word to use the default value of 0. Style name is used as a secondary sort key to resolve ordering of styles having the same priority value. """returnself._element.uiPriority_val@priority.setterdefpriority(self,value):self._element.uiPriority_val=value@propertydefquick_style(self):"""|True| if this style should be displayed in the style gallery when :attr:`.hidden` is |False|. Read/write Boolean. """returnself._element.qFormat_val@quick_style.setterdefquick_style(self,value):self._element.qFormat_val=value@propertydefstyle_id(self)->str:"""The unique key name (string) for this style. This value is subject to rewriting by Word and should generally not be changed unless you are familiar with the internals involved. """returnself._style_elm.styleId@style_id.setterdefstyle_id(self,value):self._element.styleId=value@propertydeftype(self):"""Member of :ref:`WdStyleType` corresponding to the type of this style, e.g. ``WD_STYLE_TYPE.PARAGRAPH``."""type=self._style_elm.typeiftypeisNone:returnWD_STYLE_TYPE.PARAGRAPHreturntype@propertydefunhide_when_used(self):"""|True| if an application should make this style visible the next time it is applied to content. False otherwise. Note that |docx| does not automatically unhide a style having |True| for this attribute when it is applied to content. """returnself._element.unhideWhenUsed_val@unhide_when_used.setterdefunhide_when_used(self,value):self._element.unhideWhenUsed_val=value
[文档]classCharacterStyle(BaseStyle):"""A character style. A character style is applied to a |Run| object and primarily provides character- level formatting via the |Font| object in its :attr:`.font` property. """@propertydefbase_style(self):"""Style object this style inherits from or |None| if this style is not based on another style."""base_style=self._element.base_styleifbase_styleisNone:returnNonereturnStyleFactory(base_style)@base_style.setterdefbase_style(self,style):style_id=style.style_idifstyleisnotNoneelseNoneself._element.basedOn_val=style_id@propertydeffont(self):"""The |Font| object providing access to the character formatting properties for this style, such as font name and size."""returnFont(self._element)
# -- just in case someone uses the old name in an extension function --_CharacterStyle=CharacterStyle
[文档]classParagraphStyle(CharacterStyle):"""A paragraph style. A paragraph style provides both character formatting and paragraph formatting such as indentation and line-spacing. """def__repr__(self):return"_ParagraphStyle('%s') id: %s"%(self.name,id(self))@propertydefnext_paragraph_style(self):"""|_ParagraphStyle| object representing the style to be applied automatically to a new paragraph inserted after a paragraph of this style. Returns self if no next paragraph style is defined. Assigning |None| or `self` removes the setting such that new paragraphs are created using this same style. """next_style_elm=self._element.next_styleifnext_style_elmisNone:returnselfifnext_style_elm.type!=WD_STYLE_TYPE.PARAGRAPH:returnselfreturnStyleFactory(next_style_elm)@next_paragraph_style.setterdefnext_paragraph_style(self,style):ifstyleisNoneorstyle.style_id==self.style_id:self._element._remove_next()else:self._element.get_or_add_next().val=style.style_id@propertydefparagraph_format(self):"""The |ParagraphFormat| object providing access to the paragraph formatting properties for this style such as indentation."""returnParagraphFormat(self._element)
# -- just in case someone uses the old name in an extension function --_ParagraphStyle=ParagraphStyle
[文档]class_TableStyle(ParagraphStyle):"""A table style. A table style provides character and paragraph formatting for its contents as well as special table formatting properties. """def__repr__(self):return"_TableStyle('%s') id: %s"%(self.name,id(self))
[文档]class_NumberingStyle(BaseStyle):"""A numbering style. Not yet implemented. """