From d61fb7f77ae7521c0c3e8cffa3b45e4f18a41b3a Mon Sep 17 00:00:00 2001 From: Alex Marchant Date: Thu, 12 Sep 2024 17:28:16 -0400 Subject: [PATCH] Wait to remove elements until they are all processed --- libmat2/office.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/libmat2/office.py b/libmat2/office.py index f796932..d067128 100644 --- a/libmat2/office.py +++ b/libmat2/office.py @@ -283,9 +283,15 @@ class MSOfficeParser(ZipParser): for children in element.iterfind('./*'): elements_ins.append((element, position, children)) break + for (element, position, children) in elements_ins: parent_map[element].insert(position, children) - parent_map[element].remove(element) + + # the list can sometimes contain duplicate elements, so don't remove + # until all children have been processed + for (element, position, children) in elements_ins: + if element in parent_map[element]: + parent_map[element].remove(element) tree.write(full_path, xml_declaration=True, encoding='utf-8') return True