From 4bd90bf83661a801df181e29aaa193478b882f36 Mon Sep 17 00:00:00 2001 From: Khalim Conn-Kowlessar Date: Tue, 14 Jul 2026 09:19:40 +0000 Subject: [PATCH] =?UTF-8?q?Render=20the=20scenario=20export=20workbook=20w?= =?UTF-8?q?ith=20a=20sheet=20per=20scenario=20=F0=9F=9F=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 4.8 (1M context) --- .../xlsx/scenario_export_workbook.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/infrastructure/xlsx/scenario_export_workbook.py b/infrastructure/xlsx/scenario_export_workbook.py index 0da8a6f4f..f86614a08 100644 --- a/infrastructure/xlsx/scenario_export_workbook.py +++ b/infrastructure/xlsx/scenario_export_workbook.py @@ -8,8 +8,11 @@ dynamic number of scenario sheets is handled natively. from __future__ import annotations +from io import BytesIO from typing import Sequence +from openpyxl import Workbook + from domain.scenario_export.scenario_sheet import ExportSheet @@ -17,4 +20,17 @@ def render_workbook(sheets: Sequence[tuple[str, ExportSheet]]) -> bytes: """Render the scenario sheets into a single branded ``.xlsx`` as bytes. Each tuple is a Scenario name and its shaped sheet; sheet order follows the input (the requested ``scenario_ids`` order).""" - raise NotImplementedError + workbook = Workbook() + default = workbook.active + if default is not None: + workbook.remove(default) + + for name, sheet in sheets: + worksheet = workbook.create_sheet(title=name) + worksheet.append(list(sheet.columns)) + for row in sheet.rows: + worksheet.append([row.get(column, "") for column in sheet.columns]) + + buffer = BytesIO() + workbook.save(buffer) + return buffer.getvalue()