Commit 74aefcd5 authored by Miguel Angel Reina Ortega's avatar Miguel Angel Reina Ortega
Browse files

Try fix lists

parent 1c3272ac
Loading
Loading
Loading
Loading
Loading
+112 −34
Original line number Diff line number Diff line
@@ -1031,6 +1031,8 @@ def update_lists(docx_input, docx_output):
    counter_b1 = 0
    counter_b2 = 0
    counter_b3 = 0
    counter_b4 = 0
    counter_b5 = 0
    counter_compact = 0
    counter_table = 0
    counter_numbered = 0
@@ -1162,10 +1164,9 @@ def update_lists(docx_input, docx_output):
                        else:
                            # Take all elements except pPr (usually only runs and hyperlinks) in this paragraph and append them to the previous paragraph. New run starting by <w:br/>
                            previous_para = para.getprevious()
                            if previous_para is not None:
                            if previous_para is not None and previous_para.tag == f"{{{ns['w']}}}p":
                                if (previous_para.xpath('./w:pPr/w:pStyle[@w:val="SourceCode"]', namespaces=ns)):
                                    compact_style.set(f"{{{ns['w']}}}val", "B1")
                                    #continue
                                else:
                                    break_run = OxmlElement('w:r')
                                    break_run.append(OxmlElement('w:br'))
@@ -1173,11 +1174,12 @@ def update_lists(docx_input, docx_output):
                                    for element in para.xpath('./*', namespaces=ns):
                                        if element.tag != f"{{{ns['w']}}}pPr":
                                            previous_para.append(element)
                                    # Remove the original paragraph
                                    para.getparent().remove(para)
                                    para_removed = True
                                    #print(f'Removed the original paragraph')
                                    #compact_style.set(f"{{{ns['w']}}}val", "FP")
                            else:
                                compact_style.set(f"{{{ns['w']}}}val", "B1")
                    #Remove numPr from pPr
                    #if not para_removed:
                    #    numPr = pPr.xpath('./w:numPr', namespaces=ns)[0]
@@ -1201,23 +1203,22 @@ def update_lists(docx_input, docx_output):
                        else:
                           # Take all elements except pPr (usually only runs and hyperlinks) in this paragraph and append them to the previous paragraph. New run starting by <w:br/>
                            previous_para = para.getprevious()
                            if previous_para is not None:
                            if previous_para is not None and previous_para.tag == f"{{{ns['w']}}}p":
                                if (previous_para.xpath('./w:pPr/w:pStyle[@w:val="SourceCode"]', namespaces=ns)):
                                    compact_style.set(f"{{{ns['w']}}}val", "B2")
                                    #ontinue
                                else:
                                    if previous_para is not None:
                                    break_run = OxmlElement('w:r')
                                    break_run.append(OxmlElement('w:br'))
                                    previous_para.append(break_run)
                                    for element in para.xpath('./*', namespaces=ns):
                                        if element.tag != f"{{{ns['w']}}}pPr":
                                            previous_para.append(element)
                                        # Remove the original paragraph
                                    para.getparent().remove(para)
                                    para_removed = True
                                    #print(f'Removed the original paragraph')
                                    #compact_style.set(f"{{{ns['w']}}}val", "FP")
                            else:
                                compact_style.set(f"{{{ns['w']}}}val", "B2")
                    #Remove numPr from pPr
                    #if not para_removed:
                    #    numPr = pPr.xpath('./w:numPr', namespaces=ns)[0]
