MCPcopy Index your code
hub / github.com/python-openxml/python-docx / CT_Styles

Class CT_Styles

src/docx/oxml/styles.py:272–320  ·  view source on GitHub ↗

`` `` element, the root element of a styles part, i.e. styles.xml.

Source from the content-addressed store, hash-verified

270
271
272class CT_Styles(BaseOxmlElement):
273 """``<w:styles>`` element, the root element of a styles part, i.e. styles.xml."""
274
275 _tag_seq = ("w:docDefaults", "w:latentStyles", "w:style")
276 latentStyles = ZeroOrOne("w:latentStyles", successors=_tag_seq[2:])
277 style = ZeroOrMore("w:style", successors=())
278 del _tag_seq
279
280 def add_style_of_type(self, name, style_type, builtin):
281 """Return a newly added `w:style` element having `name` and `style_type`.
282
283 `w:style/@customStyle` is set based on the value of `builtin`.
284 """
285 style = self.add_style()
286 style.type = style_type
287 style.customStyle = None if builtin else True
288 style.styleId = styleId_from_name(name)
289 style.name_val = name
290 return style
291
292 def default_for(self, style_type):
293 """Return `w:style[@w:type="*{style_type}*][-1]` or |None| if not found."""
294 default_styles_for_type = [
295 s for s in self._iter_styles() if s.type == style_type and s.default
296 ]
297 if not default_styles_for_type:
298 return None
299 # spec calls for last default in document order
300 return default_styles_for_type[-1]
301
302 def get_by_id(self, styleId: str) -> CT_Style | None:
303 """`w:style` child where @styleId = `styleId`.
304
305 |None| if not found.
306 """
307 xpath = f'w:style[@w:styleId="{styleId}"]'
308 return next(iter(self.xpath(xpath)), None)
309
310 def get_by_name(self, name: str) -> CT_Style | None:
311 """`w:style` child with `w:name` grandchild having value `name`.
312
313 |None| if not found.
314 """
315 xpath = 'w:style[w:name/@w:val="%s"]' % name
316 return next(iter(self.xpath(xpath)), None)
317
318 def _iter_styles(self):
319 """Generate each of the `w:style` child elements in document order."""
320 return (style for style in self.xpath("w:style"))

Callers

nothing calls this directly

Calls 2

ZeroOrOneClass · 0.90
ZeroOrMoreClass · 0.90

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…