File size: 1,267 Bytes
6b2ba4f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
46
47
48
49
50
51
52
import xml.etree.ElementTree as ET
import pandas as pd
import gradio as gr

def xml_to_excel(xml_file):
    tree = ET.parse(xml_file.name)
    root = tree.getroot()

    # Detect the most common child tag under root
    children_tags = [child.tag for child in root]
    if not children_tags:
        return None
    entry_tag = max(set(children_tags), key=children_tags.count)

    data = []
    for entry in root.findall(entry_tag):
        row = {}

        # Attributes
        for attr_name, attr_value in entry.attrib.items():
            row[attr_name] = attr_value

        # Child elements
        for child in entry:
            row[child.tag] = child.text.strip() if child.text else ""

        data.append(row)

    if not data:
        return None

    # Convert to DataFrame
    df = pd.DataFrame(data)

    # Save Excel file
    output_file = "output.xlsx"
    df.to_excel(output_file, index=False)

    return output_file

# Gradio interface
demo = gr.Interface(
    fn=xml_to_excel,
    inputs=gr.File(label="Upload XML File", file_types=[".xml"]),
    outputs=gr.File(label="Download Excel File"),
    title="XML to Excel Converter",
    description="Upload an XML file and get the parsed Excel file."
)

if __name__ == "__main__":
    demo.launch()