Loading generateBaseline/postprocessing.py +164 −48 Viewed Changes for generateBaseline/postprocessing.py: 164 added lines, 48 removed lines. Original line number Diff line number Diff line Loading @@ -1052,20 +1052,20 @@ def update_lists(docx_input, docx_output): - Tuple (bool, str): (True, num_fmt) if numbered list, (False, num_fmt) if unnumbered, (None, None) if cannot determine """ if numbering_root is None: return (None, None) # Cannot determine without numbering.xml return (None, None, None, None, None) # Cannot determine without numbering.xml # Get numId and ilvl from paragraph numId_elem = para.xpath('./w:pPr/w:numPr/w:numId', namespaces=ns) ilvl_elem = para.xpath('./w:pPr/w:numPr/w:ilvl', namespaces=ns) if not numId_elem or not ilvl_elem: return (None, None) return (None, None, None, None, None) numId = numId_elem[0].get(f"{{{ns['w']}}}val") ilvl = ilvl_elem[0].get(f"{{{ns['w']}}}val") if numId is None or ilvl is None: return (None, None) return (None, None, None, None, None) try: # Find the num element with this numId Loading @@ -1074,16 +1074,16 @@ def update_lists(docx_input, docx_output): namespaces=ns ) if not num_elem: return (None, None, None) return (None, None, None, None, None) # Get the abstractNumId abstract_num_id_elem = num_elem[0].xpath('./w:abstractNumId', namespaces=ns) if not abstract_num_id_elem: return (None, None, None) return (None, None, None, None, None) abstract_num_id = abstract_num_id_elem[0].get(f"{{{ns['w']}}}val") if abstract_num_id is None: return (None, None, None) return (None, None, None, None, None) # Find the abstractNum abstract_num = numbering_root.xpath( Loading @@ -1091,7 +1091,7 @@ def update_lists(docx_input, docx_output): namespaces=ns ) if not abstract_num: return (None, None, None, None) return (None, None, None, None, None) # Check the format for this level numFmt_elem = abstract_num[0].xpath( Loading @@ -1099,7 +1099,7 @@ def update_lists(docx_input, docx_output): namespaces=ns ) if not numFmt_elem: return (None, None, None, None) return (None, None, None, None, None) numFmt = numFmt_elem[0].get(f"{{{ns['w']}}}val") Loading Loading @@ -1133,16 +1133,16 @@ def update_lists(docx_input, docx_output): 'check', 'arrow', 'arrowhead', 'rtArrow', 'hyphen'] if numFmt in numbered_formats: return (True, numFmt, lvl_text, abstract_num_id) # Return (True, format string) return (True, numFmt, lvl_text, abstract_num_id, numId) elif numFmt in unnumbered_formats: return (False, numFmt, lvl_text, abstract_num_id) # Return (False, format string) return (False, numFmt, lvl_text, abstract_num_id, numId) else: # Unknown format, default to unnumbered return (False, numFmt, lvl_text, abstract_num_id) return (False, numFmt, lvl_text, abstract_num_id, numId) except Exception as e: # If any error occurs, return None to fall back to heuristic return (None, None, None, None) return (None, None, None, None, None) counter_regular = 0 counter_b1 = 0 counter_b2 = 0 Loading @@ -1153,6 +1153,9 @@ def update_lists(docx_input, docx_output): counter_table = 0 counter_numbered = 0 numbering_modified = False styles_modified = False # Track processed paragraphs to avoid reprocessing processed_paras = set() Loading Loading @@ -1192,36 +1195,107 @@ def update_lists(docx_input, docx_output): text += text_elem.text return text style_abstract_id_cache = {} def get_style_abstract_id(styles_root, numbering_root, style_id): """ Find the abstractNumId used by the given style. Find the abstractNumId used by the given list paragraph style. """ nonlocal styles_modified if style_id in style_abstract_id_cache: return style_abstract_id_cache[style_id] styles = styles_root style_elements = styles.xpath(f'//w:style[@w:styleId="{style_id}"]', namespaces=ns) if not style_elements: print(f'Style {style_id} not found') style_abstract_id_cache[style_id] = None return None style = style_elements[0] num_id_el = style.xpath('.//w:numId', namespaces=ns) if not num_id_el: style_abstract_id_cache[style_id] = None return None style_num_id = num_id_el[0].get(qn('w:val')) if style_num_id is None or numbering_root is None: style_abstract_id_cache[style_id] = None return None num_elem = numbering_root.xpath( f'.//w:num[@w:numId="{style_num_id}"]', namespaces=ns, ) if not num_elem: style_abstract_id_cache[style_id] = None return None #raise ValueError(f"Style {style_id} has no numId") # Remove numPr from style if it exists abstract_num_id_elem = num_elem[0].xpath('./w:abstractNumId', namespaces=ns) if not abstract_num_id_elem: style_abstract_id_cache[style_id] = None return None abstract_id = abstract_num_id_elem[0].get(qn('w:val')) style_abstract_id_cache[style_id] = abstract_id # Remove numPr from style once; paragraph-level numPr drives numbering. num_pr = style.xpath('.//w:numPr', namespaces=ns) if num_pr: for el in num_pr: el.getparent().remove(el) styles_modified = True return num_id_el[0].get(qn('w:val')) return abstract_id def update_list_instance_to_style(numbering_root, abstract_num_id, abstract_id_bn): def update_list_instance_to_style(numbering_root, num_id, target_abstract_num_id): nonlocal numbering_modified if num_id is None or target_abstract_num_id is None: return for num in numbering_root.xpath(f'//w:num[@w:abstractNumId="{abstract_num_id}"]', namespaces=ns): for num in numbering_root.xpath(f'//w:num[@w:numId="{num_id}"]', namespaces=ns): abstract = num.xpath('./w:abstractNumId', namespaces=ns)[0] old_id = abstract.get(qn('w:val')) abstract.set(qn('w:val'), abstract_id_bn) if abstract.get(qn('w:val')) != target_abstract_num_id: abstract.set(qn('w:val'), target_abstract_num_id) numbering_modified = True def get_abstract_id_for_num(numbering_root, num_id): if numbering_root is None or num_id is None: return None nums = numbering_root.xpath(f'//w:num[@w:numId="{num_id}"]', namespaces=ns) if not nums: return None abstract_id_elem = nums[0].xpath('./w:abstractNumId', namespaces=ns) return abstract_id_elem[0].get(qn('w:val')) if abstract_id_elem else None def apply_list_level_indent(pPr, numbering_root, num_id, ilvl, fallback_abstract_id=None): """Set paragraph indent from the active numbering level definition.""" if numbering_root is None: return abstract_id = get_abstract_id_for_num(numbering_root, num_id) or fallback_abstract_id if abstract_id is None: return lvl_ppr_elems = numbering_root.xpath( f'.//w:abstractNum[@w:abstractNumId="{abstract_id}"]/w:lvl[@w:ilvl="{ilvl}"]/w:pPr', namespaces=ns, ) if not lvl_ppr_elems and fallback_abstract_id and fallback_abstract_id != abstract_id: lvl_ppr_elems = numbering_root.xpath( f'.//w:abstractNum[@w:abstractNumId="{fallback_abstract_id}"]/w:lvl[@w:ilvl="{ilvl}"]/w:pPr', namespaces=ns, ) if not lvl_ppr_elems: return lvl_ppr = lvl_ppr_elems[0] for tag in ('ind', 'tabs'): for el in pPr.xpath(f'./w:{tag}', namespaces=ns): pPr.remove(el) src = lvl_ppr.find(f'{{{ns["w"]}}}{tag}') if src is not None: pPr.append(etree.fromstring(etree.tostring(src))) for para in paragraphs: para_removed = False Loading @@ -1240,20 +1314,33 @@ def update_lists(docx_input, docx_output): else: compact_style = OxmlElement('w:pStyle') # Check if it is a numbered list and get the format is_numbered, num_format, lvl_text, abstract_num_id = is_numbered_list(para, numbering_root) is_numbered, num_format, lvl_text, abstract_num_id, num_id = is_numbered_list(para, numbering_root) ilvl_elem = para.xpath('./w:pPr/w:numPr/w:ilvl', namespaces=ns) ilvl = ilvl_elem[0].get(f"{{{ns['w']}}}val") if ilvl_elem else "0" if is_numbered: # is_numbered is True if numbered, False if unnumbered, None if cannot determine #pStyle = OxmlElement('w:pStyle') # If format is decimal (numbers), use BN; otherwise use BL # Style each level by its numbering format; set abstractNum only at top level # so nested letter items keep BN multilevel numbering but get BL paragraph style. if num_format == 'decimal': if ilvl == "0": abstract_id_bn = get_style_abstract_id(styles_root, numbering_root, "BN") if abstract_id_bn is not None and abstract_num_id is not None: update_list_instance_to_style(numbering_root, abstract_num_id, abstract_id_bn) if abstract_id_bn is not None: update_list_instance_to_style(numbering_root, num_id, abstract_id_bn) compact_style.set(f"{{{ns['w']}}}val", "BN") else: if ilvl == "0": abstract_id_bl = get_style_abstract_id(styles_root, numbering_root, "BL") if abstract_id_bl is not None and abstract_num_id is not None: update_list_instance_to_style(numbering_root, abstract_num_id, abstract_id_bl) if abstract_id_bl is not None: update_list_instance_to_style(numbering_root, num_id, abstract_id_bl) compact_style.set(f"{{{ns['w']}}}val", "BL") apply_list_level_indent( pPr, numbering_root, num_id, ilvl, get_style_abstract_id(styles_root, numbering_root, "BN") if num_format == 'decimal' else get_style_abstract_id(styles_root, numbering_root, "BL"), ) pPr.insert(0, compact_style) # Remove numId from numPr #numPr = pPr.xpath('./w:numPr', namespaces=ns)[0] Loading @@ -1265,16 +1352,16 @@ def update_lists(docx_input, docx_output): if para.xpath('./w:pPr/w:numPr/w:ilvl[@w:val="0"]', namespaces=ns): if para.xpath('ancestor::w:tbl', namespaces=ns): abstract_id_tb1 = get_style_abstract_id(styles_root, numbering_root, "TB1") if abstract_id_tb1 is not None and abstract_num_id is not None: update_list_instance_to_style(numbering_root, abstract_num_id, abstract_id_tb1) if abstract_id_tb1 is not None: update_list_instance_to_style(numbering_root, num_id, abstract_id_tb1) compact_style.set(f"{{{ns['w']}}}val", "TB1") counter_table += 1 else: if not lvl_text == " ": abstract_id_b1 = get_style_abstract_id(styles_root, numbering_root, "B1") if abstract_id_b1 is not None and abstract_num_id is not None: update_list_instance_to_style(numbering_root, abstract_num_id, abstract_id_b1) if abstract_id_b1 is not None: update_list_instance_to_style(numbering_root, num_id, abstract_id_b1) compact_style.set(f"{{{ns['w']}}}val", "B1") else: Loading Loading @@ -1304,16 +1391,16 @@ def update_lists(docx_input, docx_output): elif para.xpath('./w:pPr/w:numPr/w:ilvl[@w:val="1"]', namespaces=ns): if para.xpath('ancestor::w:tbl', namespaces=ns): abstract_id_tb2 = get_style_abstract_id(styles_root, numbering_root, "TB2") if abstract_id_tb2 is not None and abstract_num_id is not None: update_list_instance_to_style(numbering_root, abstract_num_id, abstract_id_tb2) if abstract_id_tb2 is not None: update_list_instance_to_style(numbering_root, num_id, abstract_id_tb2) compact_style.set(f"{{{ns['w']}}}val", "TB2") counter_table += 1 else: if not lvl_text == " ": abstract_id_b2 = get_style_abstract_id(styles_root, numbering_root, "B2") if abstract_id_b2 is not None and abstract_num_id is not None: update_list_instance_to_style(numbering_root, abstract_num_id, abstract_id_b2) if abstract_id_b2 is not None: update_list_instance_to_style(numbering_root, num_id, abstract_id_b2) compact_style.set(f"{{{ns['w']}}}val", "B2") else: Loading Loading @@ -1343,16 +1430,16 @@ def update_lists(docx_input, docx_output): elif para.xpath('./w:pPr/w:numPr/w:ilvl[@w:val="2"]', namespaces=ns): if para.xpath('ancestor::w:tbl', namespaces=ns): abstract_id_tb3 = get_style_abstract_id(styles_root, numbering_root, "TB3") if abstract_id_tb3 is not None and abstract_num_id is not None: update_list_instance_to_style(numbering_root, abstract_num_id, abstract_id_tb3) if abstract_id_tb3 is not None: update_list_instance_to_style(numbering_root, num_id, abstract_id_tb3) compact_style.set(f"{{{ns['w']}}}val", "TB3") counter_table += 1 else: if not lvl_text == " ": abstract_id_b3 = get_style_abstract_id(styles_root, numbering_root, "B3") if abstract_id_b3 is not None and abstract_num_id is not None: update_list_instance_to_style(numbering_root, abstract_num_id, abstract_id_b3) if abstract_id_b3 is not None: update_list_instance_to_style(numbering_root, num_id, abstract_id_b3) compact_style.set(f"{{{ns['w']}}}val", "B3") else: Loading Loading @@ -1382,16 +1469,16 @@ def update_lists(docx_input, docx_output): elif para.xpath('./w:pPr/w:numPr/w:ilvl[@w:val="3"]', namespaces=ns): if para.xpath('ancestor::w:tbl', namespaces=ns): abstract_id_tb4 = get_style_abstract_id(styles_root, numbering_root, "TB4") if abstract_id_tb4 is not None and abstract_num_id is not None: update_list_instance_to_style(numbering_root, abstract_num_id, abstract_id_tb4) if abstract_id_tb4 is not None: update_list_instance_to_style(numbering_root, num_id, abstract_id_tb4) compact_style.set(f"{{{ns['w']}}}val", "TB4") counter_table += 1 else: if not lvl_text == " ": abstract_id_b4 = get_style_abstract_id(styles_root, numbering_root, "B4") if abstract_id_b4 is not None and abstract_num_id is not None: update_list_instance_to_style(numbering_root, abstract_num_id, abstract_id_b4) if abstract_id_b4 is not None: update_list_instance_to_style(numbering_root, num_id, abstract_id_b4) compact_style.set(f"{{{ns['w']}}}val", "B4") else: Loading Loading @@ -1421,16 +1508,16 @@ def update_lists(docx_input, docx_output): elif para.xpath('./w:pPr/w:numPr/w:ilvl[@w:val="4"]', namespaces=ns): if para.xpath('ancestor::w:tbl', namespaces=ns): abstract_id_tb5 = get_style_abstract_id(styles_root, numbering_root, "TB5") if abstract_id_tb5 is not None and abstract_num_id is not None: update_list_instance_to_style(numbering_root, abstract_num_id, abstract_id_tb5) if abstract_id_tb5 is not None: update_list_instance_to_style(numbering_root, num_id, abstract_id_tb5) compact_style.set(f"{{{ns['w']}}}val", "TB5") counter_table += 1 else: if not lvl_text == " ": abstract_id_b5 = get_style_abstract_id(styles_root, numbering_root, "B5") if abstract_id_b5 is not None and abstract_num_id is not None: update_list_instance_to_style(numbering_root, abstract_num_id, abstract_id_b5) if abstract_id_b5 is not None: update_list_instance_to_style(numbering_root, num_id, abstract_id_b5) compact_style.set(f"{{{ns['w']}}}val", "B5") else: Loading Loading @@ -1458,6 +1545,20 @@ def update_lists(docx_input, docx_output): # pPr.remove(numPr) counter_b5 += 1 if not para_removed: style_for_indent = compact_style.get(f"{{{ns['w']}}}val") fallback_abstract = None if style_for_indent in ("BN", "B1", "B2", "B3", "B4", "B5", "TB1", "TB2", "TB3", "TB4", "TB5"): fallback_abstract = get_style_abstract_id( styles_root, numbering_root, style_for_indent ) elif style_for_indent == "BL": fallback_abstract = get_style_abstract_id( styles_root, numbering_root, "BL" ) apply_list_level_indent( pPr, numbering_root, num_id, ilvl, fallback_abstract ) pPr.insert(0, compact_style) counter_compact += 1 continue Loading Loading @@ -1635,6 +1736,12 @@ def update_lists(docx_input, docx_output): print(f'Updated {counter_numbered} numbered list items') xml_data = etree.tostring(root, xml_declaration=True, encoding="UTF-8", standalone="yes") numbering_data = None styles_data = None if numbering_modified and numbering_root is not None: numbering_data = etree.tostring(numbering_root, xml_declaration=True, encoding="UTF-8", standalone="yes") if styles_modified and styles_root is not None: styles_data = etree.tostring(styles_root, xml_declaration=True, encoding="UTF-8", standalone="yes") # Create temp file tmp_fd, tmp_path = tempfile.mkstemp(suffix=".docx") Loading @@ -1644,10 +1751,19 @@ def update_lists(docx_input, docx_output): # Write new docx to temp file with zipfile.ZipFile(docx_input, 'r') as zin, zipfile.ZipFile(tmp_path, 'w', zipfile.ZIP_DEFLATED) as zout: for item in zin.infolist(): if item.filename != "word/document.xml": if item.filename == "word/document.xml": continue if item.filename == "word/numbering.xml" and numbering_data is not None: continue if item.filename == "word/styles.xml" and styles_data is not None: continue data = zin.read(item.filename) zout.writestr(item.filename, data) zout.writestr("word/document.xml", xml_data) if numbering_data is not None: zout.writestr("word/numbering.xml", numbering_data) if styles_data is not None: zout.writestr("word/styles.xml", styles_data) # Write to output file shutil.move(tmp_path, docx_output) Loading Loading
generateBaseline/postprocessing.py +164 −48 Viewed Changes for generateBaseline/postprocessing.py: 164 added lines, 48 removed lines. Original line number Diff line number Diff line Loading @@ -1052,20 +1052,20 @@ def update_lists(docx_input, docx_output): - Tuple (bool, str): (True, num_fmt) if numbered list, (False, num_fmt) if unnumbered, (None, None) if cannot determine """ if numbering_root is None: return (None, None) # Cannot determine without numbering.xml return (None, None, None, None, None) # Cannot determine without numbering.xml # Get numId and ilvl from paragraph numId_elem = para.xpath('./w:pPr/w:numPr/w:numId', namespaces=ns) ilvl_elem = para.xpath('./w:pPr/w:numPr/w:ilvl', namespaces=ns) if not numId_elem or not ilvl_elem: return (None, None) return (None, None, None, None, None) numId = numId_elem[0].get(f"{{{ns['w']}}}val") ilvl = ilvl_elem[0].get(f"{{{ns['w']}}}val") if numId is None or ilvl is None: return (None, None) return (None, None, None, None, None) try: # Find the num element with this numId Loading @@ -1074,16 +1074,16 @@ def update_lists(docx_input, docx_output): namespaces=ns ) if not num_elem: return (None, None, None) return (None, None, None, None, None) # Get the abstractNumId abstract_num_id_elem = num_elem[0].xpath('./w:abstractNumId', namespaces=ns) if not abstract_num_id_elem: return (None, None, None) return (None, None, None, None, None) abstract_num_id = abstract_num_id_elem[0].get(f"{{{ns['w']}}}val") if abstract_num_id is None: return (None, None, None) return (None, None, None, None, None) # Find the abstractNum abstract_num = numbering_root.xpath( Loading @@ -1091,7 +1091,7 @@ def update_lists(docx_input, docx_output): namespaces=ns ) if not abstract_num: return (None, None, None, None) return (None, None, None, None, None) # Check the format for this level numFmt_elem = abstract_num[0].xpath( Loading @@ -1099,7 +1099,7 @@ def update_lists(docx_input, docx_output): namespaces=ns ) if not numFmt_elem: return (None, None, None, None) return (None, None, None, None, None) numFmt = numFmt_elem[0].get(f"{{{ns['w']}}}val") Loading Loading @@ -1133,16 +1133,16 @@ def update_lists(docx_input, docx_output): 'check', 'arrow', 'arrowhead', 'rtArrow', 'hyphen'] if numFmt in numbered_formats: return (True, numFmt, lvl_text, abstract_num_id) # Return (True, format string) return (True, numFmt, lvl_text, abstract_num_id, numId) elif numFmt in unnumbered_formats: return (False, numFmt, lvl_text, abstract_num_id) # Return (False, format string) return (False, numFmt, lvl_text, abstract_num_id, numId) else: # Unknown format, default to unnumbered return (False, numFmt, lvl_text, abstract_num_id) return (False, numFmt, lvl_text, abstract_num_id, numId) except Exception as e: # If any error occurs, return None to fall back to heuristic return (None, None, None, None) return (None, None, None, None, None) counter_regular = 0 counter_b1 = 0 counter_b2 = 0 Loading @@ -1153,6 +1153,9 @@ def update_lists(docx_input, docx_output): counter_table = 0 counter_numbered = 0 numbering_modified = False styles_modified = False # Track processed paragraphs to avoid reprocessing processed_paras = set() Loading Loading @@ -1192,36 +1195,107 @@ def update_lists(docx_input, docx_output): text += text_elem.text return text style_abstract_id_cache = {} def get_style_abstract_id(styles_root, numbering_root, style_id): """ Find the abstractNumId used by the given style. Find the abstractNumId used by the given list paragraph style. """ nonlocal styles_modified if style_id in style_abstract_id_cache: return style_abstract_id_cache[style_id] styles = styles_root style_elements = styles.xpath(f'//w:style[@w:styleId="{style_id}"]', namespaces=ns) if not style_elements: print(f'Style {style_id} not found') style_abstract_id_cache[style_id] = None return None style = style_elements[0] num_id_el = style.xpath('.//w:numId', namespaces=ns) if not num_id_el: style_abstract_id_cache[style_id] = None return None style_num_id = num_id_el[0].get(qn('w:val')) if style_num_id is None or numbering_root is None: style_abstract_id_cache[style_id] = None return None num_elem = numbering_root.xpath( f'.//w:num[@w:numId="{style_num_id}"]', namespaces=ns, ) if not num_elem: style_abstract_id_cache[style_id] = None return None #raise ValueError(f"Style {style_id} has no numId") # Remove numPr from style if it exists abstract_num_id_elem = num_elem[0].xpath('./w:abstractNumId', namespaces=ns) if not abstract_num_id_elem: style_abstract_id_cache[style_id] = None return None abstract_id = abstract_num_id_elem[0].get(qn('w:val')) style_abstract_id_cache[style_id] = abstract_id # Remove numPr from style once; paragraph-level numPr drives numbering. num_pr = style.xpath('.//w:numPr', namespaces=ns) if num_pr: for el in num_pr: el.getparent().remove(el) styles_modified = True return num_id_el[0].get(qn('w:val')) return abstract_id def update_list_instance_to_style(numbering_root, abstract_num_id, abstract_id_bn): def update_list_instance_to_style(numbering_root, num_id, target_abstract_num_id): nonlocal numbering_modified if num_id is None or target_abstract_num_id is None: return for num in numbering_root.xpath(f'//w:num[@w:abstractNumId="{abstract_num_id}"]', namespaces=ns): for num in numbering_root.xpath(f'//w:num[@w:numId="{num_id}"]', namespaces=ns): abstract = num.xpath('./w:abstractNumId', namespaces=ns)[0] old_id = abstract.get(qn('w:val')) abstract.set(qn('w:val'), abstract_id_bn) if abstract.get(qn('w:val')) != target_abstract_num_id: abstract.set(qn('w:val'), target_abstract_num_id) numbering_modified = True def get_abstract_id_for_num(numbering_root, num_id): if numbering_root is None or num_id is None: return None nums = numbering_root.xpath(f'//w:num[@w:numId="{num_id}"]', namespaces=ns) if not nums: return None abstract_id_elem = nums[0].xpath('./w:abstractNumId', namespaces=ns) return abstract_id_elem[0].get(qn('w:val')) if abstract_id_elem else None def apply_list_level_indent(pPr, numbering_root, num_id, ilvl, fallback_abstract_id=None): """Set paragraph indent from the active numbering level definition.""" if numbering_root is None: return abstract_id = get_abstract_id_for_num(numbering_root, num_id) or fallback_abstract_id if abstract_id is None: return lvl_ppr_elems = numbering_root.xpath( f'.//w:abstractNum[@w:abstractNumId="{abstract_id}"]/w:lvl[@w:ilvl="{ilvl}"]/w:pPr', namespaces=ns, ) if not lvl_ppr_elems and fallback_abstract_id and fallback_abstract_id != abstract_id: lvl_ppr_elems = numbering_root.xpath( f'.//w:abstractNum[@w:abstractNumId="{fallback_abstract_id}"]/w:lvl[@w:ilvl="{ilvl}"]/w:pPr', namespaces=ns, ) if not lvl_ppr_elems: return lvl_ppr = lvl_ppr_elems[0] for tag in ('ind', 'tabs'): for el in pPr.xpath(f'./w:{tag}', namespaces=ns): pPr.remove(el) src = lvl_ppr.find(f'{{{ns["w"]}}}{tag}') if src is not None: pPr.append(etree.fromstring(etree.tostring(src))) for para in paragraphs: para_removed = False Loading @@ -1240,20 +1314,33 @@ def update_lists(docx_input, docx_output): else: compact_style = OxmlElement('w:pStyle') # Check if it is a numbered list and get the format is_numbered, num_format, lvl_text, abstract_num_id = is_numbered_list(para, numbering_root) is_numbered, num_format, lvl_text, abstract_num_id, num_id = is_numbered_list(para, numbering_root) ilvl_elem = para.xpath('./w:pPr/w:numPr/w:ilvl', namespaces=ns) ilvl = ilvl_elem[0].get(f"{{{ns['w']}}}val") if ilvl_elem else "0" if is_numbered: # is_numbered is True if numbered, False if unnumbered, None if cannot determine #pStyle = OxmlElement('w:pStyle') # If format is decimal (numbers), use BN; otherwise use BL # Style each level by its numbering format; set abstractNum only at top level # so nested letter items keep BN multilevel numbering but get BL paragraph style. if num_format == 'decimal': if ilvl == "0": abstract_id_bn = get_style_abstract_id(styles_root, numbering_root, "BN") if abstract_id_bn is not None and abstract_num_id is not None: update_list_instance_to_style(numbering_root, abstract_num_id, abstract_id_bn) if abstract_id_bn is not None: update_list_instance_to_style(numbering_root, num_id, abstract_id_bn) compact_style.set(f"{{{ns['w']}}}val", "BN") else: if ilvl == "0": abstract_id_bl = get_style_abstract_id(styles_root, numbering_root, "BL") if abstract_id_bl is not None and abstract_num_id is not None: update_list_instance_to_style(numbering_root, abstract_num_id, abstract_id_bl) if abstract_id_bl is not None: update_list_instance_to_style(numbering_root, num_id, abstract_id_bl) compact_style.set(f"{{{ns['w']}}}val", "BL") apply_list_level_indent( pPr, numbering_root, num_id, ilvl, get_style_abstract_id(styles_root, numbering_root, "BN") if num_format == 'decimal' else get_style_abstract_id(styles_root, numbering_root, "BL"), ) pPr.insert(0, compact_style) # Remove numId from numPr #numPr = pPr.xpath('./w:numPr', namespaces=ns)[0] Loading @@ -1265,16 +1352,16 @@ def update_lists(docx_input, docx_output): if para.xpath('./w:pPr/w:numPr/w:ilvl[@w:val="0"]', namespaces=ns): if para.xpath('ancestor::w:tbl', namespaces=ns): abstract_id_tb1 = get_style_abstract_id(styles_root, numbering_root, "TB1") if abstract_id_tb1 is not None and abstract_num_id is not None: update_list_instance_to_style(numbering_root, abstract_num_id, abstract_id_tb1) if abstract_id_tb1 is not None: update_list_instance_to_style(numbering_root, num_id, abstract_id_tb1) compact_style.set(f"{{{ns['w']}}}val", "TB1") counter_table += 1 else: if not lvl_text == " ": abstract_id_b1 = get_style_abstract_id(styles_root, numbering_root, "B1") if abstract_id_b1 is not None and abstract_num_id is not None: update_list_instance_to_style(numbering_root, abstract_num_id, abstract_id_b1) if abstract_id_b1 is not None: update_list_instance_to_style(numbering_root, num_id, abstract_id_b1) compact_style.set(f"{{{ns['w']}}}val", "B1") else: Loading Loading @@ -1304,16 +1391,16 @@ def update_lists(docx_input, docx_output): elif para.xpath('./w:pPr/w:numPr/w:ilvl[@w:val="1"]', namespaces=ns): if para.xpath('ancestor::w:tbl', namespaces=ns): abstract_id_tb2 = get_style_abstract_id(styles_root, numbering_root, "TB2") if abstract_id_tb2 is not None and abstract_num_id is not None: update_list_instance_to_style(numbering_root, abstract_num_id, abstract_id_tb2) if abstract_id_tb2 is not None: update_list_instance_to_style(numbering_root, num_id, abstract_id_tb2) compact_style.set(f"{{{ns['w']}}}val", "TB2") counter_table += 1 else: if not lvl_text == " ": abstract_id_b2 = get_style_abstract_id(styles_root, numbering_root, "B2") if abstract_id_b2 is not None and abstract_num_id is not None: update_list_instance_to_style(numbering_root, abstract_num_id, abstract_id_b2) if abstract_id_b2 is not None: update_list_instance_to_style(numbering_root, num_id, abstract_id_b2) compact_style.set(f"{{{ns['w']}}}val", "B2") else: Loading Loading @@ -1343,16 +1430,16 @@ def update_lists(docx_input, docx_output): elif para.xpath('./w:pPr/w:numPr/w:ilvl[@w:val="2"]', namespaces=ns): if para.xpath('ancestor::w:tbl', namespaces=ns): abstract_id_tb3 = get_style_abstract_id(styles_root, numbering_root, "TB3") if abstract_id_tb3 is not None and abstract_num_id is not None: update_list_instance_to_style(numbering_root, abstract_num_id, abstract_id_tb3) if abstract_id_tb3 is not None: update_list_instance_to_style(numbering_root, num_id, abstract_id_tb3) compact_style.set(f"{{{ns['w']}}}val", "TB3") counter_table += 1 else: if not lvl_text == " ": abstract_id_b3 = get_style_abstract_id(styles_root, numbering_root, "B3") if abstract_id_b3 is not None and abstract_num_id is not None: update_list_instance_to_style(numbering_root, abstract_num_id, abstract_id_b3) if abstract_id_b3 is not None: update_list_instance_to_style(numbering_root, num_id, abstract_id_b3) compact_style.set(f"{{{ns['w']}}}val", "B3") else: Loading Loading @@ -1382,16 +1469,16 @@ def update_lists(docx_input, docx_output): elif para.xpath('./w:pPr/w:numPr/w:ilvl[@w:val="3"]', namespaces=ns): if para.xpath('ancestor::w:tbl', namespaces=ns): abstract_id_tb4 = get_style_abstract_id(styles_root, numbering_root, "TB4") if abstract_id_tb4 is not None and abstract_num_id is not None: update_list_instance_to_style(numbering_root, abstract_num_id, abstract_id_tb4) if abstract_id_tb4 is not None: update_list_instance_to_style(numbering_root, num_id, abstract_id_tb4) compact_style.set(f"{{{ns['w']}}}val", "TB4") counter_table += 1 else: if not lvl_text == " ": abstract_id_b4 = get_style_abstract_id(styles_root, numbering_root, "B4") if abstract_id_b4 is not None and abstract_num_id is not None: update_list_instance_to_style(numbering_root, abstract_num_id, abstract_id_b4) if abstract_id_b4 is not None: update_list_instance_to_style(numbering_root, num_id, abstract_id_b4) compact_style.set(f"{{{ns['w']}}}val", "B4") else: Loading Loading @@ -1421,16 +1508,16 @@ def update_lists(docx_input, docx_output): elif para.xpath('./w:pPr/w:numPr/w:ilvl[@w:val="4"]', namespaces=ns): if para.xpath('ancestor::w:tbl', namespaces=ns): abstract_id_tb5 = get_style_abstract_id(styles_root, numbering_root, "TB5") if abstract_id_tb5 is not None and abstract_num_id is not None: update_list_instance_to_style(numbering_root, abstract_num_id, abstract_id_tb5) if abstract_id_tb5 is not None: update_list_instance_to_style(numbering_root, num_id, abstract_id_tb5) compact_style.set(f"{{{ns['w']}}}val", "TB5") counter_table += 1 else: if not lvl_text == " ": abstract_id_b5 = get_style_abstract_id(styles_root, numbering_root, "B5") if abstract_id_b5 is not None and abstract_num_id is not None: update_list_instance_to_style(numbering_root, abstract_num_id, abstract_id_b5) if abstract_id_b5 is not None: update_list_instance_to_style(numbering_root, num_id, abstract_id_b5) compact_style.set(f"{{{ns['w']}}}val", "B5") else: Loading Loading @@ -1458,6 +1545,20 @@ def update_lists(docx_input, docx_output): # pPr.remove(numPr) counter_b5 += 1 if not para_removed: style_for_indent = compact_style.get(f"{{{ns['w']}}}val") fallback_abstract = None if style_for_indent in ("BN", "B1", "B2", "B3", "B4", "B5", "TB1", "TB2", "TB3", "TB4", "TB5"): fallback_abstract = get_style_abstract_id( styles_root, numbering_root, style_for_indent ) elif style_for_indent == "BL": fallback_abstract = get_style_abstract_id( styles_root, numbering_root, "BL" ) apply_list_level_indent( pPr, numbering_root, num_id, ilvl, fallback_abstract ) pPr.insert(0, compact_style) counter_compact += 1 continue Loading Loading @@ -1635,6 +1736,12 @@ def update_lists(docx_input, docx_output): print(f'Updated {counter_numbered} numbered list items') xml_data = etree.tostring(root, xml_declaration=True, encoding="UTF-8", standalone="yes") numbering_data = None styles_data = None if numbering_modified and numbering_root is not None: numbering_data = etree.tostring(numbering_root, xml_declaration=True, encoding="UTF-8", standalone="yes") if styles_modified and styles_root is not None: styles_data = etree.tostring(styles_root, xml_declaration=True, encoding="UTF-8", standalone="yes") # Create temp file tmp_fd, tmp_path = tempfile.mkstemp(suffix=".docx") Loading @@ -1644,10 +1751,19 @@ def update_lists(docx_input, docx_output): # Write new docx to temp file with zipfile.ZipFile(docx_input, 'r') as zin, zipfile.ZipFile(tmp_path, 'w', zipfile.ZIP_DEFLATED) as zout: for item in zin.infolist(): if item.filename != "word/document.xml": if item.filename == "word/document.xml": continue if item.filename == "word/numbering.xml" and numbering_data is not None: continue if item.filename == "word/styles.xml" and styles_data is not None: continue data = zin.read(item.filename) zout.writestr(item.filename, data) zout.writestr("word/document.xml", xml_data) if numbering_data is not None: zout.writestr("word/numbering.xml", numbering_data) if styles_data is not None: zout.writestr("word/styles.xml", styles_data) # Write to output file shutil.move(tmp_path, docx_output) Loading