Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import xml.etree.ElementTree as ET
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import gradio as gr
|
| 4 |
+
|
| 5 |
+
def xml_to_excel(xml_file):
|
| 6 |
+
tree = ET.parse(xml_file.name)
|
| 7 |
+
root = tree.getroot()
|
| 8 |
+
|
| 9 |
+
# Detect the most common child tag under root
|
| 10 |
+
children_tags = [child.tag for child in root]
|
| 11 |
+
if not children_tags:
|
| 12 |
+
return None
|
| 13 |
+
entry_tag = max(set(children_tags), key=children_tags.count)
|
| 14 |
+
|
| 15 |
+
data = []
|
| 16 |
+
for entry in root.findall(entry_tag):
|
| 17 |
+
row = {}
|
| 18 |
+
|
| 19 |
+
# Attributes
|
| 20 |
+
for attr_name, attr_value in entry.attrib.items():
|
| 21 |
+
row[attr_name] = attr_value
|
| 22 |
+
|
| 23 |
+
# Child elements
|
| 24 |
+
for child in entry:
|
| 25 |
+
row[child.tag] = child.text.strip() if child.text else ""
|
| 26 |
+
|
| 27 |
+
data.append(row)
|
| 28 |
+
|
| 29 |
+
if not data:
|
| 30 |
+
return None
|
| 31 |
+
|
| 32 |
+
# Convert to DataFrame
|
| 33 |
+
df = pd.DataFrame(data)
|
| 34 |
+
|
| 35 |
+
# Save Excel file
|
| 36 |
+
output_file = "output.xlsx"
|
| 37 |
+
df.to_excel(output_file, index=False)
|
| 38 |
+
|
| 39 |
+
return output_file
|
| 40 |
+
|
| 41 |
+
# Gradio interface
|
| 42 |
+
demo = gr.Interface(
|
| 43 |
+
fn=xml_to_excel,
|
| 44 |
+
inputs=gr.File(label="Upload XML File", file_types=[".xml"]),
|
| 45 |
+
outputs=gr.File(label="Download Excel File"),
|
| 46 |
+
title="XML to Excel Converter",
|
| 47 |
+
description="Upload an XML file and get the parsed Excel file."
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
if __name__ == "__main__":
|
| 51 |
+
demo.launch()
|