neosimHan's picture
Initial deploy: Google Workspace SSO + reels-bench v2.1.3
a3c17c2
Raw History Blame Contribute Delete
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)