From 90bf4ad7148e924bb5a720fd2cfca06219c91891 Mon Sep 17 00:00:00 2001 From: Arthur Souza Rodrigues Date: Thu, 8 Jan 2026 22:28:49 -0500 Subject: [PATCH] fix: restore parent ol numId after nested list closes When an ordered list contains a nested list, items after the nested list were restarting numbering at "1." instead of continuing from the parent list's count. The fix uses a stack (_ol_id_stack) to preserve parent list context: - On
    open: push current_ol_num_id to stack before creating new one - On
      close: pop from stack to restore parent's numId Example fix:
      1. First
      2. Second
        1. Second.A
        2. Second.B
      3. Third
      --- html4docx/h4d.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/html4docx/h4d.py b/html4docx/h4d.py index 5184e16..0f03708 100644 --- a/html4docx/h4d.py +++ b/html4docx/h4d.py @@ -52,6 +52,7 @@ def set_initial_attrs(self, document=None): self.list_restart_counter = 0 self.current_ol_num_id = None self._list_num_ids = {} + self._ol_id_stack = [] # Stack to track parent ol IDs for nested lists # NEW: Set style map & tag overrides according to options @@ -1448,7 +1449,9 @@ def handle_starttag(self, tag, attrs): return elif tag in ['ol', 'ul']: if tag == 'ol': - # Assign new ID if it's a fresh top-level list + # Push current ID to stack before creating new one (for nested lists) + self._ol_id_stack.append(self.current_ol_num_id) + # Assign new ID for this
        self.list_restart_counter += 1 self.current_ol_num_id = self.list_restart_counter else: @@ -1609,7 +1612,11 @@ def handle_endtag(self, tag): utils.remove_last_occurence(self.tags['list'], tag) if tag == 'ol': self._list_num_ids.pop(self.current_ol_num_id, None) - self.current_ol_num_id = None + # Restore parent's ol ID from stack (for nested lists) + if self._ol_id_stack: + self.current_ol_num_id = self._ol_id_stack.pop() + else: + self.current_ol_num_id = None return elif tag == 'table': if self.include_tables: