add MergeEngine

This commit is contained in:
Jean-Marie Place 2023-05-18 06:56:48 +02:00
parent c536336017
commit 8860b02777
4 changed files with 44 additions and 15 deletions

View File

@ -1,5 +1,6 @@
import openpyxl.cell import openpyxl.cell
from openpyxl.cell import Cell from openpyxl.cell import Cell
from openpyxl.worksheet.worksheet import Worksheet
from app.but.prepajury_xl import ( from app.but.prepajury_xl import (
ScoExcelSheet, ScoExcelSheet,
@ -36,6 +37,30 @@ def set_cell(
style.apply(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: class Sco_Cell:
def __init__( def __init__(
self, text: str = None, signature: int = base_signature, comment: str = None self, text: str = None, signature: int = base_signature, comment: str = None

View File

@ -1,7 +1,7 @@
import openpyxl import openpyxl
from openpyxl.worksheet.worksheet import Worksheet 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 ( from app.but.prepajury_xl_format import (
SCO_COLORS, SCO_COLORS,
FMT, FMT,
@ -167,7 +167,7 @@ class AnneeDesc:
for _ in range(3): for _ in range(3):
header.add_column() header.add_column()
def generate_header(self, ws: Worksheet, column: int, codeAnnee: str): def generate_header(self, ws: Worksheet, column: int):
start = column start = column
but_signature = FMT.FILL_BGCOLOR.write( but_signature = FMT.FILL_BGCOLOR.write(
signature=base_signature, value=header_colors[self.codeAnnee]["BUT"].value signature=base_signature, value=header_colors[self.codeAnnee]["BUT"].value
@ -177,14 +177,13 @@ class AnneeDesc:
ws, ws,
1, 1,
column, column,
text=codeAnnee, text=self.codeAnnee,
from_signature=but_signature, from_signature=but_signature,
composition=[ composition=[
(FMT.BORDER_LEFT_COLOR, SCO_COLORS.BLACK.value), (FMT.BORDER_LEFT_COLOR, SCO_COLORS.BLACK.value),
(FMT.BORDER_LEFT_STYLE, SCO_BORDERTHICKNESS.BORDER_MEDIUM.value), (FMT.BORDER_LEFT_STYLE, SCO_BORDERTHICKNESS.BORDER_MEDIUM.value),
], ],
) )
ws.cell(1, column).value = codeAnnee
but_style.apply(ws.cell(1, column)) but_style.apply(ws.cell(1, column))
# for niveau in self.niveaux.values(): # for niveau in self.niveaux.values():
# column = niveau.generate_header(ws, column) # column = niveau.generate_header(ws, column)
@ -278,22 +277,16 @@ class ParcoursDesc:
style = FMT.style(self.signature_header) style = FMT.style(self.signature_header)
ws.cell(row, column).value = title ws.cell(row, column).value = title
style.apply(ws.cell(row, column)) 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: if content_list is None:
ws.merge_cells( merge_v.close(end_row=4)
start_row=row, end_row=4, start_column=column, end_column=column
)
column += 1 column += 1
else: else:
first = column first = column
for content in content_list: for content in content_list:
column = self.handle_description(ws, content, row + 1, column) column = self.handle_description(ws, content, row + 1, column)
if column - first > 1: merge_h.close(end_column=column - 1)
ws.merge_cells(
start_row=row,
end_row=row,
start_column=first,
end_column=column - 1,
)
# left_medium = FMT.compose( # left_medium = FMT.compose(
# self.signature_header, # 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(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(3, column - 1))
# FMT.get_style(FMT.ALL, right_medium).apply(ws.cell(4, column - 1)) # FMT.get_style(FMT.ALL, right_medium).apply(ws.cell(4, column - 1))
# merge_h.close(end_column=column)
return column return column
def generate_etudiant_header(self, ws: Worksheet) -> int: def generate_etudiant_header(self, ws: Worksheet) -> int:
@ -350,7 +344,7 @@ class ParcoursDesc:
def generate_header(self, ws: Worksheet): def generate_header(self, ws: Worksheet):
column: int = self.generate_etudiant_header(ws) column: int = self.generate_etudiant_header(ws)
for codeAnnee in listeAnnees: 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): def generate(self, workbook: ScoExcelBook):
header = _Header() header = _Header()

View File

@ -28,6 +28,7 @@
from __future__ import annotations from __future__ import annotations
from openpyxl.cell import WriteOnlyCell from openpyxl.cell import WriteOnlyCell
from openpyxl.worksheet.worksheet import Worksheet
from app.but.prepajury_xl_format import Sco_Style 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. """génération d'un stream binaire représentant la totalité du classeur.
retourne le flux retourne le flux
""" """
sheet: Worksheet = self.wb.get_sheet_by_name("Sheet")
self.wb.remove_sheet(sheet)
for sheet in self.sheets: for sheet in self.sheets:
sheet.prepare() sheet.prepare()
# construction d'un flux # construction d'un flux

View File

@ -410,6 +410,12 @@ class Composante_group(Composante):
self.cache[value] = self.build(signature) self.cache[value] = self.build(signature)
return self.cache[value] 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): class Composante_fill(Composante_group):
def __init__(self, color: Composante_Colors): def __init__(self, color: Composante_Colors):