File size: 2,805 Bytes
498db6b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
from xml.etree import ElementTree as ET

def find_list_indentation_level(para, doc):
    namespace = {"w": "http://schemas.openxmlformats.org/wordprocessingml/2006/main"}
    xml_para = para._p.xml
    try:
        xml_numbering = doc.xdoc._part.numbering_part.element.xml
    except:
        return False, 0
    root_para = ET.fromstring(xml_para)
    root_numbering = ET.fromstring(xml_numbering)
    abstract_num_reference = []
    for item in root_numbering:
        if item.tag == "{http://schemas.openxmlformats.org/wordprocessingml/2006/main}num":
            abstract_num_reference.append(item)
    is_numPr = root_para.find(".//w:numPr", namespaces=namespace)
    is_style = root_para.find(".//w:pStyle", namespaces=namespace)
    is_numId = root_para.find(".//w:numId", namespaces=namespace)
    is_lvl = root_para.find(".//w:ilvl", namespaces=namespace)
    if is_numPr != None:
        if is_numId != None and is_lvl != None:
            return True, int(is_lvl.attrib["{http://schemas.openxmlformats.org/wordprocessingml/2006/main}val"]) + 1
        elif is_numId != None and is_lvl == None:
            numId = int(is_numId.attrib["{http://schemas.openxmlformats.org/wordprocessingml/2006/main}val"])
            is_abstractNumId = [item for item in abstract_num_reference if item.attrib["{http://schemas.openxmlformats.org/wordprocessingml/2006/main}numId"] == str(numId)][0]
            numID_reference = is_abstractNumId.find(".//w:abstractNumId", namespaces=namespace).attrib["{http://schemas.openxmlformats.org/wordprocessingml/2006/main}val"]
            real_numID = root_numbering.find(f".//w:abstractNum[@w:abstractNumId='{int(numID_reference)}']", namespaces=namespace)
            if style_Id == None:
                return False, 0
            style_Id = is_style.attrib["{http://schemas.openxmlformats.org/wordprocessingml/2006/main}val"]
            is_style_in_numbering = root_numbering.find(f".//w:pStyle[@w:val='{style_Id}']...", namespaces=namespace)
            lvl = real_numID.find(".//w:ilvl", namespaces=namespace)
            return True, int(lvl.attrib["{http://schemas.openxmlformats.org/wordprocessingml/2006/main}ilvl"]) + 1
    else:
        if is_style == None:
            return False, 0
        else:
            #check if there is a style element in the root_numbering
            style_Id = is_style.attrib["{http://schemas.openxmlformats.org/wordprocessingml/2006/main}val"]
            is_style_in_numbering = root_numbering.find(f".//w:pStyle[@w:val='{style_Id}']...", namespaces=namespace)
            if is_style_in_numbering == None:
                return False, 0
            else:
                ilvl = is_style_in_numbering.attrib["{http://schemas.openxmlformats.org/wordprocessingml/2006/main}ilvl"]
                return True, int(ilvl) + 1