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.
179 lines
6.0 KiB
Python
179 lines
6.0 KiB
Python
import argparse
|
|
import calendar
|
|
from copy import copy
|
|
from datetime import datetime
|
|
from pathlib import Path
|
|
|
|
import openpyxl
|
|
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 = ["H", "I", "J"]
|
|
MONTH_ABBR = ["jan", "feb", "mrt", "apr", "mei", "jun",
|
|
"jul", "aug", "sep", "okt", "nov", "dec"]
|
|
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 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]
|
|
|
|
# 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"
|
|
ws[f"{col}5"] = f"={SETTINGS_SHEET}!$B${2 + i}"
|
|
ws[f"{col}6"] = f"={SETTINGS_SHEET}!$A${2 + i}"
|
|
ws[f"{col}6"].protection = Protection(locked=False)
|
|
ws[f"{col}6"].fill = copy(ws[f"{col}5"].fill)
|
|
|
|
for d in range(days):
|
|
r = 7 + d
|
|
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($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"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 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 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($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="$H$6:$J$6")
|
|
ws.add_data_validation(dv)
|
|
dv.add(f"C{first}:C{last}")
|
|
ws.auto_filter.ref = f"A6:K{last}"
|
|
|
|
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"
|
|
|
|
|
|
def add_settings_sheet(wb):
|
|
ws = wb.create_sheet(SETTINGS_SHEET)
|
|
ws["A1"], ws["B1"] = "Firma", "Tarief/uur"
|
|
ws["A1"].font = Font(bold=True)
|
|
ws["B1"].font = Font(bold=True)
|
|
for i, (name, rate) in enumerate(zip(FIRM_NAMES, FIRM_RATES)):
|
|
ws[f"A{2 + i}"] = name
|
|
ws[f"B{2 + i}"] = rate
|
|
ws[f"B{2 + i}"].number_format = "#,##0.00"
|
|
ws.column_dimensions["A"].width = 20
|
|
ws.column_dimensions["B"].width = 12
|
|
return ws
|
|
|
|
|
|
def generate(year, months):
|
|
if not TEMPLATE.exists():
|
|
raise SystemExit(f"Template niet gevonden: {TEMPLATE}")
|
|
|
|
wb = openpyxl.load_workbook(TEMPLATE)
|
|
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,
|
|
}
|
|
|
|
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")
|
|
else:
|
|
out = Path(f"prestatieformulier-{year}-{months[0]:02d}.xlsx")
|
|
wb.save(out)
|
|
return out
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(
|
|
description="Genereer een Excel prestatieformulier.")
|
|
parser.add_argument("--year", type=int, required=True,
|
|
help="Jaartal, bv. 2026")
|
|
parser.add_argument("--month", type=int, choices=range(1, 13),
|
|
help="Maand 1-12; weglaten voor een volledig jaar")
|
|
args = parser.parse_args()
|
|
|
|
months = [args.month] if args.month else list(range(1, 13))
|
|
out = generate(args.year, months)
|
|
print(f"Gegenereerd: {out}")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|