feat: enhance Excel table header and register styling with Navy fill, white bold font, borders, and AutoFilter

This commit is contained in:
Atharv1705
2026-11-16 10:57:16 +05:30
parent 5d98aa57cb
commit 13fd84a53c
6 changed files with 226 additions and 12 deletions
+17 -2
View File
@@ -5,7 +5,7 @@ from pathlib import Path
from typing import Any
import openpyxl
from openpyxl.styles import Font, PatternFill
from openpyxl.styles import Alignment, Border, Font, PatternFill, Side
from openpyxl.utils import get_column_letter
from sqlalchemy.orm import Session
@@ -36,7 +36,13 @@ VALID_STATUSES = ("in_service", "under_repair", "out_of_service", "condemned")
VALID_CRITICALITIES = ("critical", "non_critical")
_HEADER_FILL = PatternFill(start_color="1E3A8A", end_color="1E3A8A", fill_type="solid")
_HEADER_FONT = Font(color="FFFFFF", bold=True)
_HEADER_FONT = Font(name="Calibri", size=11, color="FFFFFF", bold=True)
_HEADER_BORDER = Border(
left=Side(style="thin", color="334155"),
right=Side(style="thin", color="334155"),
top=Side(style="thin", color="334155"),
bottom=Side(style="medium", color="0F172A"),
)
def _auto_fit_columns(ws, max_width: int = 42) -> None:
@@ -58,6 +64,11 @@ def build_import_template_xlsx(db: Session) -> BytesIO:
cell = ws.cell(row=1, column=col_idx, value=col_name)
cell.fill = _HEADER_FILL
cell.font = _HEADER_FONT
cell.alignment = Alignment(horizontal="center", vertical="center", wrap_text=True)
cell.border = _HEADER_BORDER
ws.row_dimensions[1].height = 26
ws.views.sheetView[0].showGridLines = True
ws.auto_filter.ref = f"A1:{get_column_letter(len(IMPORT_COLUMNS))}2"
ws.freeze_panes = "A2"
departments = (
@@ -119,6 +130,8 @@ def build_import_template_xlsx(db: Session) -> BytesIO:
# Sheet 2: Master Reference
ref = wb.create_sheet(title="Master Reference")
ref.row_dimensions[1].height = 26
ref.views.sheetView[0].showGridLines = True
headers_map = [
(1, "Department Code"),
(2, "Department Name"),
@@ -133,6 +146,8 @@ def build_import_template_xlsx(db: Session) -> BytesIO:
c = ref.cell(row=1, column=col_idx, value=label)
c.fill = _HEADER_FILL
c.font = _HEADER_FONT
c.alignment = Alignment(horizontal="center", vertical="center", wrap_text=True)
c.border = _HEADER_BORDER
ref.freeze_panes = "A2"
dept_by_id = {d.id: d for d in departments}
+29 -2
View File
@@ -5,7 +5,7 @@ Bulk Import Template Builders for Contracts, Calibration Plans, and Insurance Po
from io import BytesIO
import openpyxl
from openpyxl.styles import Font, PatternFill
from openpyxl.styles import Alignment, Border, Font, PatternFill, Side
from openpyxl.utils import get_column_letter
from sqlalchemy.orm import Session
@@ -13,8 +13,14 @@ from ..models import Asset, Department, Vendor
from .settings_service import get_setting_value
_HEADER_FILL = PatternFill(start_color="1E3A8A", end_color="1E3A8A", fill_type="solid")
_HEADER_FONT = Font(color="FFFFFF", bold=True)
_HEADER_FONT = Font(name="Calibri", size=11, color="FFFFFF", bold=True)
_REF_HEADER_FILL = PatternFill(start_color="334155", end_color="334155", fill_type="solid")
_HEADER_BORDER = Border(
left=Side(style="thin", color="334155"),
right=Side(style="thin", color="334155"),
top=Side(style="thin", color="334155"),
bottom=Side(style="medium", color="0F172A"),
)
def _auto_fit_columns(ws, max_width: int = 42) -> None:
@@ -46,6 +52,11 @@ def build_contracts_template_xlsx(db: Session) -> BytesIO:
c = ws.cell(row=1, column=col_idx, value=col_name)
c.fill = _HEADER_FILL
c.font = _HEADER_FONT
c.alignment = Alignment(horizontal="center", vertical="center", wrap_text=True)
c.border = _HEADER_BORDER
ws.row_dimensions[1].height = 26
ws.views.sheetView[0].showGridLines = True
ws.auto_filter.ref = f"A1:{get_column_letter(len(cols))}2"
ws.freeze_panes = "A2"
assets = db.query(Asset).join(Department).filter(Asset.status != "condemned").order_by(Asset.asset_code).all()
@@ -69,6 +80,8 @@ def build_contracts_template_xlsx(db: Session) -> BytesIO:
# Reference sheet
ws_ref = wb.create_sheet(title="Master Reference")
ws_ref.row_dimensions[1].height = 26
ws_ref.views.sheetView[0].showGridLines = True
ws_ref.cell(row=1, column=1, value="Asset Code").fill = _REF_HEADER_FILL
ws_ref.cell(row=1, column=1).font = _HEADER_FONT
ws_ref.cell(row=1, column=2, value="Asset Name").fill = _REF_HEADER_FILL
@@ -117,6 +130,11 @@ def build_calibration_template_xlsx(db: Session) -> BytesIO:
c = ws.cell(row=1, column=col_idx, value=col_name)
c.fill = _HEADER_FILL
c.font = _HEADER_FONT
c.alignment = Alignment(horizontal="center", vertical="center", wrap_text=True)
c.border = _HEADER_BORDER
ws.row_dimensions[1].height = 26
ws.views.sheetView[0].showGridLines = True
ws.auto_filter.ref = f"A1:{get_column_letter(len(cols))}2"
ws.freeze_panes = "A2"
assets = (
@@ -147,6 +165,8 @@ def build_calibration_template_xlsx(db: Session) -> BytesIO:
# Reference sheet
ws_ref = wb.create_sheet(title="Master Reference")
ws_ref.row_dimensions[1].height = 26
ws_ref.views.sheetView[0].showGridLines = True
ws_ref.cell(row=1, column=1, value="Asset Code").fill = _REF_HEADER_FILL
ws_ref.cell(row=1, column=1).font = _HEADER_FONT
ws_ref.cell(row=1, column=2, value="Asset Name").fill = _REF_HEADER_FILL
@@ -197,6 +217,11 @@ def build_insurance_template_xlsx(db: Session) -> BytesIO:
c = ws.cell(row=1, column=col_idx, value=col_name)
c.fill = _HEADER_FILL
c.font = _HEADER_FONT
c.alignment = Alignment(horizontal="center", vertical="center", wrap_text=True)
c.border = _HEADER_BORDER
ws.row_dimensions[1].height = 26
ws.views.sheetView[0].showGridLines = True
ws.auto_filter.ref = f"A1:{get_column_letter(len(cols))}2"
ws.freeze_panes = "A2"
assets = db.query(Asset).join(Department).filter(Asset.status != "condemned").order_by(Asset.asset_code).all()
@@ -218,6 +243,8 @@ def build_insurance_template_xlsx(db: Session) -> BytesIO:
# Reference sheet
ws_ref = wb.create_sheet(title="Master Reference")
ws_ref.row_dimensions[1].height = 26
ws_ref.views.sheetView[0].showGridLines = True
ws_ref.cell(row=1, column=1, value="Asset Code").fill = _REF_HEADER_FILL
ws_ref.cell(row=1, column=1).font = _HEADER_FONT
ws_ref.cell(row=1, column=2, value="Asset Name").fill = _REF_HEADER_FILL