Spaces:
Sleeping
Sleeping
Download comment_lib/export/excel.py from neosimHan/reels-bench: direct link, hf CLI and curl.
- Browser
- Download file 10.2 kB
-
https://huggingface.co/spaces/neosimHan/reels-bench/resolve/main/comment_lib/export/excel.py
- Command line
-
hf download hf://spaces/neosimHan/reels-bench/comment_lib/export/excel.py
-
curl -L -o excel.py https://huggingface.co/spaces/neosimHan/reels-bench/resolve/main/comment_lib/export/excel.py
10.2 kB
| """Excel 5์ํธ export โ openpyxl ์ฌ์ฉ.""" | |
| import sys | |
| import os | |
| # (sys.path.insert removed by sync_comment_lib.py) | |
| from datetime import datetime | |
| from comment_lib.schema import Comment, KeywordHit, InsightCluster, SentimentItem | |
| try: | |
| import openpyxl | |
| from openpyxl import Workbook | |
| from openpyxl.styles import Font, PatternFill, Alignment | |
| from openpyxl.utils import get_column_letter | |
| from openpyxl.worksheet.hyperlink import Hyperlink | |
| except ImportError: | |
| openpyxl = None | |
| # ํค๋ ์คํ์ผ ์ ์ | |
| _HEADER_FONT = None | |
| _HEADER_FILL_BLUE = None | |
| _HEADER_FILL_GREEN = None | |
| _HEADER_FILL_ORANGE = None | |
| _HEADER_FILL_PURPLE = None | |
| _HEADER_FILL_RED = None | |
| if openpyxl: | |
| _HEADER_FONT = Font(bold=True, color="FFFFFF") | |
| _HEADER_FILL_BLUE = PatternFill("solid", fgColor="2563EB") | |
| _HEADER_FILL_GREEN = PatternFill("solid", fgColor="16A34A") | |
| _HEADER_FILL_ORANGE = PatternFill("solid", fgColor="EA580C") | |
| _HEADER_FILL_PURPLE = PatternFill("solid", fgColor="7C3AED") | |
| _HEADER_FILL_RED = PatternFill("solid", fgColor="DC2626") | |
| def _auto_width(ws, min_width: int = 10, max_width: int = 60) -> None: | |
| """์ด ๋๋น๋ฅผ ๋ด์ฉ ๊ธธ์ด์ ๋ง๊ฒ ์๋ ์กฐ์ .""" | |
| for col in ws.columns: | |
| max_len = 0 | |
| col_letter = get_column_letter(col[0].column) | |
| for cell in col: | |
| if cell.value: | |
| cell_len = len(str(cell.value)) | |
| if cell_len > max_len: | |
| max_len = cell_len | |
| adjusted = min(max(max_len + 2, min_width), max_width) | |
| ws.column_dimensions[col_letter].width = adjusted | |
| def _set_header_row(ws, headers: list[str], fill) -> None: | |
| """์ฒซ ํ์ ํค๋๋ฅผ ๊ธฐ๋กํ๊ณ ์คํ์ผ ์ ์ฉ.""" | |
| ws.append(headers) | |
| for cell in ws[1]: | |
| cell.font = _HEADER_FONT | |
| cell.fill = fill | |
| cell.alignment = Alignment(horizontal="center", vertical="center", wrap_text=True) | |
| def _build_cover(ws, total: int, source: str, sheet_names: list[str], summary=None) -> None: | |
| """ํ์ง ์ํธ ์์ฑ โ summary ์์ผ๋ฉด TL;DR ๋ฐ์ค ๋ ธ์ถ.""" | |
| ws.title = "ํ์ง" | |
| ws.column_dimensions["A"].width = 22 | |
| ws.column_dimensions["B"].width = 60 | |
| title_font = Font(bold=True, size=16) | |
| label_font = Font(bold=True, size=11) | |
| summary_font = Font(bold=True, size=13, color="2563EB") | |
| ws["A1"] = "๋๊ธ ๋ถ์ ๋ฆฌํฌํธ" | |
| ws["A1"].font = title_font | |
| ws.merge_cells("A1:B1") | |
| ws["A1"].alignment = Alignment(horizontal="center") | |
| ws["A3"] = "๋ถ์ ์ผ์" | |
| ws["A3"].font = label_font | |
| ws["B3"] = datetime.now().strftime("%Y-%m-%d %H:%M") | |
| ws["A4"] = "์ด ๋๊ธ ์" | |
| ws["A4"].font = label_font | |
| ws["B4"] = total | |
| ws["A5"] = "๋ฐ์ดํฐ ์์ค" | |
| ws["A5"].font = label_font | |
| ws["B5"] = source | |
| cur_row = 7 | |
| # TL;DR โ summary ์์ผ๋ฉด ํ์ | |
| if summary: | |
| ws.cell(row=cur_row, column=1, value="๐ ํต์ฌ ํต์ฐฐ").font = summary_font | |
| ws.merge_cells(start_row=cur_row, start_column=1, end_row=cur_row, end_column=2) | |
| cur_row += 1 | |
| if summary.headline: | |
| ws.cell(row=cur_row, column=1, value="Headline").font = label_font | |
| c = ws.cell(row=cur_row, column=2, value=summary.headline) | |
| c.alignment = Alignment(wrap_text=True, vertical="top") | |
| cur_row += 1 | |
| ws.cell(row=cur_row, column=1, value="๊ธ์ ").font = label_font | |
| ws.cell(row=cur_row, column=2, | |
| value=f"{summary.positive_count}๊ฑด โ {summary.positive_summary}") | |
| cur_row += 1 | |
| ws.cell(row=cur_row, column=1, value="๋ถ์ ").font = label_font | |
| ws.cell(row=cur_row, column=2, | |
| value=f"{summary.negative_count}๊ฑด โ {summary.negative_summary}") | |
| cur_row += 1 | |
| ws.cell(row=cur_row, column=1, value="๊ธด ๋๊ธ").font = label_font | |
| ws.cell(row=cur_row, column=2, value=summary.longest_summary) | |
| cur_row += 1 | |
| if summary.top_keywords: | |
| ws.cell(row=cur_row, column=1, value="์์ ํค์๋").font = label_font | |
| ws.cell(row=cur_row, column=2, value=", ".join(summary.top_keywords)) | |
| cur_row += 1 | |
| cur_row += 1 | |
| ws.cell(row=cur_row, column=1, value="์ํธ ๋ฐ๋ก๊ฐ๊ธฐ").font = label_font | |
| cur_row += 1 | |
| link_fill = PatternFill("solid", fgColor="EFF6FF") if openpyxl else None | |
| link_font = Font(color="2563EB", underline="single") if openpyxl else None | |
| for name in sheet_names: | |
| cell = ws.cell(row=cur_row, column=2, value=name) | |
| if link_font: | |
| cell.font = link_font | |
| if link_fill: | |
| cell.fill = link_fill | |
| cell.hyperlink = f"#{name}!A1" | |
| cur_row += 1 | |
| def export_to_excel( | |
| comments: list, | |
| keywords: list, | |
| clusters: list, | |
| longest: list, | |
| positives: list, | |
| negatives: list, | |
| output_path: str, | |
| longest_ko: list = None, | |
| categories: list = None, | |
| summary = None, | |
| ) -> None: | |
| """๋ถ์ ๊ฒฐ๊ณผ๋ฅผ Excel ํ์ผ๋ก ์ ์ฅ. | |
| Args: | |
| comments: ์ ์ฒด Comment ๋ฆฌ์คํธ | |
| keywords: KeywordHit ๋ฆฌ์คํธ | |
| clusters: InsightCluster ๋ฆฌ์คํธ | |
| longest: Comment ๋ฆฌ์คํธ (๊ธด ๋๊ธ top N) | |
| positives: SentimentItem ๋ฆฌ์คํธ (๊ธ์ ) | |
| negatives: SentimentItem ๋ฆฌ์คํธ (๋ถ์ ) | |
| output_path: ์ ์ฅํ .xlsx ํ์ผ ๊ฒฝ๋ก | |
| longest_ko: longest ์ ๊ฐ์ ์์์ ํ๊ตญ์ด ๋ฒ์ญ ๋ฆฌ์คํธ | |
| categories: KeywordCategory ๋ฆฌ์คํธ (์์ผ๋ฉด Categories ์ํธ ์์ฑ) | |
| summary: OverallSummary (์์ผ๋ฉด ํ์ง์ ๋ ธ์ถ) | |
| """ | |
| if not openpyxl: | |
| raise RuntimeError("openpyxl ์ด ์ค์น๋์ด ์์ง ์์ต๋๋ค. pip install openpyxl") | |
| wb = Workbook() | |
| # ๊ธฐ๋ณธ ์ํธ ์ ๊ฑฐ | |
| wb.remove(wb.active) | |
| # --- ์ํธ ์์ --- | |
| sheet_names = ["Keywords"] | |
| if categories: | |
| sheet_names.append("Categories") | |
| sheet_names += ["Opinions", "Longest", "Positive", "Negative"] | |
| # ์ข์์ ์ ๋ณด ์๋ ๋๊ธ์ด ์์ผ๋ฉด Likes ์ํธ ์ถ๊ฐ | |
| has_likes = any(getattr(c, "likes", None) for c in comments) | |
| if has_likes: | |
| sheet_names.append("Likes") | |
| # 1) ํ์ง | |
| ws_cover = wb.create_sheet("ํ์ง") | |
| source_val = comments[0].source if comments else "unknown" | |
| _build_cover(ws_cover, len(comments), source_val, sheet_names, summary=summary) | |
| # 2) Keywords โ ์นดํ ๊ณ ๋ฆฌ ์์ผ๋ฉด category ์ปฌ๋ผ ๊ฐ์ด | |
| ws_kw = wb.create_sheet("Keywords") | |
| kw_to_cat: dict = {} | |
| if categories: | |
| for cat in categories: | |
| for kn in cat.keywords: | |
| kw_to_cat[kn] = cat.name | |
| if categories: | |
| _set_header_row(ws_kw, ["keyword", "category", "score", "count", "sample_comments"], _HEADER_FILL_BLUE) | |
| for kw in keywords: | |
| sample_text = " | ".join(kw.sample_comments) if kw.sample_comments else "" | |
| ws_kw.append([kw.keyword, kw_to_cat.get(kw.keyword, "๊ธฐํ"), | |
| round(kw.score, 4), kw.count, sample_text]) | |
| else: | |
| _set_header_row(ws_kw, ["keyword", "score", "count", "sample_comments"], _HEADER_FILL_BLUE) | |
| for kw in keywords: | |
| sample_text = " | ".join(kw.sample_comments) if kw.sample_comments else "" | |
| ws_kw.append([kw.keyword, round(kw.score, 4), kw.count, sample_text]) | |
| _auto_width(ws_kw) | |
| # 2-b) Categories ์ํธ (์์ ๋๋ง) | |
| if categories: | |
| ws_cat = wb.create_sheet("Categories") | |
| _set_header_row(ws_cat, ["category", "description", "keyword_count", "keywords"], _HEADER_FILL_BLUE) | |
| for cat in categories: | |
| ws_cat.append([cat.name, cat.description, len(cat.keywords), ", ".join(cat.keywords)]) | |
| _auto_width(ws_cat) | |
| # 3) Opinions | |
| ws_op = wb.create_sheet("Opinions") | |
| _set_header_row(ws_op, ["cluster_name", "summary", "comment_1", "comment_2", "comment_3"], _HEADER_FILL_GREEN) | |
| for cl in clusters: | |
| reps = cl.representative_comments | |
| ws_op.append([ | |
| cl.name, | |
| cl.summary, | |
| reps[0] if len(reps) > 0 else "", | |
| reps[1] if len(reps) > 1 else "", | |
| reps[2] if len(reps) > 2 else "", | |
| ]) | |
| _auto_width(ws_op) | |
| # 4) Longest โ ํ๊ตญ์ด ๋ฒ์ญ์ด ์์ผ๋ฉด text_ko ์ปฌ๋ผ ์ถ๊ฐ | |
| ws_lg = wb.create_sheet("Longest") | |
| if longest_ko: | |
| _set_header_row(ws_lg, ["user", "length", "text", "text_ko"], _HEADER_FILL_ORANGE) | |
| for i, c in enumerate(longest): | |
| ko = longest_ko[i] if i < len(longest_ko) else "" | |
| ws_lg.append([c.user, len(c.text), c.text, ko]) | |
| else: | |
| _set_header_row(ws_lg, ["user", "length", "text"], _HEADER_FILL_ORANGE) | |
| for c in longest: | |
| ws_lg.append([c.user, len(c.text), c.text]) | |
| _auto_width(ws_lg) | |
| # 5) Positive | |
| ws_pos = wb.create_sheet("Positive") | |
| _set_header_row(ws_pos, ["user", "intensity", "text", "reason"], _HEADER_FILL_PURPLE) | |
| for item in positives: | |
| ws_pos.append([item.user, round(item.intensity, 3), item.text, item.reason]) | |
| _auto_width(ws_pos) | |
| # 6) Negative | |
| ws_neg = wb.create_sheet("Negative") | |
| _set_header_row(ws_neg, ["user", "intensity", "text", "reason"], _HEADER_FILL_RED) | |
| for item in negatives: | |
| ws_neg.append([item.user, round(item.intensity, 3), item.text, item.reason]) | |
| _auto_width(ws_neg) | |
| # 7) Likes ์ํธ (์ข์์ ์ ๋ณด ์๋ ๋๊ธ๋ง) | |
| if has_likes: | |
| liked = [c for c in comments if getattr(c, "likes", None)] | |
| liked_sorted = sorted(liked, key=lambda c: c.likes, reverse=True)[:50] | |
| ws_lk = wb.create_sheet("Likes") | |
| _set_header_row(ws_lk, ["user", "likes", "time", "text"], _HEADER_FILL_RED) | |
| for c in liked_sorted: | |
| ws_lk.append([c.user, c.likes, c.time, c.text]) | |
| _auto_width(ws_lk) | |
| # ๋ชจ๋ ์ํธ ํ ์คํธ ์ ์ค๋ฐ๊ฟ ํ์ฉ | |
| for ws in wb.worksheets: | |
| for row in ws.iter_rows(min_row=2): | |
| for cell in row: | |
| if cell.value and isinstance(cell.value, str): | |
| cell.alignment = Alignment(wrap_text=True, vertical="top") | |
| wb.save(output_path) | |