reapply conditional formatting to populated file

This commit is contained in:
Daniel Roth 2026-06-09 13:29:11 +00:00
parent f8c955b2d3
commit 608006d977
2 changed files with 26 additions and 0 deletions

View file

@ -7,6 +7,10 @@ from pathlib import Path
from typing import TYPE_CHECKING, Any, cast
import openpyxl
from openpyxl.cell.rich_text import CellRichText, TextBlock
from openpyxl.cell.text import InlineFont
from openpyxl.formatting.rule import CellIsRule # type: ignore[reportUnknownVariableType]
from openpyxl.styles import Color, Font
from domain.magicplan.models import Door, Plan, Room, Window
from infrastructure.postgres.uploaded_file_table import (
@ -23,6 +27,26 @@ _TEMPLATE_PATH = Path(__file__).parent.parent / "applications" / "audit_generato
_SHEET_NAME = "D1 Ventilation"
_DATA_START_ROW = 6
_MAX_ROWS = 50
_Y_CF_RANGE = f"Y{_DATA_START_ROW}:Y{_DATA_START_ROW + _MAX_ROWS - 1}"
_Y_THRESHOLD = 7600
_Y_HEADER = CellRichText(
TextBlock(InlineFont(b=True, sz=11, rFont="Aptos Narrow"), "Area (mm2)\n"),
TextBlock(InlineFont(b=True, sz=11, color=Color(rgb="FF0000"), rFont="Aptos Narrow"), "<"),
TextBlock(InlineFont(b=True, sz=11, rFont="Aptos Narrow"), " 7600 "),
TextBlock(InlineFont(b=True, sz=11, color=Color(rgb="196B24"), rFont="Aptos Narrow"), "<"),
)
def _apply_column_y_formatting(sheet: Any) -> None:
sheet.conditional_formatting.add(
_Y_CF_RANGE,
CellIsRule(operator="lessThan", formula=[str(_Y_THRESHOLD)], font=Font(color=Color(rgb="FF0000"))),
)
sheet.conditional_formatting.add(
_Y_CF_RANGE,
CellIsRule(operator="greaterThan", formula=[str(_Y_THRESHOLD)], font=Font(color=Color(rgb="196B24"))),
)
sheet["Y3"] = _Y_HEADER
def _write_cell(sheet: Any, row: int, col: str, value: Any) -> None:
@ -75,6 +99,8 @@ def _populate_sheet(sheet: Any, plan: Plan) -> None:
_write_cell(sheet, row, "X", vent.undercut_mm if vent else 0)
# Y = formula =W*X — do not write
_apply_column_y_formatting(sheet)
def _serialise_workbook(wb: Any) -> bytes:
buf = BytesIO()