diff options
| author | Michael Williamson <mike@zwobble.org> | 2021-01-09 13:52:02 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2021-01-09 13:52:02 +0000 |
| commit | 801b505840b698858c48761c640d52a5fa5ec3fb (patch) | |
| tree | 00285ea140888c94bde0c17d1cc061b521ca29f0 /mammoth/docx/numbering_xml.py | |
| parent | d1910bc1e649deb9f0f69114b61023458f3d8d2f (diff) | |
Support numbering defined by paragraph style
Diffstat (limited to 'mammoth/docx/numbering_xml.py')
| -rw-r--r-- | mammoth/docx/numbering_xml.py | 34 |
1 files changed, 32 insertions, 2 deletions
diff --git a/mammoth/docx/numbering_xml.py b/mammoth/docx/numbering_xml.py index af3d1d5..e1ea0b4 100644 --- a/mammoth/docx/numbering_xml.py +++ b/mammoth/docx/numbering_xml.py @@ -28,6 +28,13 @@ class _AbstractNum(object): num_style_link = cobble.field() +@cobble.data +class _AbstractNumLevel(object): + level_index = cobble.field() + is_ordered = cobble.field() + paragraph_style_id = cobble.field() + + def _read_abstract_num_levels(element): levels = map(_read_abstract_num_level, element.find_children("w:lvl")) return dict( @@ -40,7 +47,12 @@ def _read_abstract_num_level(element): level_index = element.attributes["w:ilvl"] num_fmt = element.find_child_or_null("w:numFmt").attributes.get("w:val") is_ordered = num_fmt != "bullet" - return numbering_level(level_index, is_ordered) + paragraph_style_id = element.find_child_or_null("w:pStyle").attributes.get("w:val") + return _AbstractNumLevel( + level_index=level_index, + is_ordered=is_ordered, + paragraph_style_id=paragraph_style_id, + ) def _read_nums(element): @@ -65,6 +77,12 @@ class _Num(object): class Numbering(object): def __init__(self, abstract_nums, nums, styles): self._abstract_nums = abstract_nums + self._levels_by_paragraph_style_id = dict( + (level.paragraph_style_id, self._to_numbering_level(level)) + for abstract_num in abstract_nums.values() + for level in abstract_num.levels.values() + if level.paragraph_style_id is not None + ) self._nums = nums self._styles = styles @@ -75,10 +93,22 @@ class Numbering(object): else: abstract_num = self._abstract_nums[num.abstract_num_id] if abstract_num.num_style_link is None: - return abstract_num.levels.get(level) + return self._to_numbering_level(abstract_num.levels.get(level)) else: style = self._styles.find_numbering_style_by_id(abstract_num.num_style_link) return self.find_level(style.num_id, level) + def find_level_by_paragraph_style_id(self, style_id): + return self._levels_by_paragraph_style_id.get(style_id) + + def _to_numbering_level(self, abstract_num_level): + if abstract_num_level is None: + return None + else: + return numbering_level( + level_index=abstract_num_level.level_index, + is_ordered=abstract_num_level.is_ordered, + ) + Numbering.EMPTY = Numbering(abstract_nums={}, nums={}, styles=Styles.EMPTY) |
