From 8860b027775add6c8242917054ac6ec5f71e3c70 Mon Sep 17 00:00:00 2001 From: jmpla Date: Thu, 18 May 2023 06:56:48 +0200 Subject: [PATCH] add MergeEngine --- app/but/prepajury_cells.py | 25 +++++++++++++++++++++++++ app/but/prepajury_desc.py | 24 +++++++++--------------- app/but/prepajury_xl.py | 4 ++++ app/but/prepajury_xl_format.py | 6 ++++++ 4 files changed, 44 insertions(+), 15 deletions(-) diff --git a/app/but/prepajury_cells.py b/app/but/prepajury_cells.py index b16f07688..3d591f8f8 100644 --- a/app/but/prepajury_cells.py +++ b/app/but/prepajury_cells.py @@ -1,5 +1,6 @@ import openpyxl.cell from openpyxl.cell import Cell +from openpyxl.worksheet.worksheet import Worksheet from app.but.prepajury_xl import ( ScoExcelSheet, @@ -36,6 +37,30 @@ def set_cell( style.apply(cell) +class Merge_Engine: + def __init__(self, ws: Worksheet, start_row: int = 1, start_column: int = 1): + self.start_row: int = start_row + self.start_column: int = start_column + self.ws: Worksheet = ws + + def close(self, end_row=None, end_column=None): + if end_row is None: + end_row = self.start_row + if end_column is None: + end_column = self.start_column + if (end_row - self.start_row > 0) or (end_column - self.start_column > 0): + self.ws.merge_cells( + start_row=self.start_row, + start_column=self.start_column, + end_row=end_row, + end_column=end_column, + ) + if end_row is not None: + end_row = end_row + 1 + if end_column is not None: + end_column = end_column + 1 + + class Sco_Cell: def __init__( self, text: str = None, signature: int = base_signature, comment: str = None diff --git a/app/but/prepajury_desc.py b/app/but/prepajury_desc.py index 2868e6693..651636886 100644 --- a/app/but/prepajury_desc.py +++ b/app/but/prepajury_desc.py @@ -1,7 +1,7 @@ import openpyxl from openpyxl.worksheet.worksheet import Worksheet -from app.but.prepajury_cells import Sco_Cell, base_signature, set_cell +from app.but.prepajury_cells import Sco_Cell, base_signature, set_cell, Merge_Engine from app.but.prepajury_xl_format import ( SCO_COLORS, FMT, @@ -167,7 +167,7 @@ class AnneeDesc: for _ in range(3): header.add_column() - def generate_header(self, ws: Worksheet, column: int, codeAnnee: str): + def generate_header(self, ws: Worksheet, column: int): start = column but_signature = FMT.FILL_BGCOLOR.write( signature=base_signature, value=header_colors[self.codeAnnee]["BUT"].value @@ -177,14 +177,13 @@ class AnneeDesc: ws, 1, column, - text=codeAnnee, + text=self.codeAnnee, from_signature=but_signature, composition=[ (FMT.BORDER_LEFT_COLOR, SCO_COLORS.BLACK.value), (FMT.BORDER_LEFT_STYLE, SCO_BORDERTHICKNESS.BORDER_MEDIUM.value), ], ) - ws.cell(1, column).value = codeAnnee but_style.apply(ws.cell(1, column)) # for niveau in self.niveaux.values(): # column = niveau.generate_header(ws, column) @@ -278,22 +277,16 @@ class ParcoursDesc: style = FMT.style(self.signature_header) ws.cell(row, column).value = title style.apply(ws.cell(row, column)) + merge_h = Merge_Engine(ws=ws, start_row=row, start_column=column) + merge_v = Merge_Engine(ws=ws, start_row=row, start_column=column) if content_list is None: - ws.merge_cells( - start_row=row, end_row=4, start_column=column, end_column=column - ) + merge_v.close(end_row=4) column += 1 else: first = column for content in content_list: column = self.handle_description(ws, content, row + 1, column) - if column - first > 1: - ws.merge_cells( - start_row=row, - end_row=row, - start_column=first, - end_column=column - 1, - ) + merge_h.close(end_column=column - 1) # left_medium = FMT.compose( # self.signature_header, # [ @@ -322,6 +315,7 @@ class ParcoursDesc: # FMT.get_style(FMT.ALL, right_medium).apply(ws.cell(2, column - 1)) # FMT.get_style(FMT.ALL, right_medium).apply(ws.cell(3, column - 1)) # FMT.get_style(FMT.ALL, right_medium).apply(ws.cell(4, column - 1)) + # merge_h.close(end_column=column) return column def generate_etudiant_header(self, ws: Worksheet) -> int: @@ -350,7 +344,7 @@ class ParcoursDesc: def generate_header(self, ws: Worksheet): column: int = self.generate_etudiant_header(ws) for codeAnnee in listeAnnees: - column = self.annees[codeAnnee].generate_header(ws, column, codeAnnee) + column = self.annees[codeAnnee].generate_header(ws, column) def generate(self, workbook: ScoExcelBook): header = _Header() diff --git a/app/but/prepajury_xl.py b/app/but/prepajury_xl.py index 294fc67a9..8f5d10eb5 100644 --- a/app/but/prepajury_xl.py +++ b/app/but/prepajury_xl.py @@ -28,6 +28,7 @@ from __future__ import annotations from openpyxl.cell import WriteOnlyCell +from openpyxl.worksheet.worksheet import Worksheet from app.but.prepajury_xl_format import Sco_Style @@ -104,6 +105,9 @@ class ScoExcelBook: """génération d'un stream binaire représentant la totalité du classeur. retourne le flux """ + sheet: Worksheet = self.wb.get_sheet_by_name("Sheet") + self.wb.remove_sheet(sheet) + for sheet in self.sheets: sheet.prepare() # construction d'un flux diff --git a/app/but/prepajury_xl_format.py b/app/but/prepajury_xl_format.py index c41e3f38c..0bb6e8f06 100644 --- a/app/but/prepajury_xl_format.py +++ b/app/but/prepajury_xl_format.py @@ -410,6 +410,12 @@ class Composante_group(Composante): self.cache[value] = self.build(signature) return self.cache[value] + def make_zero_based(self, values: list[Enum]): + signature = 0 + for value, composante in zip(values, self.composantes): + breakpoint() + pass + class Composante_fill(Composante_group): def __init__(self, color: Composante_Colors):