JaganathC commited on
Commit
1bccc59
·
verified ·
1 Parent(s): 2adf8bd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -88
app.py CHANGED
@@ -2,104 +2,47 @@ import streamlit as st
2
  import pandas as pd
3
  import re
4
  from io import BytesIO
5
- import streamlit.components.v1 as components
6
 
7
- # -------------------------------------------------
8
- # Page Setup
9
- # -------------------------------------------------
10
- st.set_page_config(page_title="JSON Text to Excel", layout="wide")
11
- st.title("📘 JSON Text → Excel Converter(Jaganath's Bot)")
12
 
13
- st.markdown(
14
- "Paste JSON-like **text data** (not strict JSON). "
15
- "The app parses it line-by-line and generates Excel output. "
16
- "You can also copy the two columns to clipboard."
17
- )
18
-
19
- # -------------------------------------------------
20
- # Text Input (HF Safe)
21
- # -------------------------------------------------
22
- if "json_text" not in st.session_state:
23
- st.session_state.json_text = ""
24
 
25
- text_input = st.text_area(
26
- "JSON Data (Text Format)",
27
- height=380,
28
- key="json_text",
29
- placeholder='''"LABELS": {
30
- "ICN": {
31
- "ICN_SHIFT": "Shift Management"
32
- }
33
- }'''
34
  )
35
 
36
- # -------------------------------------------------
37
- # Line-by-line Parser (ROBUST)
38
- # -------------------------------------------------
39
- def parse_text_to_rows(text):
40
- rows = []
41
-
42
- for line in text.splitlines():
43
- line = line.strip()
44
- if not line:
45
- continue
46
-
47
- # Remove trailing commas
48
- line = line.rstrip(",")
49
-
50
- # Match "KEY": {
51
- obj_match = re.match(r'"([^"]+)"\s*:\s*\{', line)
52
- if obj_match:
53
- rows.append([obj_match.group(1), None])
54
- continue
55
-
56
- # Match "KEY": "VALUE"
57
- kv_match = re.match(r'"([^"]+)"\s*:\s*"([^"]*)"', line)
58
- if kv_match:
59
- rows.append([kv_match.group(1), kv_match.group(2)])
60
- continue
61
-
62
- return pd.DataFrame(
63
- rows,
64
- columns=["Object Name/Key Name", "Values"]
65
- )
66
-
67
- # -------------------------------------------------
68
- # Button Action
69
- # -------------------------------------------------
70
- if st.button("Generate Excel Data", type="primary"):
71
- raw_text = st.session_state.json_text
72
-
73
- if raw_text.strip() == "":
74
- st.warning("⚠️ Please paste JSON-like text data.")
75
  else:
76
- df = parse_text_to_rows(raw_text)
 
 
77
 
78
- if df.empty:
79
- st.error("❌ No valid key/value data found.")
80
  else:
81
- st.subheader("📊 Excel Preview")
82
- st.dataframe(df, use_container_width=True)
83
 
84
- # -------------------------
85
- # Copy to Clipboard Button (Only content)
86
- # -------------------------
87
- csv_text = df.to_csv(index=False, sep="\t", header=False) # <-- header=False
88
- copy_button_html = f"""
89
- <button onclick="navigator.clipboard.writeText(`{csv_text}`)">📋 Copy Two Columns</button>
90
- """
91
- components.html(copy_button_html, height=50)
92
 
93
- # -------------------------
94
- # Excel Export
95
- # -------------------------
96
- buffer = BytesIO()
97
- with pd.ExcelWriter(buffer, engine="xlsxwriter") as writer:
98
- df.to_excel(writer, index=False, sheet_name="Labels")
99
 
 
100
  st.download_button(
101
- "📥 Download Excel File",
102
- buffer.getvalue(),
103
- "JSON_Text_To_Excel.xlsx",
104
- "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
105
  )
 
2
  import pandas as pd
3
  import re
4
  from io import BytesIO
 
5
 
6
+ st.set_page_config(page_title="XML to Excel Converter", layout="wide")
 
 
 
 
7
 
8
+ st.title("📄 XML to Excel Converter")
9
+ st.write("Extract **ErrorCode** and **Message** from XML text and export to Excel.")
 
 
 
 
 
 
 
 
 
10
 
11
+ # --- Text Area Container ---
12
+ xml_input = st.text_area(
13
+ label="XML Data",
14
+ height=300,
15
+ placeholder="Paste your XML exception data here..."
 
 
 
 
16
  )
17
 
18
+ # --- Button ---
19
+ if st.button("Generate Excel Data"):
20
+ if not xml_input.strip():
21
+ st.warning("⚠️ Please paste XML data before generating Excel.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  else:
23
+ # Regex to extract ErrorCode and Message values inside quotes
24
+ pattern = r'ErrorCode="(.*?)".*?Message="(.*?)"'
25
+ matches = re.findall(pattern, xml_input, re.DOTALL)
26
 
27
+ if not matches:
28
+ st.error("❌ No valid ErrorCode and Message found in the XML.")
29
  else:
30
+ # Create DataFrame
31
+ df = pd.DataFrame(matches, columns=["ErrorCode", "Message"])
32
 
33
+ # Display extracted data
34
+ st.success("✅ Data extracted successfully!")
35
+ st.dataframe(df, use_container_width=True)
 
 
 
 
 
36
 
37
+ # Convert to Excel
38
+ output = BytesIO()
39
+ with pd.ExcelWriter(output, engine="openpyxl") as writer:
40
+ df.to_excel(writer, index=False, sheet_name="Errors")
 
 
41
 
42
+ # Download Button
43
  st.download_button(
44
+ label="⬇️ Download Excel",
45
+ data=output.getvalue(),
46
+ file_name="ErrorCodes.xlsx",
47
+ mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
48
  )