Spaces:
Running
Running
| import streamlit as st | |
| import pandas as pd | |
| import re | |
| from io import BytesIO | |
| st.set_page_config(page_title="XML to Excel Converter", layout="wide") | |
| st.title("📄 XML to Excel Converter") | |
| st.write("Extract **ErrorCode** and **Message** from XML text and export to Excel.") | |
| # --- Text Area Container --- | |
| xml_input = st.text_area( | |
| label="XML Data", | |
| height=300, | |
| placeholder="Paste your XML exception data here..." | |
| ) | |
| # --- Button --- | |
| if st.button("Generate Excel Data"): | |
| if not xml_input.strip(): | |
| st.warning("⚠️ Please paste XML data before generating Excel.") | |
| else: | |
| # Regex to extract ErrorCode and Message values inside quotes | |
| pattern = r'ErrorCode="(.*?)".*?Message="(.*?)"' | |
| matches = re.findall(pattern, xml_input, re.DOTALL) | |
| if not matches: | |
| st.error("❌ No valid ErrorCode and Message found in the XML.") | |
| else: | |
| # Create DataFrame | |
| df = pd.DataFrame(matches, columns=["ErrorCode", "Message"]) | |
| # Display extracted data | |
| st.success("✅ Data extracted successfully!") | |
| st.dataframe(df, use_container_width=True) | |
| # Convert to Excel | |
| output = BytesIO() | |
| with pd.ExcelWriter(output, engine="openpyxl") as writer: | |
| df.to_excel(writer, index=False, sheet_name="Errors") | |
| # Download Button | |
| st.download_button( | |
| label="⬇️ Download Excel", | |
| data=output.getvalue(), | |
| file_name="ErrorCodes.xlsx", | |
| mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" | |
| ) | |