"""Styles object, container for all objects in the styles part."""from__future__importannotationsfromwarningsimportwarnfromdocx.enum.styleimportWD_STYLE_TYPEfromdocx.oxml.stylesimportCT_Stylesfromdocx.sharedimportElementProxyfromdocx.stylesimportBabelFishfromdocx.styles.latentimportLatentStylesfromdocx.styles.styleimportBaseStyle,StyleFactory
[文档]classStyles(ElementProxy):"""Provides access to the styles defined in a document. Accessed using the :attr:`.Document.styles` property. Supports ``len()``, iteration, and dictionary-style access by style name. """def__init__(self,styles:CT_Styles):super().__init__(styles)self._element=stylesdef__contains__(self,name):"""Enables `in` operator on style name."""internal_name=BabelFish.ui2internal(name)returnany(style.name_val==internal_nameforstyleinself._element.style_lst)def__getitem__(self,key:str):"""Enables dictionary-style access by UI name. Lookup by style id is deprecated, triggers a warning, and will be removed in a near-future release. """style_elm=self._element.get_by_name(BabelFish.ui2internal(key))ifstyle_elmisnotNone:returnStyleFactory(style_elm)style_elm=self._element.get_by_id(key)ifstyle_elmisnotNone:msg=("style lookup by style_id is deprecated. Use style name as ""key instead.")warn(msg,UserWarning,stacklevel=2)returnStyleFactory(style_elm)raiseKeyError("no style with name '%s'"%key)def__iter__(self):return(StyleFactory(style)forstyleinself._element.style_lst)def__len__(self):returnlen(self._element.style_lst)
[文档]defadd_style(self,name,style_type,builtin=False):"""Return a newly added style object of `style_type` and identified by `name`. A builtin style can be defined by passing True for the optional `builtin` argument. """style_name=BabelFish.ui2internal(name)ifstyle_nameinself:raiseValueError("document already contains style '%s'"%name)style=self._element.add_style_of_type(style_name,style_type,builtin)returnStyleFactory(style)
[文档]defdefault(self,style_type:WD_STYLE_TYPE):"""Return the default style for `style_type` or |None| if no default is defined for that type (not common)."""style=self._element.default_for(style_type)ifstyleisNone:returnNonereturnStyleFactory(style)
defget_by_id(self,style_id:str|None,style_type:WD_STYLE_TYPE):"""Return the style of `style_type` matching `style_id`. Returns the default for `style_type` if `style_id` is not found or is |None|, or if the style having `style_id` is not of `style_type`. """ifstyle_idisNone:returnself.default(style_type)returnself._get_by_id(style_id,style_type)defget_style_id(self,style_or_name,style_type):"""Return the id of the style corresponding to `style_or_name`, or |None| if `style_or_name` is |None|. If `style_or_name` is not a style object, the style is looked up using `style_or_name` as a style name, raising |ValueError| if no style with that name is defined. Raises |ValueError| if the target style is not of `style_type`. """ifstyle_or_nameisNone:returnNoneelifisinstance(style_or_name,BaseStyle):returnself._get_style_id_from_style(style_or_name,style_type)else:returnself._get_style_id_from_name(style_or_name,style_type)@propertydeflatent_styles(self):"""A |LatentStyles| object providing access to the default behaviors for latent styles and the collection of |_LatentStyle| objects that define overrides of those defaults for a particular named latent style."""returnLatentStyles(self._element.get_or_add_latentStyles())def_get_by_id(self,style_id:str|None,style_type:WD_STYLE_TYPE):"""Return the style of `style_type` matching `style_id`. Returns the default for `style_type` if `style_id` is not found or if the style having `style_id` is not of `style_type`. """style=self._element.get_by_id(style_id)ifstyle_idelseNoneifstyleisNoneorstyle.type!=style_type:returnself.default(style_type)returnStyleFactory(style)def_get_style_id_from_name(self,style_name:str,style_type:WD_STYLE_TYPE)->str|None:"""Return the id of the style of `style_type` corresponding to `style_name`. Returns |None| if that style is the default style for `style_type`. Raises |ValueError| if the named style is not found in the document or does not match `style_type`. """returnself._get_style_id_from_style(self[style_name],style_type)def_get_style_id_from_style(self,style:BaseStyle,style_type:WD_STYLE_TYPE)->str|None:"""Id of `style`, or |None| if it is the default style of `style_type`. Raises |ValueError| if style is not of `style_type`. """ifstyle.type!=style_type:raiseValueError("assigned style is type %s, need type %s"%(style.type,style_type))ifstyle==self.default(style_type):returnNonereturnstyle.style_id