Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import pandas as pd | |
| import re | |
| from io import BytesIO | |
| import streamlit.components.v1 as components | |
| # ------------------------------------------------- | |
| # Page Setup | |
| # ------------------------------------------------- | |
| st.set_page_config(page_title="JSON Text to Excel", layout="wide") | |
| st.title("π JSON Text β Excel Converter(Jaganath's Bot)") | |
| st.markdown( | |
| "Paste JSON-like **text data** (not strict JSON). " | |
| "The app parses it line-by-line and generates Excel output. " | |
| "You can also copy the two columns to clipboard." | |
| ) | |
| # ------------------------------------------------- | |
| # Text Input (HF Safe) | |
| # ------------------------------------------------- | |
| if "json_text" not in st.session_state: | |
| st.session_state.json_text = "" | |
| text_input = st.text_area( | |
| "JSON Data (Text Format)", | |
| height=380, | |
| key="json_text", | |
| placeholder='''"LABELS": { | |
| "ICN": { | |
| "ICN_SHIFT": "Shift Management" | |
| } | |
| }''' | |
| ) | |
| # ------------------------------------------------- | |
| # Line-by-line Parser (ROBUST) | |
| # ------------------------------------------------- | |
| def parse_text_to_rows(text): | |
| rows = [] | |
| for line in text.splitlines(): | |
| line = line.strip() | |
| if not line: | |
| continue | |
| # Remove trailing commas | |
| line = line.rstrip(",") | |
| # Match "KEY": { | |
| obj_match = re.match(r'"([^"]+)"\s*:\s*\{', line) | |
| if obj_match: | |
| rows.append([obj_match.group(1), None]) | |
| continue | |
| # Match "KEY": "VALUE" | |
| kv_match = re.match(r'"([^"]+)"\s*:\s*"([^"]*)"', line) | |
| if kv_match: | |
| rows.append([kv_match.group(1), kv_match.group(2)]) | |
| continue | |
| return pd.DataFrame( | |
| rows, | |
| columns=["Object Name/Key Name", "Values"] | |
| ) | |
| # ------------------------------------------------- | |
| # Button Action | |
| # ------------------------------------------------- | |
| if st.button("Generate Excel Data", type="primary"): | |
| raw_text = st.session_state.json_text | |
| if raw_text.strip() == "": | |
| st.warning("β οΈ Please paste JSON-like text data.") | |
| else: | |
| df = parse_text_to_rows(raw_text) | |
| if df.empty: | |
| st.error("β No valid key/value data found.") | |
| else: | |
| st.subheader("π Excel Preview") | |
| st.dataframe(df, use_container_width=True) | |
| # ------------------------- | |
| # Copy to Clipboard Button (Only content) | |
| # ------------------------- | |
| csv_text = df.to_csv(index=False, sep="\t", header=False) # <-- header=False | |
| copy_button_html = f""" | |
| <button onclick="navigator.clipboard.writeText(`{csv_text}`)">π Copy Two Columns</button> | |
| """ | |
| components.html(copy_button_html, height=50) | |
| # ------------------------- | |
| # Excel Export | |
| # ------------------------- | |
| buffer = BytesIO() | |
| with pd.ExcelWriter(buffer, engine="xlsxwriter") as writer: | |
| df.to_excel(writer, index=False, sheet_name="Labels") | |
| st.download_button( | |
| "π₯ Download Excel File", | |
| buffer.getvalue(), | |
| "JSON_Text_To_Excel.xlsx", | |
| "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" | |
| ) | |