From bb0ff1066ea8f890f98092244d1b489ceab7c80e Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 3 Aug 2025 07:58:40 +0100 Subject: Unify flow for storing styles --- mammoth/docx/styles_xml.py | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) (limited to 'mammoth') diff --git a/mammoth/docx/styles_xml.py b/mammoth/docx/styles_xml.py index 937f852..0b3f39d 100644 --- a/mammoth/docx/styles_xml.py +++ b/mammoth/docx/styles_xml.py @@ -56,17 +56,19 @@ def read_styles_xml_element(element): "paragraph": paragraph_styles, "character": character_styles, "table": table_styles, + "numbering": numbering_styles, } for style_element in element.find_children("w:style"): - style = _read_style_element(style_element) element_type = style_element.attributes["w:type"] if element_type == "numbering": - numbering_styles[style.style_id] = _read_numbering_style_element(style_element) + style = _read_numbering_style_element(style_element) else: - style_set = styles.get(element_type) - if style_set is not None: - style_set[style.style_id] = style + style = _read_style_element(style_element) + + style_set = styles.get(element_type) + if style_set is not None: + style_set[style.style_id] = style return Styles( paragraph_styles=paragraph_styles, @@ -80,19 +82,25 @@ Style = collections.namedtuple("Style", ["style_id", "name"]) def _read_style_element(element): - style_id = element.attributes["w:styleId"] + style_id = _read_style_id(element) name = element.find_child_or_null("w:name").attributes.get("w:val") return Style(style_id=style_id, name=name) -NumberingStyle = collections.namedtuple("NumberingStyle", ["num_id"]) +NumberingStyle = collections.namedtuple("NumberingStyle", ["style_id", "num_id"]) def _read_numbering_style_element(element): + style_id = _read_style_id(element) + num_id = element \ .find_child_or_null("w:pPr") \ .find_child_or_null("w:numPr") \ .find_child_or_null("w:numId") \ .attributes.get("w:val") - return NumberingStyle(num_id=num_id) + return NumberingStyle(style_id=style_id, num_id=num_id) + + +def _read_style_id(element): + return element.attributes["w:styleId"] -- cgit v1.2.3