Update app.py
Browse files
app.py
CHANGED
|
@@ -2,10 +2,10 @@ import streamlit as st
|
|
| 2 |
import pandas as pd
|
| 3 |
import io, zipfile, re, html, json
|
| 4 |
|
| 5 |
-
st.set_page_config(page_title="๐ฆ ๋ฐ์ค๋ผ๋ฒจ ์๋ ์์ฑ๊ธฐ (HWPX ํ๋
|
| 6 |
-
st.title("๐ฆ ๋ฐ์ค๋ผ๋ฒจ ์๋ ์์ฑ๊ธฐ โ HWPX **ํ๋
|
| 7 |
|
| 8 |
-
#
|
| 9 |
def compute_year_range(series: pd.Series) -> str:
|
| 10 |
s = series.astype(str).fillna("")
|
| 11 |
valid = s[~s.isin(["", "0", "0000"])]
|
|
@@ -29,11 +29,12 @@ def build_merged_df(df: pd.DataFrame) -> pd.DataFrame:
|
|
| 29 |
else:
|
| 30 |
prod_df = pd.DataFrame({"๋ฐ์ค๋ฒํธ": df["๋ฐ์ค๋ฒํธ"].unique(), "์์ฐ์ฐ๋": "0000-0000"})
|
| 31 |
|
| 32 |
-
# ๋ชฉ๋ก(๊ด๋ฆฌ๋ฒํธ+์ ๋ชฉ)
|
| 33 |
has_mgmt = "๊ด๋ฆฌ๋ฒํธ" in df.columns
|
| 34 |
list_rows = []
|
| 35 |
for box, g in df.groupby("๋ฐ์ค๋ฒํธ"):
|
| 36 |
-
lines = [f"- {r['๊ด๋ฆฌ๋ฒํธ']} {r['์ ๋ชฉ']}" if has_mgmt else f"- {r['์ ๋ชฉ']}"
|
|
|
|
| 37 |
list_rows.append({"๋ฐ์ค๋ฒํธ": box, "๋ชฉ๋ก": "\r\n".join(lines)})
|
| 38 |
list_df = pd.DataFrame(list_rows)
|
| 39 |
|
|
@@ -44,9 +45,6 @@ def build_merged_df(df: pd.DataFrame) -> pd.DataFrame:
|
|
| 44 |
|
| 45 |
return meta_df.merge(list_df, on="๋ฐ์ค๋ฒํธ", how="left").merge(prod_df, on="๋ฐ์ค๋ฒํธ", how="left")
|
| 46 |
|
| 47 |
-
# ---------- HWPX ํ๋ ์นํ ----------
|
| 48 |
-
FIELD_BLOCK_RE_TMPL = r'(<hp:fieldBegin[^>]*name="{name}"[^>]*>.*?</hp:fieldBegin>)(.*?)(<hp:fieldEnd[^>]*/>)'
|
| 49 |
-
|
| 50 |
def _runs_plain(text: str) -> str:
|
| 51 |
return f"<hp:run><hp:t>{html.escape('' if text is None else str(text))}</hp:t></hp:run>"
|
| 52 |
|
|
@@ -60,17 +58,16 @@ def _runs_list(text: str) -> str:
|
|
| 60 |
parts.append(f"<hp:run><hp:t>{html.escape(ln)}</hp:t></hp:run>")
|
| 61 |
return "".join(parts)
|
| 62 |
|
| 63 |
-
|
|
|
|
| 64 |
out = io.BytesIO()
|
| 65 |
zout = zipfile.ZipFile(out, "w")
|
| 66 |
|
| 67 |
-
# 1) mimetype: ๋ฌด์์ถ + ๋งจ ์
|
| 68 |
if "mimetype" in zin.namelist():
|
| 69 |
zi = zipfile.ZipInfo("mimetype")
|
| 70 |
zi.compress_type = zipfile.ZIP_STORED
|
| 71 |
zout.writestr(zi, zin.read("mimetype"))
|
| 72 |
|
| 73 |
-
# 2) ๋๋จธ์ง ํ์ผ
|
| 74 |
for e in zin.infolist():
|
| 75 |
if e.filename == "mimetype":
|
| 76 |
continue
|
|
@@ -89,53 +86,64 @@ def _write_hwpx_like_src(zin: zipfile.ZipFile, writer_fn) -> bytes:
|
|
| 89 |
zout.close(); out.seek(0)
|
| 90 |
return out.getvalue()
|
| 91 |
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
zin = zipfile.ZipFile(io.BytesIO(hwpx_bytes), "r")
|
| 97 |
|
| 98 |
-
# ์ค์
|
| 99 |
present = set()
|
| 100 |
for e in zin.infolist():
|
| 101 |
if e.filename.startswith("Contents/") and e.filename.endswith(".xml"):
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
|
|
|
|
|
|
|
|
|
| 106 |
|
| 107 |
def writer(fname: str, xml: str) -> str:
|
| 108 |
changed = False
|
| 109 |
for k in present:
|
| 110 |
val = mapping.get(k, "")
|
| 111 |
is_list = bool(re.match(r"^(๋ชฉ๋ก|list)\d+$", k, re.IGNORECASE))
|
| 112 |
-
|
| 113 |
-
pat = re.compile(FIELD_BLOCK_RE_TMPL.format(name=re.escape(k)), re.DOTALL)
|
| 114 |
-
|
| 115 |
-
def _repl(m):
|
| 116 |
-
# ํ๋ ๋ด๋ถ ์ฑ์ฐ๊ณ , fieldEnd ๋ค์ ๊ฐ์ ํ
์คํธ ํ ๋ฒ ๋
|
| 117 |
-
vis = inner
|
| 118 |
-
if dbg: dbg["field_hits"][k] = dbg["field_hits"].get(k, 0) + 1
|
| 119 |
-
return f"{m.group(1)}{inner}{m.group(3)}{vis}"
|
| 120 |
|
| 121 |
-
|
|
|
|
| 122 |
if n:
|
| 123 |
changed = True
|
| 124 |
xml = xml2
|
|
|
|
|
|
|
| 125 |
if changed and dbg and fname not in dbg["files_touched"]:
|
| 126 |
dbg["files_touched"].append(fname)
|
| 127 |
return xml
|
| 128 |
|
| 129 |
-
out =
|
| 130 |
zin.close()
|
| 131 |
return (out, dbg) if collect_debug else (out, None)
|
| 132 |
|
| 133 |
-
#
|
| 134 |
with st.expander("์ฌ์ฉ๋ฒ", expanded=True):
|
| 135 |
st.markdown("""
|
| 136 |
-
- ํ
ํ๋ฆฟ์ **ํ๊ธ ํ๋์ปจํธ๋กค**(์: `name="๋ฐ์ค๋ฒํธ1"`)
|
| 137 |
-
-
|
| 138 |
-
-
|
| 139 |
""")
|
| 140 |
|
| 141 |
tpl_file = st.file_uploader("๐ HWPX ํ
ํ๋ฆฟ ์
๋ก๋", type=["hwpx"])
|
|
@@ -164,8 +172,8 @@ if tpl_file and data_file:
|
|
| 164 |
work = merged[merged["๋ฐ์ค๋ฒํธ"].isin(selected)] if selected else merged
|
| 165 |
rows = work.sort_values("๋ฐ์ค๋ฒํธ").to_dict(orient="records")
|
| 166 |
|
| 167 |
-
# 1ํ์ด์ง
|
| 168 |
-
st.subheader("๐งช 1ํ์ด์ง ๋งคํ ํ๋ฆฌ๋ทฐ
|
| 169 |
keys = ["๋ฐ์ค๋ฒํธ","์ข
๋ฃ์ฐ๋","๋ณด์กด๊ธฐ๊ฐ","๋จ์์
๋ฌด","๊ธฐ๋ก๋ฌผ์ฒ ","๋ชฉ๋ก"]
|
| 170 |
n = int(batch_size)
|
| 171 |
preview = {}
|
|
@@ -177,15 +185,17 @@ if tpl_file and data_file:
|
|
| 177 |
else:
|
| 178 |
for k in keys:
|
| 179 |
preview[f"{k}{i+1}"] = ""
|
| 180 |
-
st.dataframe(
|
| 181 |
-
|
|
|
|
|
|
|
| 182 |
|
| 183 |
if st.button("๐ ๋ผ๋ฒจ ์์ฑ (ํ์ด์ง๋ณ HWPX ZIP)"):
|
| 184 |
mem_zip = io.BytesIO()
|
| 185 |
zout = zipfile.ZipFile(mem_zip, "w", zipfile.ZIP_DEFLATED)
|
| 186 |
-
|
| 187 |
pages = (len(rows) + n - 1) // n
|
| 188 |
all_dbg = []
|
|
|
|
| 189 |
for p in range(pages):
|
| 190 |
chunk = rows[p*n:(p+1)*n]
|
| 191 |
mapping = {}
|
|
@@ -198,7 +208,7 @@ if tpl_file and data_file:
|
|
| 198 |
for k in keys:
|
| 199 |
mapping[f"{k}{i+1}"] = ""
|
| 200 |
|
| 201 |
-
out_hwpx, dbg =
|
| 202 |
all_dbg.append({"page": p+1, "stats": dbg})
|
| 203 |
name = "_".join([r.get("๋ฐ์ค๋ฒํธ","") for r in chunk]) if chunk else f"empty_{p+1}"
|
| 204 |
zout.writestr(f"label_{name}.hwpx", out_hwpx)
|
|
@@ -208,4 +218,4 @@ if tpl_file and data_file:
|
|
| 208 |
st.download_button("โฌ๏ธ ๋๋ฒ๊ทธ(JSON)", data=json.dumps(all_dbg, ensure_ascii=False, indent=2),
|
| 209 |
file_name="debug.json", mime="application/json")
|
| 210 |
|
| 211 |
-
st.caption("ํ๋
|
|
|
|
| 2 |
import pandas as pd
|
| 3 |
import io, zipfile, re, html, json
|
| 4 |
|
| 5 |
+
st.set_page_config(page_title="๐ฆ ๋ฐ์ค๋ผ๋ฒจ ์๋ ์์ฑ๊ธฐ (HWPX ํ๋ ํ๋ฌธํ)", layout="wide")
|
| 6 |
+
st.title("๐ฆ ๋ฐ์ค๋ผ๋ฒจ ์๋ ์์ฑ๊ธฐ โ HWPX **ํ๋ ์ ๊ฑฐ/ํ๋ฌธํ ๋ฐฉ์**")
|
| 7 |
|
| 8 |
+
# ================= ๊ณตํต ์ ํธ =================
|
| 9 |
def compute_year_range(series: pd.Series) -> str:
|
| 10 |
s = series.astype(str).fillna("")
|
| 11 |
valid = s[~s.isin(["", "0", "0000"])]
|
|
|
|
| 29 |
else:
|
| 30 |
prod_df = pd.DataFrame({"๋ฐ์ค๋ฒํธ": df["๋ฐ์ค๋ฒํธ"].unique(), "์์ฐ์ฐ๋": "0000-0000"})
|
| 31 |
|
| 32 |
+
# ๋ชฉ๋ก(๊ด๋ฆฌ๋ฒํธ + ์ ๋ชฉ)
|
| 33 |
has_mgmt = "๊ด๋ฆฌ๋ฒํธ" in df.columns
|
| 34 |
list_rows = []
|
| 35 |
for box, g in df.groupby("๋ฐ์ค๋ฒํธ"):
|
| 36 |
+
lines = [f"- {r['๊ด๋ฆฌ๋ฒํธ']} {r['์ ๋ชฉ']}" if has_mgmt else f"- {r['์ ๋ชฉ']}"
|
| 37 |
+
for _, r in g.iterrows()]
|
| 38 |
list_rows.append({"๋ฐ์ค๋ฒํธ": box, "๋ชฉ๋ก": "\r\n".join(lines)})
|
| 39 |
list_df = pd.DataFrame(list_rows)
|
| 40 |
|
|
|
|
| 45 |
|
| 46 |
return meta_df.merge(list_df, on="๋ฐ์ค๋ฒํธ", how="left").merge(prod_df, on="๋ฐ์ค๋ฒํธ", how="left")
|
| 47 |
|
|
|
|
|
|
|
|
|
|
| 48 |
def _runs_plain(text: str) -> str:
|
| 49 |
return f"<hp:run><hp:t>{html.escape('' if text is None else str(text))}</hp:t></hp:run>"
|
| 50 |
|
|
|
|
| 58 |
parts.append(f"<hp:run><hp:t>{html.escape(ln)}</hp:t></hp:run>")
|
| 59 |
return "".join(parts)
|
| 60 |
|
| 61 |
+
# =============== HWPX ์ฐ๊ธฐ (mimetype ๋งจ์/๋ฌด์์ถ) ===============
|
| 62 |
+
def write_hwpx_like_src(zin: zipfile.ZipFile, writer_fn) -> bytes:
|
| 63 |
out = io.BytesIO()
|
| 64 |
zout = zipfile.ZipFile(out, "w")
|
| 65 |
|
|
|
|
| 66 |
if "mimetype" in zin.namelist():
|
| 67 |
zi = zipfile.ZipInfo("mimetype")
|
| 68 |
zi.compress_type = zipfile.ZIP_STORED
|
| 69 |
zout.writestr(zi, zin.read("mimetype"))
|
| 70 |
|
|
|
|
| 71 |
for e in zin.infolist():
|
| 72 |
if e.filename == "mimetype":
|
| 73 |
continue
|
|
|
|
| 86 |
zout.close(); out.seek(0)
|
| 87 |
return out.getvalue()
|
| 88 |
|
| 89 |
+
# =============== ํ๋ ํ๋ฌธํ(์ ๊ฑฐ) ์นํ ===============
|
| 90 |
+
# ํ๊ธ์ ํ๋๊ฐ ๋ณดํต ์ด๋ ๊ฒ ๋ค์ด๊ฐ๋๋ค:
|
| 91 |
+
# <hp:run> ... <hp:fieldBegin name="ํค" .../> ... </hp:run>
|
| 92 |
+
# (์ค๊ฐ์ ์ฌ๋ฌ run/ํ
์คํธ)
|
| 93 |
+
# <hp:run> ... <hp:fieldEnd/> ... </hp:run>
|
| 94 |
+
# => ์๋ ์ ๊ท์์ผ๋ก "fieldBegin run ~ fieldEnd run" ์ ์ฒด๋ฅผ ๊ฐ run๋ค๋ก ๋์ฒดํฉ๋๋ค.
|
| 95 |
+
FIELD_RANGE_RE_TMPL = (
|
| 96 |
+
r'(<hp:run[^>]*>[^<]*'
|
| 97 |
+
r'<hp:fieldBegin[^>]*name="{name}"[^>]*/>'
|
| 98 |
+
r'.*?</hp:run>)'
|
| 99 |
+
r'(.*?)'
|
| 100 |
+
r'(<hp:run[^>]*>.*?<hp:fieldEnd[^>]*/>.*?</hp:run>)'
|
| 101 |
+
)
|
| 102 |
+
|
| 103 |
+
def apply_field_flatten(hwpx_bytes: bytes, mapping: dict, collect_debug=False):
|
| 104 |
+
dbg = {"mode":"field-flatten","files_touched":[], "field_hits":{}} if collect_debug else None
|
| 105 |
zin = zipfile.ZipFile(io.BytesIO(hwpx_bytes), "r")
|
| 106 |
|
| 107 |
+
# ์ค์ ์กด์ฌํ๋ name๋ง ์ถ์ถ
|
| 108 |
present = set()
|
| 109 |
for e in zin.infolist():
|
| 110 |
if e.filename.startswith("Contents/") and e.filename.endswith(".xml"):
|
| 111 |
+
try:
|
| 112 |
+
s = zin.read(e.filename).decode("utf-8", errors="ignore")
|
| 113 |
+
for k in mapping.keys():
|
| 114 |
+
if f'name="{k}"' in s:
|
| 115 |
+
present.add(k)
|
| 116 |
+
except:
|
| 117 |
+
pass
|
| 118 |
|
| 119 |
def writer(fname: str, xml: str) -> str:
|
| 120 |
changed = False
|
| 121 |
for k in present:
|
| 122 |
val = mapping.get(k, "")
|
| 123 |
is_list = bool(re.match(r"^(๋ชฉ๋ก|list)\d+$", k, re.IGNORECASE))
|
| 124 |
+
replacement_runs = _runs_list(val) if is_list else _runs_plain(val)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
|
| 126 |
+
pat = re.compile(FIELD_RANGE_RE_TMPL.format(name=re.escape(k)), re.DOTALL)
|
| 127 |
+
xml2, n = pat.subn(replacement_runs, xml)
|
| 128 |
if n:
|
| 129 |
changed = True
|
| 130 |
xml = xml2
|
| 131 |
+
if dbg: dbg["field_hits"][k] = dbg["field_hits"].get(k, 0) + 1
|
| 132 |
+
|
| 133 |
if changed and dbg and fname not in dbg["files_touched"]:
|
| 134 |
dbg["files_touched"].append(fname)
|
| 135 |
return xml
|
| 136 |
|
| 137 |
+
out = write_hwpx_like_src(zin, writer)
|
| 138 |
zin.close()
|
| 139 |
return (out, dbg) if collect_debug else (out, None)
|
| 140 |
|
| 141 |
+
# ================= UI =================
|
| 142 |
with st.expander("์ฌ์ฉ๋ฒ", expanded=True):
|
| 143 |
st.markdown("""
|
| 144 |
+
- ํ
ํ๋ฆฟ์ **ํ๊ธ ํ๋์ปจํธ๋กค**์ด์ด์ผ ํฉ๋๋ค. (์: `name="๋ฐ์ค๋ฒํธ1"`)
|
| 145 |
+
- ์ด ์ฑ์ ํ๋ ๊ตฌ๊ฐ์ **ํ๋ฌธํ(ํ๋ ์ ๊ฑฐ)** ํ์ฌ ๊ฐ run๋ค๋ก ๋ฐ๊ฟ๋๋ค. โ ํ๊ธ ๋ทฐ์ด์์ **ํญ์ ๋ณด์**.
|
| 146 |
+
- ๋ผ๋ฒจ ํ ํ์ด์ง์ N๊ฐ๋ฉด, ํ๋๋ช
์ `๋ฐ์ค๋ฒํธ1..N`, `์ข
๋ฃ์ฐ๋1..N`, `๋ณด์กด๊ธฐ๊ฐ1..N`, `๋จ์์
๋ฌด1..N`, `๊ธฐ๋ก๋ฌผ์ฒ 1..N`, `๋ชฉ๋ก1..N`.
|
| 147 |
""")
|
| 148 |
|
| 149 |
tpl_file = st.file_uploader("๐ HWPX ํ
ํ๋ฆฟ ์
๋ก๋", type=["hwpx"])
|
|
|
|
| 172 |
work = merged[merged["๋ฐ์ค๋ฒํธ"].isin(selected)] if selected else merged
|
| 173 |
rows = work.sort_values("๋ฐ์ค๋ฒํธ").to_dict(orient="records")
|
| 174 |
|
| 175 |
+
# 1ํ์ด์ง ํ๋ฆฌ๋ทฐ
|
| 176 |
+
st.subheader("๐งช 1ํ์ด์ง ๋งคํ ํ๋ฆฌ๋ทฐ")
|
| 177 |
keys = ["๋ฐ์ค๋ฒํธ","์ข
๋ฃ์ฐ๋","๋ณด์กด๊ธฐ๊ฐ","๋จ์์
๋ฌด","๊ธฐ๋ก๋ฌผ์ฒ ","๋ชฉ๋ก"]
|
| 178 |
n = int(batch_size)
|
| 179 |
preview = {}
|
|
|
|
| 185 |
else:
|
| 186 |
for k in keys:
|
| 187 |
preview[f"{k}{i+1}"] = ""
|
| 188 |
+
st.dataframe(
|
| 189 |
+
pd.DataFrame([{"ํ๋๋ช
":k, "๊ฐ ์๋ถ๋ถ":str(v)[:120]} for k,v in sorted(preview.items())]),
|
| 190 |
+
use_container_width=True, height=320
|
| 191 |
+
)
|
| 192 |
|
| 193 |
if st.button("๐ ๋ผ๋ฒจ ์์ฑ (ํ์ด์ง๋ณ HWPX ZIP)"):
|
| 194 |
mem_zip = io.BytesIO()
|
| 195 |
zout = zipfile.ZipFile(mem_zip, "w", zipfile.ZIP_DEFLATED)
|
|
|
|
| 196 |
pages = (len(rows) + n - 1) // n
|
| 197 |
all_dbg = []
|
| 198 |
+
|
| 199 |
for p in range(pages):
|
| 200 |
chunk = rows[p*n:(p+1)*n]
|
| 201 |
mapping = {}
|
|
|
|
| 208 |
for k in keys:
|
| 209 |
mapping[f"{k}{i+1}"] = ""
|
| 210 |
|
| 211 |
+
out_hwpx, dbg = apply_field_flatten(tpl_bytes, mapping, collect_debug=True)
|
| 212 |
all_dbg.append({"page": p+1, "stats": dbg})
|
| 213 |
name = "_".join([r.get("๋ฐ์ค๋ฒํธ","") for r in chunk]) if chunk else f"empty_{p+1}"
|
| 214 |
zout.writestr(f"label_{name}.hwpx", out_hwpx)
|
|
|
|
| 218 |
st.download_button("โฌ๏ธ ๋๋ฒ๊ทธ(JSON)", data=json.dumps(all_dbg, ensure_ascii=False, indent=2),
|
| 219 |
file_name="debug.json", mime="application/json")
|
| 220 |
|
| 221 |
+
st.caption("ํ๋ ๊ตฌ๊ฐ์ ํต์งธ๋ก ๊ฐ run๋ค๋ก ๊ต์ฒดํฉ๋๋ค. (ํ๋ ์ ๊ฑฐ โ ๊ฐ์ด ํ์คํ ๋ณด์
๋๋ค)")
|