Files
37duAutomation/plugins/XQ0001-0100/XQ0036 门店员工起调薪处理/report/excel.py
T

42 lines
1.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# -*- coding: utf-8 -*-
"""结果导出(xlsx)。"""
from __future__ import annotations
import os
import pandas as pd
from openpyxl.styles import Alignment, Font, PatternFill
from openpyxl.utils import get_column_letter
from .. import config
def export_result(records: list, save_dir: str, log=None) -> str:
os.makedirs(save_dir, exist_ok=True)
path = os.path.join(save_dir, config.RESULT_FILENAME)
frame = pd.DataFrame(records, columns=config.COLUMNS)
frame.to_excel(path, index=False)
try:
from openpyxl import load_workbook
book = load_workbook(path)
sheet = book.active
sheet.freeze_panes = "A2"
for column in sheet[1]:
column.font = Font(bold=True)
column.alignment = Alignment(horizontal="center", vertical="center")
column.fill = PatternFill("solid", fgColor="DDEBF7")
for idx, name in enumerate(config.COLUMNS, 1):
width = 14 if idx in (9, 10, 20, 21, 22) else 12
letter = get_column_letter(idx)
sheet.column_dimensions[letter].width = width
book.save(path)
except Exception as exc: # noqa: BLE001 - 美化失败不影响结果
if log:
log(f"[WARN] 结果表样式处理失败:{exc}", "WARNING")
if log:
log(f"[INFO] 结果已导出:{path}({len(records)} 行)")
return path