@@ -1241,28 +1242,105 @@ def update_lists(docx_input, docx_output):
                        else:
                            # Take all elements except pPr (usually only runs and hyperlinks) in this paragraph and append them to the previous paragraph. New run starting by <w:br/>
                            previous_para = para.getprevious()
                            if previous_para is not None:
                            if previous_para is not None and previous_para.tag == f"{{{ns['w']}}}p":
                                if (previous_para.xpath('./w:pPr/w:pStyle[@w:val="SourceCode"]', namespaces=ns)):
                                    compact_style.set(f"{{{ns['w']}}}val", "B3")
                                    #continue
                                else:
                                    if previous_para is not None:
                                    break_run = OxmlElement('w:r')
                                    break_run.append(OxmlElement('w:br'))
                                    previous_para.append(break_run)
                                    for element in para.xpath('./*', namespaces=ns):
                                        if element.tag != f"{{{ns['w']}}}pPr":
                                            previous_para.append(element)
                                        # Remove the original paragraph
                                    para.getparent().remove(para)
                                    para_removed = True
                                    #print(f'Removed the original paragraph')
                                    #compact_style.set(f"{{{ns['w']}}}val", "FP")
                            else:
                                compact_style.set(f"{{{ns['w']}}}val", "B3")
                    #Remove numPr from pPr
                    #if not para_removed:
                    #   numPr = pPr.xpath('./w:numPr', namespaces=ns)[0]
                    #   pPr.remove(numPr)
                    counter_b3 += 1
                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)

                        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)

                            compact_style.set(f"{{{ns['w']}}}val", "B4")
                        else:
                            # Take all elements except pPr (usually only runs and hyperlinks) in this paragraph and append them to the previous paragraph. New run starting by <w:br/>
                            previous_para = para.getprevious()
                            if previous_para is not None and previous_para.tag == f"{{{ns['w']}}}p":
                                if (previous_para.xpath('./w:pPr/w:pStyle[@w:val="SourceCode"]', namespaces=ns)):
                                    compact_style.set(f"{{{ns['w']}}}val", "B4")
                                else:
                                    break_run = OxmlElement('w:r')
                                    break_run.append(OxmlElement('w:br'))
                                    previous_para.append(break_run)
                                    for element in para.xpath('./*', namespaces=ns):
                                        if element.tag != f"{{{ns['w']}}}pPr":
                                            previous_para.append(element)
                                    para.getparent().remove(para)
                                    para_removed = True
                                    #print(f'Removed the original paragraph')
                                    #compact_style.set(f"{{{ns['w']}}}val", "FP")
                            else:
                                compact_style.set(f"{{{ns['w']}}}val", "B4")
                    #Remove numPr from pPr
                    #if not para_removed:
                    #   numPr = pPr.xpath('./w:numPr', namespaces=ns)[0]
                    #   pPr.remove(numPr)
                    counter_b4 += 1
                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)

                        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)

                            compact_style.set(f"{{{ns['w']}}}val", "B5")
                        else:
                            # Take all elements except pPr (usually only runs and hyperlinks) in this paragraph and append them to the previous paragraph. New run starting by <w:br/>
                            previous_para = para.getprevious()
                            if previous_para is not None and previous_para.tag == f"{{{ns['w']}}}p":
                                if (previous_para.xpath('./w:pPr/w:pStyle[@w:val="SourceCode"]', namespaces=ns)):
                                    compact_style.set(f"{{{ns['w']}}}val", "B5")
                                else:
                                    break_run = OxmlElement('w:r')
                                    break_run.append(OxmlElement('w:br'))
                                    previous_para.append(break_run)
                                    for element in para.xpath('./*', namespaces=ns):
                                        if element.tag != f"{{{ns['w']}}}pPr":
                                            previous_para.append(element)
                                    para.getparent().remove(para)
                                    para_removed = True
                                    #print(f'Removed the original paragraph')
                                    #compact_style.set(f"{{{ns['w']}}}val", "FP")
                            else:
                                compact_style.set(f"{{{ns['w']}}}val", "B5")
                    #Remove numPr from pPr
                    #if not para_removed:
                    #   numPr = pPr.xpath('./w:numPr', namespaces=ns)[0]
                    #   pPr.remove(numPr)
                    counter_b5 += 1
                
                pPr.insert(0, compact_style)
                counter_compact += 1
@@ -1436,7 +1514,7 @@ def update_lists(docx_input, docx_output):
                processed_paras.add(id(para))
                parent.remove(para)"""

    print(f'Updated {counter_b1} B1 style paragraphs, {counter_b2} B2 style paragraphs, {counter_b3} B3 style paragraphs')
    print(f'Updated {counter_b1} B1 style paragraphs, {counter_b2} B2 style paragraphs, {counter_b3} B3 style paragraphs, {counter_b4} B4 style paragraphs, {counter_b5} B5 style paragraphs')
    print(f'Updated {counter_compact} Compact style paragraphs, {counter_table} unnumbered list items in tables, {counter_regular} outside tables')
    print(f'Updated {counter_numbered} numbered list items')