import openpyxl.cell from openpyxl.cell import Cell from openpyxl.worksheet.worksheet import Worksheet from app.but.prepajury_xl import ( ScoExcelSheet, ) from app.but.prepajury_xl_format import ( SCO_HALIGN, SCO_VALIGN, SCO_FONTNAME, SCO_FONTSIZE, FMT, Sco_Style, ) base_signature = ( FMT.FONT_NAME.set(SCO_FONTNAME.FONTNAME_CALIBRI) + FMT.FONT_SIZE.set(SCO_FONTSIZE.FONTSIZE_13) + FMT.ALIGNMENT_HALIGN.set(SCO_HALIGN.HALIGN_CENTER) + FMT.ALIGNMENT_VALIGN.set(SCO_VALIGN.VALIGN_CENTER) ) def set_cell( ws: Cell, row: int, column: int, text: str = "", from_signature: int = 0, composition: list = [], ): cell = ws.cell(row, column) cell.value = text sign = FMT.compose(composition, from_signature) style: Sco_Style = FMT.style(sign) 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 ): self.text = text self.signature = signature or base_signature self.comment = comment def format(self, value=None): self.signature = FMT.ALL.composante.write(self.signature, value) def copy(self, cell: openpyxl.cell.Cell): cell.value = self.text style: Sco_Style = FMT.style(self.signature) style.apply(cell) def make_cell(self, worksheet: ScoExcelSheet): cell = worksheet.make_cell(self.text or "") style: Sco_Style = FMT.style(self.signature) style.apply(cell) return cell