import os import re import unicodedata import pandas as pd import openpyxl base_dir = os.path.dirname(os.path.abspath(__file__)) # Locate directory handling Unicode normalization (NFC/NFD) target_dir = None for d in os.listdir(base_dir): if unicodedata.normalize('NFC', d) == '사건종류_요건사실론문서_검증': target_dir = os.path.join(base_dir, d) break if not target_dir: raise FileNotFoundError("Directory '사건종류_요건사실론문서_검증' not found.") pattern = re.compile(r'^검증_요건사실론_(.+)_v1\.md$') items = [] for f in os.listdir(target_dir): f_norm = unicodedata.normalize('NFC', f) m = pattern.match(f_norm) if m: items.append(unicodedata.normalize('NFC', m.group(1))) items_sorted = sorted(items) output_path = os.path.join(target_dir, '검증문서_리스트.xlsx') df = pd.DataFrame(items_sorted, columns=['사건종류']) df.to_excel(output_path, index=False) # Auto-adjust column width wb = openpyxl.load_workbook(output_path) ws = wb.active for col in ws.columns: max_len = 0 col_letter = col[0].column_letter for cell in col: if cell.value is not None: val_str = str(cell.value) length = sum(2 if ord(c) > 127 else 1 for c in val_str) if length > max_len: max_len = length ws.column_dimensions[col_letter].width = max(max_len + 4, 15) wb.save(output_path) print(f"Excel file created with {len(items_sorted)} items at: {output_path}")