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:
@@ -5,28 +5,48 @@ from datetime import datetime
|
|||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
import openpyxl
|
import openpyxl
|
||||||
from openpyxl.styles import Font, Protection
|
from openpyxl.styles import Alignment, Font, Protection
|
||||||
from openpyxl.worksheet.datavalidation import DataValidation
|
from openpyxl.worksheet.datavalidation import DataValidation
|
||||||
|
from openpyxl.worksheet.properties import PageSetupProperties
|
||||||
|
|
||||||
TEMPLATE = Path(__file__).resolve().parent / "docs" / "Ineas.xlsx"
|
TEMPLATE = Path(__file__).resolve().parent / "docs" / "Ineas.xlsx"
|
||||||
SETTINGS_SHEET = "Instellingen"
|
SETTINGS_SHEET = "Instellingen"
|
||||||
FIRM_NAMES = ["Galaxy", "Auro 3D", "Firma 3"]
|
FIRM_NAMES = ["Galaxy", "Auro 3D", "Firma 3"]
|
||||||
FIRM_RATES = [20, 20, 40]
|
FIRM_RATES = [20, 20, 40]
|
||||||
FIRM_COLS = ["G", "H", "I"]
|
FIRM_COLS = ["H", "I", "J"]
|
||||||
MONTH_ABBR = ["jan", "feb", "mrt", "apr", "mei", "jun",
|
MONTH_ABBR = ["jan", "feb", "mrt", "apr", "mei", "jun",
|
||||||
"jul", "aug", "sep", "okt", "nov", "dec"]
|
"jul", "aug", "sep", "okt", "nov", "dec"]
|
||||||
STYLE_COLS = "ABCDEFGHIJ"
|
OLD_COLS = "ABCDEFGHIJ"
|
||||||
WIDTH_COLS = "BCDEFGHI" # Firma Van Tot Pauze Uren Galaxy Auro 3D Firma 3
|
NEW_COLS = "ABCDEFGHIJK"
|
||||||
COL_WIDTH = round(2.5 * 96 / 2.54 / 7, 2) # 2,5 cm in Excel-karakterbreedte
|
# 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]
|
days = calendar.monthrange(year, month)[1]
|
||||||
|
|
||||||
if ws.max_row >= 7:
|
# Rijen 1-3 blijven links; rijen 4-6 schuiven één kolom op voor de Week-kolom.
|
||||||
ws.delete_rows(7, ws.max_row - 6)
|
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["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):
|
for i, col in enumerate(FIRM_COLS):
|
||||||
ws[f"{col}4"] = f"={col}6"
|
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):
|
for d in range(days):
|
||||||
r = 7 + d
|
r = 7 + d
|
||||||
ws[f"A{r}"] = "=C1" if d == 0 else f"=A{r - 1}+1"
|
ws[f"A{r}"] = f"=WEEKNUM(B{r},21)"
|
||||||
ws[f"F{r}"] = f"=D{r}-C{r}-E{r}"
|
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:
|
for col in FIRM_COLS:
|
||||||
ws[f"{col}{r}"] = f"=IF($B{r}={col}$4,$F{r}*{col}$5,0)"
|
ws[f"{col}{r}"] = f"=IF($C{r}={col}$4,$G{r}*{col}$5,0)"
|
||||||
for col in STYLE_COLS:
|
for col in NEW_COLS:
|
||||||
ws[f"{col}{r}"]._style = copy(day_styles[col])
|
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
|
ws.row_dimensions[r].height = None
|
||||||
|
|
||||||
total = 6 + days + 1
|
total = 6 + days + 1
|
||||||
first, last = 7, 6 + days
|
first, last = 7, 6 + days
|
||||||
ws[f"A{total}"] = "Totaal"
|
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:
|
for col in FIRM_COLS:
|
||||||
ws[f"{col}{total}"] = f"=SUM({col}{first}:{col}{last})"
|
ws[f"{col}{total}"] = f"=SUM({col}{first}:{col}{last})"
|
||||||
for col in STYLE_COLS:
|
for col in NEW_COLS:
|
||||||
ws[f"{col}{total}"]._style = copy(total_styles[col])
|
ws[f"{col}{total}"]._style = copy(pack["total"][OLD_FOR_NEW[col]])
|
||||||
|
|
||||||
hours = total + 1
|
hours = total + 1
|
||||||
ws[f"A{hours}"] = "Uren per klant"
|
ws[f"A{hours}"] = "Uren per klant"
|
||||||
for col in STYLE_COLS:
|
for col in NEW_COLS:
|
||||||
ws[f"{col}{hours}"]._style = copy(total_styles[col])
|
ws[f"{col}{hours}"]._style = copy(pack["total"][OLD_FOR_NEW[col]])
|
||||||
for col in FIRM_COLS:
|
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"
|
ws[f"{col}{hours}"].number_format = "0.00"
|
||||||
|
|
||||||
dv = DataValidation(type="list", allow_blank=True,
|
dv = DataValidation(type="list", allow_blank=True,
|
||||||
formula1="$G$6:$I$6")
|
formula1="$H$6:$J$6")
|
||||||
ws.add_data_validation(dv)
|
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)
|
ws.column_dimensions["A"].width = cm_to_width(2)
|
||||||
for col in WIDTH_COLS:
|
ws.column_dimensions["B"].width = pack["datum_width"]
|
||||||
ws.column_dimensions[col].width = COL_WIDTH
|
for col in "CDEFGHIJ":
|
||||||
ws.print_area = f"A1:J{hours}"
|
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"
|
ws.print_title_rows = "1:6"
|
||||||
|
|
||||||
|
|
||||||
@@ -93,17 +128,29 @@ def generate(year, months):
|
|||||||
raise SystemExit(f"Template niet gevonden: {TEMPLATE}")
|
raise SystemExit(f"Template niet gevonden: {TEMPLATE}")
|
||||||
|
|
||||||
wb = openpyxl.load_workbook(TEMPLATE)
|
wb = openpyxl.load_workbook(TEMPLATE)
|
||||||
master = wb["Sheet1"]
|
tpl = wb["Sheet1"]
|
||||||
day_styles = {c: copy(master[f"{c}7"]._style) for c in STYLE_COLS}
|
header = {}
|
||||||
total_styles = {c: copy(master[f"{c}37"]._style) for c in STYLE_COLS}
|
for r in range(1, 7):
|
||||||
protection = copy(master.protection)
|
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 month in months:
|
||||||
for ws, month in zip(sheets, months):
|
ws = wb.create_sheet(f"{month:02d}-{MONTH_ABBR[month - 1]}")
|
||||||
ws.title = f"{month:02d}-{MONTH_ABBR[month - 1]}"
|
build_month(ws, year, month, pack)
|
||||||
build_month(ws, year, month, day_styles, total_styles, protection)
|
|
||||||
|
|
||||||
|
wb.remove(tpl)
|
||||||
add_settings_sheet(wb)
|
add_settings_sheet(wb)
|
||||||
|
wb.active = 0
|
||||||
|
|
||||||
if len(months) == 12:
|
if len(months) == 12:
|
||||||
out = Path(f"prestatieformulier-{year}.xlsx")
|
out = Path(f"prestatieformulier-{year}.xlsx")
|
||||||
|
|||||||
Reference in New Issue
Block a user