Files
2026Technology-Competition/tests/excel_helpers.py
T

20 lines
641 B
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.
from openpyxl import Workbook
def new_workbook(sheets: dict[str, list[list]]) -> Workbook:
"""生成临时 Workbookkey=Sheet 名,value=gridcell 值)。"""
wb = Workbook()
wb.remove(wb.active)
for name, grid in sheets.items():
ws = wb.create_sheet(name)
for r, row in enumerate(grid, start=1):
for c, value in enumerate(row, start=1):
ws.cell(row=r, column=c, value=value)
return wb
def save_workbook(tmp_path, wb: Workbook) -> str:
"""落盘到 tmp_path 并返回路径字符串。"""
path = tmp_path / "source.xlsx"
wb.save(path)
return str(path)