ScoDoc-Lille/app/but/prepajury_xl.py

411 lines
15 KiB
Python

# -*- mode: python -*-
# -*- coding: utf-8 -*-
##############################################################################
#
# Gestion scolarite IUT
#
# Copyright (c) 1999 - 2023 Emmanuel Viennet. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# Emmanuel Viennet emmanuel.viennet@viennet.net
#
##############################################################################
from __future__ import annotations
from collections import defaultdict
from openpyxl.cell import WriteOnlyCell
from openpyxl.worksheet.worksheet import Worksheet
from app.but.prepajury_xl_format import (
Sco_Style,
FMT,
SCO_FONTNAME,
SCO_FONTSIZE,
SCO_HALIGN,
SCO_VALIGN,
SCO_NUMBER_FORMAT,
SCO_BORDERTHICKNESS,
SCO_COLORS,
fmt_atomics,
)
""" Excel file handling
"""
import datetime
from tempfile import NamedTemporaryFile
import openpyxl.utils.datetime
from openpyxl.styles.numbers import FORMAT_DATE_DDMMYY
from openpyxl.comments import Comment
from openpyxl import Workbook
import app.scodoc.sco_utils as scu
from app.scodoc.sco_exceptions import ScoValueError
# Un style est enregistré comme un dictionnaire qui précise la valeur d'un attribut dans la liste suivante:
# font, border, number_format, fill,...
# (cf https://openpyxl.readthedocs.io/en/stable/styles.html#working-with-styles)
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)
+ FMT.NUMBER_FORMAT.set(SCO_NUMBER_FORMAT.NUMBER_GENERAL)
)
class Sco_Cell:
def __init__(self, text: str = "", signature: int = 0):
self.text = text
self.signature = signature
def alter(self, signature: int):
for fmt in fmt_atomics:
value: int = fmt.composante.read(signature)
if value > 0:
self.signature = fmt.write(value, self.signature)
def build(self, ws: Worksheet, row: int, column: int):
cell = ws.cell(row, column)
cell.value = self.text
FMT.ALL.apply(cell=cell, signature=self.signature)
class Frame_Engine:
def __init__(
self,
ws: ScoExcelSheet,
start_row: int = 1,
start_column: int = 1,
thickness: SCO_BORDERTHICKNESS = SCO_BORDERTHICKNESS.NONE,
color: SCO_COLORS = SCO_COLORS.BLACK,
):
self.start_row: int = start_row
self.start_column: int = start_column
self.ws: ScoExcelSheet = ws
self.border_style = FMT.BORDER_LEFT.make_zero_based_constant([thickness, color])
def close(self, end_row: int, end_column: int):
left_signature: int = FMT.BORDER_LEFT.write(self.border_style)
right_signature: int = FMT.BORDER_RIGHT.write(self.border_style)
top_signature: int = FMT.BORDER_TOP.write(self.border_style)
bottom_signature: int = FMT.BORDER_BOTTOM.write(self.border_style)
for row in range(self.start_row, end_row + 1):
self.ws.cells[row][self.start_column].alter(left_signature)
self.ws.cells[row][end_column].alter(right_signature)
for column in range(self.start_column, end_column + 1):
self.ws.cells[self.start_row][column].alter(top_signature)
self.ws.cells[end_row][column].alter(bottom_signature)
class Merge_Engine:
def __init__(self, start_row: int = 1, start_column: int = 1):
self.start_row: int = start_row
self.start_column: int = start_column
self.end_row: int = None
self.end_column: int = None
self.closed: bool = False
def close(self, end_row=None, end_column=None):
if end_row is None:
self.end_row = self.start_row + 1
else:
self.end_row = end_row + 1
if end_column is None:
self.end_column = self.start_column + 1
else:
self.end_column = end_column + 1
self.closed = True
def write(self, ws: Worksheet):
if self.closed:
if (self.end_row - self.start_row > 0) and (
self.end_column - self.start_column > 0
):
ws.merge_cells(
start_row=self.start_row,
start_column=self.start_column,
end_row=self.end_row - 1,
end_column=self.end_column - 1,
)
def __repr__(self):
return f"( {self.start_row}:{self.start_column}-{self.end_row}:{self.end_column})[{self.closed}]"
def xldate_as_datetime(xldate, datemode=0):
"""Conversion d'une date Excel en datetime python
Deux formats de chaîne acceptés:
* JJ/MM/YYYY (chaîne naïve)
* Date ISO (valeur de type date lue dans la feuille)
Peut lever une ValueError
"""
try:
return datetime.datetime.strptime(xldate, "%d/%m/%Y")
except:
return openpyxl.utils.datetime.from_ISO8601(xldate)
def adjust_sheetname(sheet_name):
"""Renvoie un nom convenable pour une feuille excel: < 31 cars, sans caractères spéciaux
Le / n'est pas autorisé par exemple.
Voir https://xlsxwriter.readthedocs.io/workbook.html#add_worksheet
"""
sheet_name = scu.make_filename(sheet_name)
# Le nom de la feuille ne peut faire plus de 31 caractères.
# si la taille du nom de feuille est > 31 on tronque (on pourrait remplacer par 'feuille' ?)
return sheet_name[:31]
class ScoExcelBook:
"""Permet la génération d'un classeur xlsx composé de plusieurs feuilles.
usage:
wb = ScoExcelBook()
ws0 = wb.create_sheet('sheet name 0')
ws1 = wb.create_sheet('sheet name 1')
...
steam = wb.generate()
"""
def __init__(self):
self.sheets = [] # list of sheets
self.wb = Workbook()
def create_sheet(self, sheet_name="feuille", default_signature: int = 0):
"""Crée une nouvelle feuille dans ce classeur
sheet_name -- le nom de la feuille
default_style -- le style par défaut
"""
sheet_name = adjust_sheetname(sheet_name)
ws = self.wb.create_sheet(sheet_name)
sheet = ScoExcelSheet(sheet_name, default_signature=default_signature, ws=ws)
self.sheets.append(sheet)
return sheet
def generate(self):
"""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
# (https://openpyxl.readthedocs.io/en/stable/tutorial.html#saving-as-a-stream)
with NamedTemporaryFile() as tmp:
self.wb.save(tmp.name)
tmp.seek(0)
return tmp.read()
class ScoExcelSheet:
"""Représente une feuille qui peut être indépendante ou intégrée dans un ScoExcelBook.
En application des directives de la bibliothèque sur l'écriture optimisée, l'ordre des opérations
est imposé:
* instructions globales (largeur/maquage des colonnes et ligne, ...)
* construction et ajout des cellules et ligne selon le sens de lecture (occidental)
ligne de haut en bas et cellules de gauche à droite (i.e. A1, A2, .. B1, B2, ..)
* pour finir appel de la méthode de génération
"""
def __init__(
self,
sheet_name: str = "feuille",
default_signature: int = 0,
ws: Worksheet = None,
):
"""Création de la feuille. sheet_name
-- le nom de la feuille default_style
-- le style par défaut des cellules ws
-- None si la feuille est autonome (dans ce cas elle crée son propre wb), sinon c'est la worksheet
créée par le workbook propriétaire un workbook est créé et associé à cette feuille.
"""
# Le nom de la feuille ne peut faire plus de 31 caractères.
# si la taille du nom de feuille est > 31 on tronque (on pourrait remplacer par 'feuille' ?)
self.sheet_name = adjust_sheetname(sheet_name)
self.default_signature = default_signature
self.merges: list[Merge_Engine] = []
if ws is None:
self.wb = Workbook()
self.ws = self.wb.active
self.ws.title = self.sheet_name
else:
self.wb = None
self.ws = ws
# internal data
self.cells = defaultdict(lambda: defaultdict(Sco_Cell))
self.column_dimensions = {}
self.row_dimensions = {}
def get_frame_engine(
self,
start_row: int,
start_column: int,
thickness: SCO_BORDERTHICKNESS = SCO_BORDERTHICKNESS.NONE,
color: SCO_COLORS = SCO_COLORS.NONE,
):
return Frame_Engine(
ws=self,
start_row=start_row,
start_column=start_column,
thickness=thickness,
color=color,
)
def get_merge_engine(self, start_row: int, start_column: int):
merge_engine: Merge_Engine = Merge_Engine(
start_row=start_row, start_column=start_column
)
self.merges.append(merge_engine)
return merge_engine
@staticmethod
def i2col(idx):
if idx < 26: # one letter key
return chr(idx + 65)
else: # two letters AA..ZZ
first = idx // 26
second = idx % 26
return "" + chr(first + 64) + chr(second + 65)
def set_cell(
self,
row: int,
column: int,
text: str = "",
from_signature: int = 0,
composition: list = [],
):
cell: Sco_Cell = self.cells[row][column]
cell.text = text
cell.alter(FMT.compose(composition, from_signature))
def set_column_dimension_width(self, cle=None, value=21):
"""Détermine la largeur d'une colonne. cle -- identifie la colonne ("A" "B", ... ou 0, 1, 2, ...) si None,
value donne la liste des largeurs de colonnes depuis A, B, C, ... value -- la dimension (unité : 7 pixels
comme affiché dans Excel)
"""
if cle is None:
for i, val in enumerate(value):
self.ws.column_dimensions[self.i2col(i)].width = val
# No keys: value is a list of widths
elif isinstance(cle, str): # accepts set_column_with("D", ...)
self.ws.column_dimensions[cle].width = value
else:
self.ws.column_dimensions[self.i2col(cle)].width = value
def set_row_dimension_height(self, cle=None, value=21):
"""Détermine la hauteur d'une ligne. cle -- identifie la ligne (1, 2, ...) si None,
value donne la liste des hauteurs de colonnes depuis 1, 2, 3, ... value -- la dimension
"""
if cle is None:
for i, val in enumerate(value, start=1):
self.ws.row_dimensions[i].height = val
# No keys: value is a list of widths
else:
self.ws.row_dimensions[cle].height = value
def set_row_dimension_hidden(self, cle, value):
"""Masque ou affiche une ligne.
cle -- identifie la colonne (1...)
value -- boolean (vrai = colonne cachée)
"""
self.ws.row_dimensions[cle].hidden = value
def set_column_dimension_hidden(self, cle, value):
"""Masque ou affiche une ligne.
cle -- identifie la colonne (1...)
value -- boolean (vrai = colonne cachée)
"""
self.ws.column_dimensions[cle].hidden = value
# def make_cell(self, value: any = None, style: Sco_Style = None, comment=None):
# """Construit une cellule.
# value -- contenu de la cellule (texte, numérique, booléen ou date)
# style -- style par défaut (dictionnaire cf. excel_make_style) de la feuille si non spécifié
# """
# # adaptation des valeurs si nécessaire
# if value is None:
# value = ""
# elif value is True:
# value = 1
# elif value is False:
# value = 0
# elif isinstance(value, datetime.datetime):
# value = value.replace(
# tzinfo=None
# ) # make date naive (cf https://openpyxl.readthedocs.io/en/latest/datetime.html#timezones)
#
# # création de la cellule
# cell = WriteOnlyCell(self.ws, value)
#
# if style is not None:
# style.apply(cell)
#
# if not comment is None:
# cell.comment = Comment(comment, "scodoc")
# lines = comment.splitlines()
# cell.comment.width = 7 * max([len(line) for line in lines]) if lines else 7
# cell.comment.height = 20 * len(lines) if lines else 20
#
# # test datatype to overwrite datetime format
# if isinstance(value, datetime.date):
# cell.data_type = "d"
# cell.number_format = FORMAT_DATE_DDMMYY
# elif isinstance(value, int) or isinstance(value, float):
# cell.data_type = "n"
# else:
# cell.data_type = "s"
#
# return cell
def prepare(self):
"""génére un flux décrivant la feuille.
Ce flux pourra ensuite être repris dans send_excel_file (classeur mono feille)
ou pour la génération d'un classeur multi-feuilles
"""
# for row in self.column_dimensions.keys():
# self.ws.column_dimensions[row] = self.column_dimensions[row]
# for row in self.row_dimensions.keys():
# self.ws.row_dimensions[row] = self.row_dimensions[row]
# for row in self.rows:
# self.ws.append(row)
for row in self.cells:
for column in self.cells[row]:
self.cells[row][column].build(self.ws, row, column)
for merge_engine in self.merges:
merge_engine.write(self.ws)
def generate(self):
"""génération d'un classeur mono-feuille"""
# this method makes sense only if it is a standalone worksheet (else call workbook.generate()
if self.wb is None: # embeded sheet
raise ScoValueError("can't generate a single sheet from a ScoWorkbook")
# construction d'un flux (https://openpyxl.readthedocs.io/en/stable/tutorial.html#saving-as-a-stream)
self.prepare()
with NamedTemporaryFile() as tmp:
self.wb.save(tmp.name)
tmp.seek(0)
return tmp.read()