Add Week column, filter and adjust layout widths

Eerste kolom Week met ISO-weeknummer (WEEKNUM(...,21)); autofilter op de kolomkoppen; bovenste kop (rijen 1-3) terug naar links; kolombreedtes Firma..Firma 3 op 3,5 cm en Opmerkingen op 7 cm; Week-kolom 2 cm en gecentreerd.
This commit is contained in:
2026-10-17 19:04:01 +02:00
parent be6b7a15a3
commit fc32a849ff
+80 -33
View File
@@ -5,28 +5,48 @@ from datetime import datetime
from pathlib import Path
import openpyxl
from openpyxl.styles import Font, Protection
from openpyxl.styles import Alignment, Font, Protection
from openpyxl.worksheet.datavalidation import DataValidation
from openpyxl.worksheet.properties import PageSetupProperties
TEMPLATE = Path(__file__).resolve().parent / "docs" / "Ineas.xlsx"
SETTINGS_SHEET = "Instellingen"
FIRM_NAMES = ["Galaxy", "Auro 3D", "Firma 3"]
FIRM_RATES = [20, 20, 40]
FIRM_COLS = ["G", "H", "I"]
FIRM_COLS = ["H", "I", "J"]
MONTH_ABBR = ["jan", "feb", "mrt", "apr", "mei", "jun",
"jul", "aug", "sep", "okt", "nov", "dec"]
STYLE_COLS = "ABCDEFGHIJ"
WIDTH_COLS = "BCDEFGHI" # Firma Van Tot Pauze Uren Galaxy Auro 3D Firma 3
COL_WIDTH = round(2.5 * 96 / 2.54 / 7, 2) # 2,5 cm in Excel-karakterbreedte
OLD_COLS = "ABCDEFGHIJ"
NEW_COLS = "ABCDEFGHIJK"
# nieuwe kolom -> oude kolom waarvan de stijl komt
OLD_FOR_NEW = {"A": "A", "B": "A", "C": "B", "D": "C", "E": "D",
"F": "E", "G": "F", "H": "G", "I": "H", "J": "I", "K": "J"}
def build_month(ws, year, month, day_styles, total_styles, protection):
def cm_to_width(cm):
"""cm naar Excel-karakterbreedte."""
return round(cm * 96 / 2.54 / 7, 2)
def build_month(ws, year, month, pack):
days = calendar.monthrange(year, month)[1]
if ws.max_row >= 7:
ws.delete_rows(7, ws.max_row - 6)
# Rijen 1-3 blijven links; rijen 4-6 schuiven één kolom op voor de Week-kolom.
for r in range(1, 7):
shift = 1 if r >= 4 else 0
for c in OLD_COLS:
value, style = pack["header"][r][c]
cell = ws[f"{chr(ord(c) + shift)}{r}"]
cell._style = copy(style)
if value is not None:
cell.value = value
ws["C1"] = datetime(year, month, 1)
ws["D1"] = "=C1"
ws.merge_cells("D1:E1")
ws["A6"] = "Week"
ws["A6"]._style = copy(pack["header"][6]["A"][1])
ws["A6"].alignment = Alignment(horizontal="center")
for i, col in enumerate(FIRM_COLS):
ws[f"{col}4"] = f"={col}6"
@@ -37,40 +57,55 @@ def build_month(ws, year, month, day_styles, total_styles, protection):
for d in range(days):
r = 7 + d
ws[f"A{r}"] = "=C1" if d == 0 else f"=A{r - 1}+1"
ws[f"F{r}"] = f"=D{r}-C{r}-E{r}"
ws[f"A{r}"] = f"=WEEKNUM(B{r},21)"
ws[f"B{r}"] = "=C1" if d == 0 else f"=B{r - 1}+1"
ws[f"G{r}"] = f"=E{r}-D{r}-F{r}"
for col in FIRM_COLS:
ws[f"{col}{r}"] = f"=IF($B{r}={col}$4,$F{r}*{col}$5,0)"
for col in STYLE_COLS:
ws[f"{col}{r}"]._style = copy(day_styles[col])
ws[f"{col}{r}"] = f"=IF($C{r}={col}$4,$G{r}*{col}$5,0)"
for col in NEW_COLS:
ws[f"{col}{r}"]._style = copy(pack["day"][OLD_FOR_NEW[col]])
ws[f"A{r}"].number_format = "0"
ws[f"A{r}"].alignment = Alignment(horizontal="center")
ws.row_dimensions[r].height = None
total = 6 + days + 1
first, last = 7, 6 + days
ws[f"A{total}"] = "Totaal"
ws[f"F{total}"] = f"=SUM(F{first}:F{last})"
ws[f"G{total}"] = f"=SUM(G{first}:G{last})"
for col in FIRM_COLS:
ws[f"{col}{total}"] = f"=SUM({col}{first}:{col}{last})"
for col in STYLE_COLS:
ws[f"{col}{total}"]._style = copy(total_styles[col])
for col in NEW_COLS:
ws[f"{col}{total}"]._style = copy(pack["total"][OLD_FOR_NEW[col]])
hours = total + 1
ws[f"A{hours}"] = "Uren per klant"
for col in STYLE_COLS:
ws[f"{col}{hours}"]._style = copy(total_styles[col])
for col in NEW_COLS:
ws[f"{col}{hours}"]._style = copy(pack["total"][OLD_FOR_NEW[col]])
for col in FIRM_COLS:
ws[f"{col}{hours}"] = f"=SUMIF($B${first}:$B${last},{col}$4,$F${first}:$F${last})"
ws[f"{col}{hours}"] = (
f"=SUMIF($C${first}:$C${last},{col}$4,$G${first}:$G${last})")
ws[f"{col}{hours}"].number_format = "0.00"
dv = DataValidation(type="list", allow_blank=True,
formula1="$G$6:$I$6")
formula1="$H$6:$J$6")
ws.add_data_validation(dv)
dv.add(f"B{first}:B{last}")
dv.add(f"C{first}:C{last}")
ws.auto_filter.ref = f"A6:K{last}"
ws.protection = copy(protection)
for col in WIDTH_COLS:
ws.column_dimensions[col].width = COL_WIDTH
ws.print_area = f"A1:J{hours}"
ws.column_dimensions["A"].width = cm_to_width(2)
ws.column_dimensions["B"].width = pack["datum_width"]
for col in "CDEFGHIJ":
ws.column_dimensions[col].width = cm_to_width(3.5)
ws.column_dimensions["K"].width = cm_to_width(7)
ws.protection = copy(pack["protection"])
ws.page_setup.orientation = "landscape"
ws.page_setup.paperSize = 9
ws.page_setup.fitToWidth = 1
ws.page_setup.fitToHeight = 0
ws.sheet_properties.pageSetUpPr = PageSetupProperties(fitToPage=True)
ws.page_margins = copy(pack["margins"])
ws.print_area = f"A1:K{hours}"
ws.print_title_rows = "1:6"
@@ -93,17 +128,29 @@ def generate(year, months):
raise SystemExit(f"Template niet gevonden: {TEMPLATE}")
wb = openpyxl.load_workbook(TEMPLATE)
master = wb["Sheet1"]
day_styles = {c: copy(master[f"{c}7"]._style) for c in STYLE_COLS}
total_styles = {c: copy(master[f"{c}37"]._style) for c in STYLE_COLS}
protection = copy(master.protection)
tpl = wb["Sheet1"]
header = {}
for r in range(1, 7):
header[r] = {}
for c in OLD_COLS:
cell = tpl[f"{c}{r}"]
header[r][c] = (cell.value, copy(cell._style))
pack = {
"header": header,
"day": {c: copy(tpl[f"{c}7"]._style) for c in OLD_COLS},
"total": {c: copy(tpl[f"{c}37"]._style) for c in OLD_COLS},
"protection": copy(tpl.protection),
"margins": copy(tpl.page_margins),
"datum_width": tpl.column_dimensions["A"].width,
}
sheets = [master] + [wb.copy_worksheet(master) for _ in range(len(months) - 1)]
for ws, month in zip(sheets, months):
ws.title = f"{month:02d}-{MONTH_ABBR[month - 1]}"
build_month(ws, year, month, day_styles, total_styles, protection)
for month in months:
ws = wb.create_sheet(f"{month:02d}-{MONTH_ABBR[month - 1]}")
build_month(ws, year, month, pack)
wb.remove(tpl)
add_settings_sheet(wb)
wb.active = 0
if len(months) == 12:
out = Path(f"prestatieformulier-{year}.xlsx")