Commit 1eff553a authored by Miguel Angel Reina Ortega's avatar Miguel Angel Reina Ortega
Browse files

Test to change bullet list characters

parent b37c2774
Loading
Loading
Loading
Loading
Loading
+88
Changes for generateBaseline/postprocessing.py: 88 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -1039,6 +1039,89 @@ def update_lists(docx_input, docx_output):
    """
    ns = {"w": "http://schemas.openxmlformats.org/wordprocessingml/2006/main"}

    def patch_bullet_characters(root):
        """
        root: lxml root element of numbering.xml
        """
        nonlocal numbering_modified

        BASE_LEFT = 720      # 0.5"
        HANGING = 360        # 0.25"
        # Bullet character for each nesting level
        BULLET_CHARS = {
                0: {"char": "", "font": "Symbol", "left": BASE_LEFT, "hanging": HANGING},      # large bullet
                1: {"char": "", "font": "Wingdings", "left": BASE_LEFT + HANGING, "hanging": HANGING},   # small square
                2: {"char": "o", "font": "Courier New", "left": BASE_LEFT + 2 * HANGING, "hanging": HANGING}, # hollow circle
                3: {"char": "-", "font": "Aptos", "left": BASE_LEFT + 3 * HANGING, "hanging": HANGING},       # dash
                4: {"char": "", "font": "Symbol", "left": BASE_LEFT + 4 * HANGING, "hanging": HANGING},      # large bullet
                5: {"char": "", "font": "Wingdings", "left": BASE_LEFT + 5 * HANGING, "hanging": HANGING},   # small square
                6: {"char": "o", "font": "Courier New", "left": BASE_LEFT + 6 * HANGING, "hanging": HANGING}, # hollow circle
                7: {"char": "-", "font": "Aptos", "left": BASE_LEFT + 7 * HANGING, "hanging": HANGING},       # dash
                8: {"char": "", "font": "Symbol", "left": BASE_LEFT + 8 * HANGING, "hanging": HANGING},      # large bullet
                9: {"char": "", "font": "Wingdings", "left": BASE_LEFT + 9 * HANGING, "hanging": HANGING},   # small square
                
            #0: {"char": "•", "font": "Symbol"},   # U+2022
            #1: {"char": "▪", "font": "Symbol"},   # U+25AA
            #2: {"char": "○", "font": "Symbol"},   # U+25CB
            #3: {"char": "▫", "font": "Symbol"},   # U+25AB
            #4: {"char": "◆", "font": "Symbol"},   # U+25C6
            #5: {"char": "■", "font": "Symbol"},   # U+25A0
            #6: {"char": "►", "font": "Symbol"},   # U+25BA
            #7: {"char": "◦", "font": "Symbol"},   # U+25E6
            #8: {"char": "‣", "font": "Symbol"},   # U+2023
        }
        for lvl in root.xpath(".//w:lvl", namespaces=ns):

            num_fmt = lvl.find("w:numFmt", namespaces=ns)
            if num_fmt is None:
                continue

            if num_fmt.get(f"{{{ns['w']}}}val") != "bullet":
                continue

            ilvl = int(lvl.get(f"{{{ns['w']}}}ilvl"))

            if ilvl not in BULLET_CHARS:
                continue

            lvl_text = lvl.find("w:lvlText", namespaces=ns)
            if lvl_text is None:
                continue
            
            if lvl_text.get(f"{{{ns['w']}}}val") != " ":
                lvl_text.set(f"{{{ns['w']}}}val", BULLET_CHARS[ilvl]["char"])

            rfonts = lvl.find("w:rPr/w:rFonts", namespaces=ns)
            if rfonts is None:
                rpr = lvl.find("w:rPr", namespaces=ns)
                if rpr is None:
                    rpr = etree.SubElement(lvl, qn("w:rPr"))
                rfonts = etree.SubElement(rpr, qn("w:rFonts"))

            font = BULLET_CHARS[ilvl]["font"]

            for attr in ("ascii", "hAnsi", "eastAsia", "cs"):
                rfonts.set(qn(f"w:{attr}"), font)

            rfonts.set(qn("w:hint"), "default")

            # Update indentation
            indent = lvl.find("w:pPr/w:ind", namespaces=ns)
            if indent is not None:
                indent.set(qn("w:left"), str(BULLET_CHARS[ilvl]["left"]))
                indent.set(qn("w:hanging"), str(BULLET_CHARS[ilvl]["hanging"]))

            # Update the tab stop so the text starts at left + hanging
            tab = lvl.find("w:pPr/w:tabs/w:tab", namespaces=ns)
            if tab is not None:
                text_start = (
                    BULLET_CHARS[ilvl]["left"] +
                    BULLET_CHARS[ilvl]["hanging"]
                )
                tab.set(qn("w:pos"), str(text_start))

        numbering_modified = True

    # Read XML files
    with zipfile.ZipFile(docx_input, 'r') as zin:
        xml_data = zin.read("word/document.xml")
@@ -1046,6 +1129,7 @@ def update_lists(docx_input, docx_output):
        try:
            numbering_data = zin.read("word/numbering.xml")
            numbering_root = etree.fromstring(numbering_data)
            patch_bullet_characters(numbering_root)
        except KeyError:
            numbering_root = None

@@ -1058,6 +1142,10 @@ def update_lists(docx_input, docx_output):
            
    root = etree.fromstring(xml_data)
    
    



    def is_numbered_list(para, numbering_root):
        """
        Check if a paragraph is part of a numbered list by checking numbering.xml.