princemaxp's picture
Create app.py
6b2ba4f verified
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()