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="๐ฆ ๋ฐ์ค๋ผ๋ฒจ ์๋ ์์ฑ๊ธฐ (ํ๋
|
| 6 |
-
st.title("๐ฆ ๋ฐ์ค๋ผ๋ฒจ ์๋ ์์ฑ๊ธฐ
|
| 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,14 +29,11 @@ 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 |
-
if has_mgmt
|
| 37 |
-
lines = [f"- {r['๊ด๋ฆฌ๋ฒํธ']} {r['์ ๋ชฉ']}" for _, r in g.iterrows()]
|
| 38 |
-
else:
|
| 39 |
-
lines = [f"- {r['์ ๋ชฉ']}" for _, r in g.iterrows()]
|
| 40 |
list_rows.append({"๋ฐ์ค๋ฒํธ": box, "๋ชฉ๋ก": "\r\n".join(lines)})
|
| 41 |
list_df = pd.DataFrame(list_rows)
|
| 42 |
|
|
@@ -47,7 +44,13 @@ def build_merged_df(df: pd.DataFrame) -> pd.DataFrame:
|
|
| 47 |
|
| 48 |
return meta_df.merge(list_df, on="๋ฐ์ค๋ฒํธ", how="left").merge(prod_df, on="๋ฐ์ค๋ฒํธ", how="left")
|
| 49 |
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
if text is None: return ""
|
| 52 |
lines = str(text).replace("\r\n", "\n").split("\n")
|
| 53 |
parts = []
|
|
@@ -57,17 +60,12 @@ def _build_list_runs(text: str) -> str:
|
|
| 57 |
parts.append(f"<hp:run><hp:t>{html.escape(ln)}</hp:t></hp:run>")
|
| 58 |
return "".join(parts)
|
| 59 |
|
| 60 |
-
def
|
| 61 |
-
return f"<hp:run><hp:t>{html.escape('' if text is None else str(text))}</hp:t></hp:run>"
|
| 62 |
-
|
| 63 |
-
# =============== HWPX ์ฐ๊ธฐ ๊ณตํต (mimetype ๋ฌด์์ถ/๋งจ์) ===============
|
| 64 |
-
def write_hwpx_like_src(zin: zipfile.ZipFile, writer_fn) -> bytes:
|
| 65 |
out = io.BytesIO()
|
| 66 |
zout = zipfile.ZipFile(out, "w")
|
| 67 |
-
names = zin.namelist()
|
| 68 |
|
| 69 |
-
# 1) mimetype
|
| 70 |
-
if "mimetype" in
|
| 71 |
zi = zipfile.ZipInfo("mimetype")
|
| 72 |
zi.compress_type = zipfile.ZIP_STORED
|
| 73 |
zout.writestr(zi, zin.read("mimetype"))
|
|
@@ -91,129 +89,63 @@ def write_hwpx_like_src(zin: zipfile.ZipFile, writer_fn) -> bytes:
|
|
| 91 |
zout.close(); out.seek(0)
|
| 92 |
return out.getvalue()
|
| 93 |
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
def token_mode_apply(hwpx_bytes: bytes, mapping: dict, collect_debug=False):
|
| 98 |
-
dbg = {"mode":"token","files_touched":[], "token_hits":{}} if collect_debug else None
|
| 99 |
-
zin = zipfile.ZipFile(io.BytesIO(hwpx_bytes), "r")
|
| 100 |
-
|
| 101 |
-
token_keys = list(mapping.keys())
|
| 102 |
-
|
| 103 |
-
def writer_fn(fname: str, xml: str) -> str:
|
| 104 |
-
changed = False
|
| 105 |
-
# run ๊ฒฝ๊ณ ๋ณํฉ (ํ ํฐ์ด ์๋ ค ์์ด๋ ์ด์ด๋ถ์ด๊ธฐ)
|
| 106 |
-
xml2 = RUN_JOIN_RE.sub('', xml)
|
| 107 |
-
if xml2 != xml:
|
| 108 |
-
changed = True
|
| 109 |
-
xml = xml2
|
| 110 |
-
# ํ ํฐ ๋ฌธ์์ด ์นํ
|
| 111 |
-
for k in token_keys:
|
| 112 |
-
tok = f"{{{{{k}}}}}"
|
| 113 |
-
if tok in xml:
|
| 114 |
-
val = mapping[k]
|
| 115 |
-
if re.match(r"^(๋ชฉ๋ก|list)\d+$", k, re.IGNORECASE):
|
| 116 |
-
# ํ ํฐ์ run ์์ ๋ค์ด๊ฐ ์์ผ๋ฏ๋ก, run ๊ตฌ์กฐ๋ฅผ ํต์งธ๋ก ์์ฑ
|
| 117 |
-
xml = xml.replace(tok, _build_list_runs(val))
|
| 118 |
-
else:
|
| 119 |
-
xml = xml.replace(tok, html.escape("" if val is None else str(val)))
|
| 120 |
-
changed = True
|
| 121 |
-
if dbg: dbg["token_hits"][k] = dbg["token_hits"].get(k, 0) + 1
|
| 122 |
-
if changed and dbg and fname not in dbg["files_touched"]:
|
| 123 |
-
dbg["files_touched"].append(fname)
|
| 124 |
-
return xml
|
| 125 |
-
|
| 126 |
-
out = write_hwpx_like_src(zin, writer_fn)
|
| 127 |
-
zin.close()
|
| 128 |
-
return (out, dbg) if collect_debug else (out, None)
|
| 129 |
-
|
| 130 |
-
# =============== ๋ชจ๋2: ํ๋์ปจํธ๋กค ์นํ (๊ฐ์ ํ
์คํธ ์ค๋ณต ์ฝ์
) ===============
|
| 131 |
-
# <hp:fieldBegin ... name="ํค"> ... </hp:fieldBegin> [๋ณธ๋ฌธ] <hp:fieldEnd ... />
|
| 132 |
-
FIELD_BLOCK_RE_TMPL = r'(<hp:fieldBegin[^>]*name="{name}"[^>]*>.*?</hp:fieldBegin>)(.*?)(<hp:fieldEnd[^>]*/>)'
|
| 133 |
-
|
| 134 |
-
def field_mode_apply(hwpx_bytes: bytes, mapping: dict, collect_debug=False):
|
| 135 |
dbg = {"mode":"field","files_touched":[], "field_hits":{}} if collect_debug else None
|
| 136 |
zin = zipfile.ZipFile(io.BytesIO(hwpx_bytes), "r")
|
| 137 |
|
| 138 |
-
#
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
s = zin.read(fn).decode("utf-8", errors="ignore")
|
| 144 |
for k in mapping.keys():
|
| 145 |
if f'name="{k}"' in s:
|
| 146 |
-
|
| 147 |
-
except:
|
| 148 |
-
pass
|
| 149 |
|
| 150 |
-
def
|
| 151 |
-
|
| 152 |
-
for k in
|
| 153 |
val = mapping.get(k, "")
|
| 154 |
is_list = bool(re.match(r"^(๋ชฉ๋ก|list)\d+$", k, re.IGNORECASE))
|
| 155 |
-
|
|
|
|
| 156 |
|
| 157 |
def _repl(m):
|
| 158 |
-
# ํ๋ ๋ด๋ถ
|
| 159 |
-
|
| 160 |
-
# ํ๋ ๋ค์ ๊ฐ์ ํ
์คํธ๋ฅผ 'ํ ๋ฒ ๋' ๋ฃ์ด ํญ์ ๋ณด์ด๊ฒ
|
| 161 |
-
visible_dup = inner
|
| 162 |
if dbg: dbg["field_hits"][k] = dbg["field_hits"].get(k, 0) + 1
|
| 163 |
-
return f
|
| 164 |
|
| 165 |
-
|
| 166 |
if n:
|
| 167 |
-
|
| 168 |
-
xml =
|
| 169 |
-
|
| 170 |
-
if any_change and dbg and fname not in dbg["files_touched"]:
|
| 171 |
dbg["files_touched"].append(fname)
|
| 172 |
return xml
|
| 173 |
|
| 174 |
-
out =
|
| 175 |
zin.close()
|
| 176 |
return (out, dbg) if collect_debug else (out, None)
|
| 177 |
|
| 178 |
-
#
|
| 179 |
-
|
| 180 |
-
zin = zipfile.ZipFile(io.BytesIO(hwpx_bytes), "r")
|
| 181 |
-
has_token = False
|
| 182 |
-
has_field = False
|
| 183 |
-
for e in zin.infolist():
|
| 184 |
-
if not (e.filename.startswith("Contents/") and e.filename.endswith(".xml")):
|
| 185 |
-
continue
|
| 186 |
-
try:
|
| 187 |
-
s = zin.read(e.filename).decode("utf-8", errors="ignore")
|
| 188 |
-
if "{{" in s and "}}" in s:
|
| 189 |
-
has_token = True
|
| 190 |
-
if "<hp:fieldBegin" in s and 'name="' in s:
|
| 191 |
-
has_field = True
|
| 192 |
-
except:
|
| 193 |
-
pass
|
| 194 |
-
zin.close()
|
| 195 |
-
if has_token: return "token"
|
| 196 |
-
if has_field: return "field"
|
| 197 |
-
return "unknown"
|
| 198 |
-
|
| 199 |
-
# =============== Streamlit UI ===============
|
| 200 |
-
with st.expander("์ฌ์ฉ ๋ฐฉ๋ฒ ์์ฝ", expanded=True):
|
| 201 |
st.markdown("""
|
| 202 |
-
- ํ
ํ๋ฆฟ
|
| 203 |
-
|
| 204 |
-
-
|
| 205 |
""")
|
| 206 |
|
| 207 |
-
|
| 208 |
batch_size = st.number_input("ํ
ํ๋ฆฟ์ ๋ผ๋ฒจ ์ธํธ ๊ฐ์ (ํ ํ์ด์ง N๊ฐ)", min_value=1, max_value=12, value=3, step=1)
|
| 209 |
-
|
| 210 |
|
| 211 |
-
if
|
| 212 |
-
tpl_bytes =
|
| 213 |
-
|
| 214 |
-
st.info(f"ํ์ง๋ ํ
ํ๋ฆฟ ๋ชจ๋: **{mode}**")
|
| 215 |
|
| 216 |
-
df = pd.read_csv(data) if data.name.lower().endswith(".csv") else pd.read_excel(data)
|
| 217 |
if "๋ฐ์ค๋ฒํธ" not in df.columns:
|
| 218 |
st.error("โ ํ์ ์ปฌ๋ผ '๋ฐ์ค๋ฒํธ'๊ฐ ์์ต๋๋ค.")
|
| 219 |
st.stop()
|
|
@@ -232,11 +164,11 @@ if tpl and data:
|
|
| 232 |
work = merged[merged["๋ฐ์ค๋ฒํธ"].isin(selected)] if selected else merged
|
| 233 |
rows = work.sort_values("๋ฐ์ค๋ฒํธ").to_dict(orient="records")
|
| 234 |
|
| 235 |
-
# 1ํ์ด์ง
|
| 236 |
-
st.subheader("๐งช 1ํ์ด์ง ๋งคํ ํ๋ฆฌ๋ทฐ")
|
| 237 |
keys = ["๋ฐ์ค๋ฒํธ","์ข
๋ฃ์ฐ๋","๋ณด์กด๊ธฐ๊ฐ","๋จ์์
๋ฌด","๊ธฐ๋ก๋ฌผ์ฒ ","๋ชฉ๋ก"]
|
| 238 |
-
preview = {}
|
| 239 |
n = int(batch_size)
|
|
|
|
| 240 |
for i in range(n):
|
| 241 |
if i < len(rows):
|
| 242 |
r = rows[i]
|
|
@@ -245,17 +177,15 @@ if tpl and data:
|
|
| 245 |
else:
|
| 246 |
for k in keys:
|
| 247 |
preview[f"{k}{i+1}"] = ""
|
| 248 |
-
st.dataframe(
|
| 249 |
-
|
| 250 |
-
use_container_width=True, height=320
|
| 251 |
-
)
|
| 252 |
|
| 253 |
if st.button("๐ ๋ผ๋ฒจ ์์ฑ (ํ์ด์ง๋ณ HWPX ZIP)"):
|
| 254 |
mem_zip = io.BytesIO()
|
| 255 |
zout = zipfile.ZipFile(mem_zip, "w", zipfile.ZIP_DEFLATED)
|
|
|
|
| 256 |
pages = (len(rows) + n - 1) // n
|
| 257 |
all_dbg = []
|
| 258 |
-
|
| 259 |
for p in range(pages):
|
| 260 |
chunk = rows[p*n:(p+1)*n]
|
| 261 |
mapping = {}
|
|
@@ -268,24 +198,14 @@ if tpl and data:
|
|
| 268 |
for k in keys:
|
| 269 |
mapping[f"{k}{i+1}"] = ""
|
| 270 |
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
else:
|
| 276 |
-
# ์์ ๋นต: ๋ ๋ค ์๋ (token -> field)
|
| 277 |
-
out, dbg = token_mode_apply(tpl_bytes, mapping, collect_debug=True)
|
| 278 |
-
if dbg and not dbg["files_touched"]:
|
| 279 |
-
out, dbg = field_mode_apply(tpl_bytes, mapping, collect_debug=True)
|
| 280 |
-
|
| 281 |
-
all_dbg.append({"page": p+1, "mode": dbg.get("mode") if dbg else mode, "stats": dbg})
|
| 282 |
-
page_boxes = [r.get("๋ฐ์ค๋ฒํธ","") for r in chunk]
|
| 283 |
-
name = "_".join(page_boxes) if page_boxes else f"empty_{p+1}"
|
| 284 |
-
zout.writestr(f"label_{name}.hwpx", out)
|
| 285 |
|
| 286 |
zout.close(); mem_zip.seek(0)
|
| 287 |
st.download_button("โฌ๏ธ ZIP ๋ค์ด๋ก๋", data=mem_zip, file_name="labels_by_page.zip", mime="application/zip")
|
| 288 |
st.download_button("โฌ๏ธ ๋๋ฒ๊ทธ(JSON)", data=json.dumps(all_dbg, ensure_ascii=False, indent=2),
|
| 289 |
file_name="debug.json", mime="application/json")
|
| 290 |
|
| 291 |
-
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['์ ๋ชฉ']}" for _, r in g.iterrows()]
|
|
|
|
|
|
|
|
|
|
| 37 |
list_rows.append({"๋ฐ์ค๋ฒํธ": box, "๋ชฉ๋ก": "\r\n".join(lines)})
|
| 38 |
list_df = pd.DataFrame(list_rows)
|
| 39 |
|
|
|
|
| 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 |
+
|
| 53 |
+
def _runs_list(text: str) -> str:
|
| 54 |
if text is None: return ""
|
| 55 |
lines = str(text).replace("\r\n", "\n").split("\n")
|
| 56 |
parts = []
|
|
|
|
| 60 |
parts.append(f"<hp:run><hp:t>{html.escape(ln)}</hp:t></hp:run>")
|
| 61 |
return "".join(parts)
|
| 62 |
|
| 63 |
+
def _write_hwpx_like_src(zin: zipfile.ZipFile, writer_fn) -> bytes:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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"))
|
|
|
|
| 89 |
zout.close(); out.seek(0)
|
| 90 |
return out.getvalue()
|
| 91 |
|
| 92 |
+
def apply_field_mode(hwpx_bytes: bytes, mapping: dict, collect_debug=False):
|
| 93 |
+
""" ํ๊ธ ํ๋์ปจํธ๋กค(name=...)์ ๊ฐ์ผ๋ก ์ฑ์ฐ๊ณ ,
|
| 94 |
+
fieldEnd ๋ค์ ๋์ผ ๋ด์ฉ์ '๊ฐ์ ํ
์คํธ'๋ก ํ ๋ฒ ๋ ์ฝ์
ํ์ฌ ํญ์ ๋ณด์ด๊ฒ ํ๋ค. """
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
dbg = {"mode":"field","files_touched":[], "field_hits":{}} if collect_debug else None
|
| 96 |
zin = zipfile.ZipFile(io.BytesIO(hwpx_bytes), "r")
|
| 97 |
|
| 98 |
+
# ์ค์ ๋ก ์กด์ฌํ๋ name๋ง ๊ณจ๋ผ๋ด๊ธฐ(์๋โ)
|
| 99 |
+
present = set()
|
| 100 |
+
for e in zin.infolist():
|
| 101 |
+
if e.filename.startswith("Contents/") and e.filename.endswith(".xml"):
|
| 102 |
+
s = zin.read(e.filename).decode("utf-8", errors="ignore")
|
|
|
|
| 103 |
for k in mapping.keys():
|
| 104 |
if f'name="{k}"' in s:
|
| 105 |
+
present.add(k)
|
|
|
|
|
|
|
| 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 |
+
inner = _runs_list(val) if is_list else _runs_plain(val)
|
| 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 |
+
xml2, n = pat.subn(_repl, xml)
|
| 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 = _write_hwpx_like_src(zin, writer)
|
| 130 |
zin.close()
|
| 131 |
return (out, dbg) if collect_debug else (out, None)
|
| 132 |
|
| 133 |
+
# ---------- UI ----------
|
| 134 |
+
with st.expander("์ฌ์ฉ๋ฒ", expanded=True):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
st.markdown("""
|
| 136 |
+
- ํ
ํ๋ฆฟ์ **ํ๊ธ ํ๋์ปจํธ๋กค**(์: `name="๋ฐ์ค๋ฒํธ1"`)์ด์ด์ผ ํฉ๋๋ค. (ํ์ฌ ์ฑ์ ํ๋ ๋ชจ๋ ์ ์ฉ)
|
| 137 |
+
- ํ ํ์ด์ง์ ๋ผ๋ฒจ N๊ฐ๋ผ๋ฉด, ํ๋ ์ด๋ฆ์ `๋ฐ์ค๋ฒํธ1..N`, `์ข
๋ฃ์ฐ๋1..N`, `๋ณด์กด๊ธฐ๊ฐ1..N`, `๋จ์์
๋ฌด1..N`, `๊ธฐ๋ก๋ฌผ์ฒ 1..N`, `๋ชฉ๋ก1..N` ํํ์ฌ์ผ ํฉ๋๋ค.
|
| 138 |
+
- `์ข
๋ฃ์ฐ๋` ๊ฐ์ ๋ฐ์ค๋ณ ์ต์~์ต๋๋ก ๋ฌถ์ด **์์ฐ์ฐ๋(๋ฒ์)** ๋ก ์
๋ ฅ๋ฉ๋๋ค.
|
| 139 |
""")
|
| 140 |
|
| 141 |
+
tpl_file = st.file_uploader("๐ HWPX ํ
ํ๋ฆฟ ์
๋ก๋", type=["hwpx"])
|
| 142 |
batch_size = st.number_input("ํ
ํ๋ฆฟ์ ๋ผ๋ฒจ ์ธํธ ๊ฐ์ (ํ ํ์ด์ง N๊ฐ)", min_value=1, max_value=12, value=3, step=1)
|
| 143 |
+
data_file = st.file_uploader("๐ ๋ฐ์ดํฐ ์
๋ก๋ (Excel/CSV)", type=["xlsx","xls","csv"])
|
| 144 |
|
| 145 |
+
if tpl_file and data_file:
|
| 146 |
+
tpl_bytes = tpl_file.read()
|
| 147 |
+
df = pd.read_csv(data_file) if data_file.name.lower().endswith(".csv") else pd.read_excel(data_file)
|
|
|
|
| 148 |
|
|
|
|
| 149 |
if "๋ฐ์ค๋ฒํธ" not in df.columns:
|
| 150 |
st.error("โ ํ์ ์ปฌ๋ผ '๋ฐ์ค๋ฒํธ'๊ฐ ์์ต๋๋ค.")
|
| 151 |
st.stop()
|
|
|
|
| 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 = {}
|
| 172 |
for i in range(n):
|
| 173 |
if i < len(rows):
|
| 174 |
r = rows[i]
|
|
|
|
| 177 |
else:
|
| 178 |
for k in keys:
|
| 179 |
preview[f"{k}{i+1}"] = ""
|
| 180 |
+
st.dataframe(pd.DataFrame([{"ํ๋๋ช
":k, "๊ฐ ์๋ถ๋ถ":str(v)[:120]} for k,v in sorted(preview.items())]),
|
| 181 |
+
use_container_width=True, height=320)
|
|
|
|
|
|
|
| 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 |
for k in keys:
|
| 199 |
mapping[f"{k}{i+1}"] = ""
|
| 200 |
|
| 201 |
+
out_hwpx, dbg = apply_field_mode(tpl_bytes, mapping, collect_debug=True)
|
| 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)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 205 |
|
| 206 |
zout.close(); mem_zip.seek(0)
|
| 207 |
st.download_button("โฌ๏ธ ZIP ๋ค์ด๋ก๋", data=mem_zip, file_name="labels_by_page.zip", mime="application/zip")
|
| 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("ํ๋ ๋ด๋ถ์ ๊ฐ + fieldEnd ๋ค์ ๊ฐ์ ํ
์คํธ๋ฅผ **์ค๋ณต ์ฝ์
**ํ๋ฏ๋ก, ํ๊ธ์์ ๊ฐ์ด ํญ์ ๋ณด์
๋๋ค.")
|