2021-08-11 11:40:28 +02:00
|
|
|
# -*- mode: python -*-
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
##############################################################################
|
|
|
|
#
|
|
|
|
# Gestion scolarite IUT
|
|
|
|
#
|
2023-12-31 23:04:06 +01:00
|
|
|
# Copyright (c) 1999 - 2024 Emmanuel Viennet. All rights reserved.
|
2021-08-11 11:40:28 +02:00
|
|
|
#
|
|
|
|
# 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
|
|
|
|
#
|
|
|
|
##############################################################################
|
|
|
|
|
|
|
|
|
|
|
|
""" Excel file handling
|
|
|
|
"""
|
|
|
|
import datetime
|
|
|
|
import io
|
|
|
|
import time
|
|
|
|
from enum import Enum
|
|
|
|
from tempfile import NamedTemporaryFile
|
|
|
|
|
2021-08-14 10:12:40 +02:00
|
|
|
import openpyxl.utils.datetime
|
2021-10-05 05:38:04 +02:00
|
|
|
from openpyxl.styles.numbers import FORMAT_NUMBER_00, FORMAT_GENERAL, FORMAT_DATE_DDMMYY
|
2021-08-22 13:24:36 +02:00
|
|
|
from openpyxl.comments import Comment
|
2021-09-05 14:50:35 +02:00
|
|
|
from openpyxl import Workbook, load_workbook
|
|
|
|
from openpyxl.cell import WriteOnlyCell
|
|
|
|
from openpyxl.styles import Font, Border, Side, Alignment, PatternFill
|
2022-07-07 11:56:18 +02:00
|
|
|
from openpyxl.worksheet.worksheet import Worksheet
|
2021-08-11 11:40:28 +02:00
|
|
|
|
|
|
|
import app.scodoc.sco_utils as scu
|
2021-08-29 19:57:32 +02:00
|
|
|
from app import log
|
2021-08-11 11:40:28 +02:00
|
|
|
from app.scodoc.sco_exceptions import ScoValueError
|
2022-07-08 06:32:43 +02:00
|
|
|
from app.scodoc import notesdb, sco_preferences
|
2021-08-11 11:40:28 +02:00
|
|
|
|
|
|
|
|
|
|
|
class COLORS(Enum):
|
|
|
|
BLACK = "FF000000"
|
|
|
|
WHITE = "FFFFFFFF"
|
|
|
|
RED = "FFFF0000"
|
|
|
|
BROWN = "FF993300"
|
|
|
|
PURPLE = "FF993366"
|
|
|
|
BLUE = "FF0000FF"
|
|
|
|
ORANGE = "FFFF3300"
|
|
|
|
LIGHT_YELLOW = "FFFFFF99"
|
|
|
|
|
|
|
|
|
2022-06-30 23:49:39 +02:00
|
|
|
# Un style est enregistré comme un dictionnaire qui précise la valeur d'un attribut dans la liste suivante:
|
2021-09-18 01:30:47 +02:00
|
|
|
# font, border, number_format, fill,...
|
|
|
|
# (cf https://openpyxl.readthedocs.io/en/stable/styles.html#working-with-styles)
|
2021-08-11 11:40:28 +02:00
|
|
|
|
|
|
|
|
|
|
|
def xldate_as_datetime(xldate, datemode=0):
|
2021-10-05 12:09:20 +02:00
|
|
|
"""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)
|
2021-09-08 00:10:36 +02:00
|
|
|
Peut lever une ValueError
|
|
|
|
"""
|
2021-10-05 12:09:20 +02:00
|
|
|
try:
|
2024-04-02 23:37:23 +02:00
|
|
|
return datetime.datetime.strptime(xldate, scu.DATE_FMT)
|
2021-10-05 12:09:20 +02:00
|
|
|
except:
|
|
|
|
return openpyxl.utils.datetime.from_ISO8601(xldate)
|
2021-09-05 14:50:35 +02:00
|
|
|
|
|
|
|
|
|
|
|
def adjust_sheetname(sheet_name):
|
2021-09-20 15:54:38 +02:00
|
|
|
"""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)
|
2021-09-05 14:50:35 +02:00
|
|
|
# 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]
|
2021-08-11 11:40:28 +02:00
|
|
|
|
|
|
|
|
|
|
|
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()
|
|
|
|
"""
|
2021-08-12 14:49:53 +02:00
|
|
|
|
2021-08-11 11:40:28 +02:00
|
|
|
def __init__(self):
|
|
|
|
self.sheets = [] # list of sheets
|
2021-09-05 14:50:35 +02:00
|
|
|
self.wb = Workbook(write_only=True)
|
2021-08-11 11:40:28 +02:00
|
|
|
|
|
|
|
def create_sheet(self, sheet_name="feuille", default_style=None):
|
2021-08-13 12:18:22 +02:00
|
|
|
"""Crée une nouvelle feuille dans ce classeur
|
|
|
|
sheet_name -- le nom de la feuille
|
|
|
|
default_style -- le style par défaut
|
|
|
|
"""
|
2021-09-05 14:50:35 +02:00
|
|
|
sheet_name = adjust_sheetname(sheet_name)
|
|
|
|
ws = self.wb.create_sheet(sheet_name)
|
|
|
|
sheet = ScoExcelSheet(sheet_name, default_style, ws)
|
2021-08-11 11:40:28 +02:00
|
|
|
self.sheets.append(sheet)
|
2021-08-13 12:18:22 +02:00
|
|
|
return sheet
|
2021-08-11 11:40:28 +02:00
|
|
|
|
|
|
|
def generate(self):
|
2021-08-12 14:49:53 +02:00
|
|
|
"""génération d'un stream binaire représentant la totalité du classeur.
|
2021-08-11 11:40:28 +02:00
|
|
|
retourne le flux
|
|
|
|
"""
|
|
|
|
for sheet in self.sheets:
|
2021-09-05 14:50:35 +02:00
|
|
|
sheet.prepare()
|
2021-09-18 01:30:47 +02:00
|
|
|
# construction d'un flux
|
|
|
|
# (https://openpyxl.readthedocs.io/en/stable/tutorial.html#saving-as-a-stream)
|
2021-08-11 11:40:28 +02:00
|
|
|
with NamedTemporaryFile() as tmp:
|
2021-09-05 14:50:35 +02:00
|
|
|
self.wb.save(tmp.name)
|
2021-08-11 11:40:28 +02:00
|
|
|
tmp.seek(0)
|
|
|
|
return tmp.read()
|
|
|
|
|
|
|
|
|
|
|
|
def excel_make_style(
|
2021-09-17 10:26:20 +02:00
|
|
|
bold=False,
|
|
|
|
italic=False,
|
|
|
|
outline=False,
|
|
|
|
color: COLORS = COLORS.BLACK,
|
|
|
|
bgcolor: COLORS = None,
|
|
|
|
halign=None,
|
|
|
|
valign=None,
|
|
|
|
number_format=None,
|
|
|
|
font_name="Arial",
|
|
|
|
size=10,
|
2021-08-11 11:40:28 +02:00
|
|
|
):
|
|
|
|
"""Contruit un style.
|
|
|
|
Les couleurs peuvent être spécfiées soit par une valeur de COLORS,
|
|
|
|
soit par une chaine argb (exple "FF00FF00" pour le vert)
|
|
|
|
color -- La couleur du texte
|
|
|
|
bgcolor -- la couleur de fond
|
|
|
|
halign -- alignement horizontal ("left", "right", "center")
|
|
|
|
valign -- alignement vertical ("top", "bottom", "center")
|
2021-08-23 17:47:49 +02:00
|
|
|
number_format -- formattage du contenu ("General", "@", ...)
|
2021-08-13 12:18:22 +02:00
|
|
|
font_name -- police
|
|
|
|
size -- taille de police
|
2021-08-11 11:40:28 +02:00
|
|
|
"""
|
|
|
|
style = {}
|
2021-09-05 14:50:35 +02:00
|
|
|
font = Font(
|
|
|
|
name=font_name,
|
|
|
|
bold=bold,
|
|
|
|
italic=italic,
|
|
|
|
outline=outline,
|
|
|
|
color=color.value,
|
|
|
|
size=size,
|
|
|
|
)
|
2021-08-11 11:40:28 +02:00
|
|
|
style["font"] = font
|
|
|
|
if bgcolor:
|
2021-08-13 12:18:22 +02:00
|
|
|
style["fill"] = PatternFill(fill_type="solid", fgColor=bgcolor.value)
|
2021-08-11 11:40:28 +02:00
|
|
|
if halign or valign:
|
|
|
|
al = Alignment()
|
|
|
|
if halign:
|
2021-08-13 12:18:22 +02:00
|
|
|
al.horizontal = {
|
2021-08-11 11:40:28 +02:00
|
|
|
"left": "left",
|
|
|
|
"right": "right",
|
|
|
|
"center": "center",
|
|
|
|
}[halign]
|
|
|
|
if valign:
|
2021-08-13 12:18:22 +02:00
|
|
|
al.vertical = {
|
2021-08-11 11:40:28 +02:00
|
|
|
"top": "top",
|
|
|
|
"bottom": "bottom",
|
|
|
|
"center": "center",
|
|
|
|
}[valign]
|
|
|
|
style["alignment"] = al
|
2021-08-23 17:47:49 +02:00
|
|
|
if number_format is None:
|
|
|
|
style["number_format"] = FORMAT_GENERAL
|
2021-08-11 11:40:28 +02:00
|
|
|
else:
|
2021-08-23 17:47:49 +02:00
|
|
|
style["number_format"] = number_format
|
2021-08-11 11:40:28 +02:00
|
|
|
return style
|
|
|
|
|
|
|
|
|
|
|
|
class ScoExcelSheet:
|
2022-04-06 18:51:01 +02:00
|
|
|
"""Représente une feuille qui peut être indépendante ou intégrée dans un ScoExcelBook.
|
2021-08-11 11:40:28 +02:00
|
|
|
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, ..)
|
2022-04-06 18:51:01 +02:00
|
|
|
* pour finir appel de la méthode de génération
|
2021-08-11 11:40:28 +02:00
|
|
|
"""
|
2021-08-12 14:49:53 +02:00
|
|
|
|
2023-02-06 10:58:36 +01:00
|
|
|
def __init__(self, sheet_name="feuille", default_style=None, wb: Workbook = None):
|
2021-09-05 14:50:35 +02:00
|
|
|
"""Création de la feuille. sheet_name
|
|
|
|
-- le nom de la feuille default_style
|
|
|
|
-- le style par défaut des cellules ws
|
2023-02-06 10:58:36 +01:00
|
|
|
-- 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.
|
2021-08-11 11:40:28 +02:00
|
|
|
"""
|
2021-08-22 06:07:02 +02:00
|
|
|
# Le nom de la feuille ne peut faire plus de 31 caractères.
|
2021-08-22 07:48:40 +02:00
|
|
|
# si la taille du nom de feuille est > 31 on tronque (on pourrait remplacer par 'feuille' ?)
|
2021-09-05 14:50:35 +02:00
|
|
|
self.sheet_name = adjust_sheetname(sheet_name)
|
2021-08-11 11:40:28 +02:00
|
|
|
if default_style is None:
|
|
|
|
default_style = excel_make_style()
|
|
|
|
self.default_style = default_style
|
2021-09-17 15:59:47 +02:00
|
|
|
if wb is None:
|
2021-09-05 14:50:35 +02:00
|
|
|
self.wb = Workbook()
|
|
|
|
self.ws = self.wb.active
|
|
|
|
self.ws.title = self.sheet_name
|
|
|
|
else:
|
|
|
|
self.wb = None
|
2021-09-17 15:59:47 +02:00
|
|
|
self.ws = wb
|
2021-09-05 14:50:35 +02:00
|
|
|
# internal data
|
|
|
|
self.rows = [] # list of list of cells
|
2021-08-11 11:40:28 +02:00
|
|
|
self.column_dimensions = {}
|
2021-09-05 14:50:35 +02:00
|
|
|
self.row_dimensions = {}
|
2023-12-03 15:19:35 +01:00
|
|
|
self.auto_filter = None
|
2021-09-05 14:50:35 +02:00
|
|
|
|
|
|
|
def excel_make_composite_style(
|
2021-09-17 10:26:20 +02:00
|
|
|
self,
|
|
|
|
alignment=None,
|
|
|
|
border=None,
|
|
|
|
fill=None,
|
|
|
|
number_format=None,
|
|
|
|
font=None,
|
2021-09-05 14:50:35 +02:00
|
|
|
):
|
|
|
|
style = {}
|
|
|
|
if font is not None:
|
|
|
|
style["font"] = font
|
|
|
|
if alignment is not None:
|
|
|
|
style["alignment"] = alignment
|
|
|
|
if border is not None:
|
|
|
|
style["border"] = border
|
|
|
|
if fill is not None:
|
|
|
|
style["fill"] = fill
|
|
|
|
if number_format is None:
|
|
|
|
style["number_format"] = FORMAT_GENERAL
|
|
|
|
else:
|
|
|
|
style["number_format"] = number_format
|
|
|
|
return style
|
|
|
|
|
|
|
|
@staticmethod
|
2024-06-28 19:03:20 +02:00
|
|
|
def i2col(idx: int | str) -> str:
|
|
|
|
"traduit un index ou lettre de colonne en lettre de colonne"
|
|
|
|
if isinstance(idx, str):
|
|
|
|
return idx
|
2021-09-05 14:50:35 +02:00
|
|
|
if idx < 26: # one letter key
|
|
|
|
return chr(idx + 65)
|
|
|
|
else: # two letters AA..ZZ
|
|
|
|
first = (idx // 26) + 66
|
|
|
|
second = (idx % 26) + 65
|
|
|
|
return "" + chr(first) + chr(second)
|
|
|
|
|
2024-06-28 19:03:20 +02:00
|
|
|
def set_column_dimension_width(self, cle=None, value: int | str | list = 21):
|
|
|
|
"""Détermine la largeur d'une colonne.
|
|
|
|
cle -- identifie la colonne (lettre ou indice à partir de 0),
|
|
|
|
Si cle is None, affecte toutes les colonnes.
|
|
|
|
|
|
|
|
value est soit la liste des largeurs de colonnes [ (0, width0), (1, width1), ...]
|
|
|
|
soit la largeur de la colonne indiquée par cle, soit "auto".
|
|
|
|
Largeurs en unité : 7 pixels comme affiché dans Excel)
|
|
|
|
ou value == "auto", ajuste la largeur au contenu
|
2021-09-05 14:50:35 +02:00
|
|
|
"""
|
|
|
|
if cle is None:
|
2024-06-28 19:03:20 +02:00
|
|
|
cols_widths = enumerate(value)
|
2021-09-05 14:50:35 +02:00
|
|
|
else:
|
2024-06-28 19:03:20 +02:00
|
|
|
cols_widths = [(cle, value)]
|
|
|
|
|
|
|
|
for idx, width in cols_widths:
|
|
|
|
if width == "auto":
|
|
|
|
self.adjust_column_widths(column_letter=self.i2col(idx))
|
|
|
|
else:
|
|
|
|
self.ws.column_dimensions[self.i2col(idx)].width = width
|
2021-08-11 11:40:28 +02:00
|
|
|
|
2021-09-05 14:50:35 +02:00
|
|
|
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
|
2021-08-11 11:40:28 +02:00
|
|
|
"""
|
2021-09-05 14:50:35 +02:00
|
|
|
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
|
2021-08-11 11:40:28 +02:00
|
|
|
|
2021-09-05 14:50:35 +02:00
|
|
|
def set_row_dimension_hidden(self, cle, value):
|
|
|
|
"""Masque ou affiche une ligne.
|
|
|
|
cle -- identifie la colonne (1...)
|
2021-08-11 11:40:28 +02:00
|
|
|
value -- boolean (vrai = colonne cachée)
|
|
|
|
"""
|
2021-09-05 14:50:35 +02:00
|
|
|
self.ws.row_dimensions[cle].hidden = value
|
2021-08-11 11:40:28 +02:00
|
|
|
|
2023-12-03 00:17:16 +01:00
|
|
|
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
|
|
|
|
|
2024-06-28 19:03:20 +02:00
|
|
|
def set_auto_filter(self, filter_range):
|
|
|
|
"met en place un auto-filter excel: le range désigne les cellules de titres"
|
|
|
|
self.auto_filter = filter_range
|
2023-12-03 15:19:35 +01:00
|
|
|
|
2024-06-28 19:03:20 +02:00
|
|
|
def adjust_column_widths(
|
|
|
|
self, column_letter=None, min_row=None, max_row=None, min_col=None, max_col=None
|
|
|
|
):
|
|
|
|
"""Adjust columns widths to fit their content.
|
|
|
|
If column_letter, adjust only this column, else adjust all.
|
|
|
|
(min_row, max_row, min_col, max_col) can be used to restrinct the area to consider
|
|
|
|
while determining the widths.
|
|
|
|
"""
|
|
|
|
# Create a dictionary to store the maximum width of each column
|
|
|
|
col_widths = {}
|
|
|
|
|
|
|
|
if column_letter is None:
|
|
|
|
# Iterate over each row and cell in the worksheet
|
|
|
|
for row in self.ws.iter_rows(
|
|
|
|
min_row=min_row, max_row=max_row, min_col=min_col, max_col=max_col
|
|
|
|
):
|
|
|
|
for cell in row:
|
|
|
|
# Get the length of the cell value (converted to string)
|
|
|
|
cell_value = str(cell.value)
|
|
|
|
# Update the maximum width for the column
|
|
|
|
col_widths[cell.column_letter] = max(
|
|
|
|
col_widths.get(cell.column_letter, 0), len(cell_value)
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
min_row = self.ws.min_row if min_row is None else min_row
|
|
|
|
max_row = self.ws.max_row if max_row is None else max_row
|
|
|
|
for row in range(min_row, max_row + 1):
|
|
|
|
cell = self.ws[f"{column_letter}{row}"]
|
|
|
|
cell_value = str(cell.value)
|
|
|
|
col_widths[cell.column_letter] = max(
|
|
|
|
col_widths.get(cell.column_letter, 0), len(cell_value)
|
|
|
|
)
|
|
|
|
|
|
|
|
# Set the column widths based on the maximum length found
|
|
|
|
for col, width in col_widths.items():
|
|
|
|
self.ws.column_dimensions[col].width = width
|
|
|
|
|
|
|
|
def make_cell(self, value: any = None, style: dict = None, comment=None):
|
2021-08-11 11:40:28 +02:00
|
|
|
"""Construit une cellule.
|
2021-10-16 10:10:35 +02:00
|
|
|
value -- contenu de la cellule (texte, numérique, booléen ou date)
|
2021-08-13 12:18:22 +02:00
|
|
|
style -- style par défaut (dictionnaire cf. excel_make_style) de la feuille si non spécifié
|
2021-08-11 11:40:28 +02:00
|
|
|
"""
|
2022-06-30 23:49:39 +02:00
|
|
|
# adaptation des valeurs si nécessaire
|
2021-10-16 14:30:35 +02:00
|
|
|
if value is None:
|
|
|
|
value = ""
|
|
|
|
elif value is True:
|
|
|
|
value = 1
|
|
|
|
elif value is False:
|
|
|
|
value = 0
|
|
|
|
elif isinstance(value, datetime.datetime):
|
2024-06-28 19:03:20 +02:00
|
|
|
# make date naive (cf https://openpyxl.readthedocs.io/en/latest/datetime.html#timezones)
|
|
|
|
value = value.replace(tzinfo=None)
|
2021-10-16 14:30:35 +02:00
|
|
|
|
|
|
|
# création de la cellule
|
|
|
|
cell = WriteOnlyCell(self.ws, value)
|
|
|
|
|
|
|
|
# recopie des styles
|
2021-08-11 11:40:28 +02:00
|
|
|
if style is None:
|
|
|
|
style = self.default_style
|
|
|
|
if "font" in style:
|
|
|
|
cell.font = style["font"]
|
2021-09-05 14:50:35 +02:00
|
|
|
if "alignment" in style:
|
|
|
|
cell.alignment = style["alignment"]
|
2021-08-11 11:40:28 +02:00
|
|
|
if "border" in style:
|
|
|
|
cell.border = style["border"]
|
2021-09-05 14:50:35 +02:00
|
|
|
if "fill" in style:
|
|
|
|
cell.fill = style["fill"]
|
2021-08-11 11:40:28 +02:00
|
|
|
if "number_format" in style:
|
|
|
|
cell.number_format = style["number_format"]
|
|
|
|
if "fill" in style:
|
|
|
|
cell.fill = style["fill"]
|
|
|
|
if "alignment" in style:
|
|
|
|
cell.alignment = style["alignment"]
|
2021-08-22 13:24:36 +02:00
|
|
|
if not comment is None:
|
|
|
|
cell.comment = Comment(comment, "scodoc")
|
2021-08-22 19:17:43 +02:00
|
|
|
lines = comment.splitlines()
|
2023-03-02 22:55:25 +01:00
|
|
|
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
|
2021-10-16 14:30:35 +02:00
|
|
|
|
2021-10-17 20:20:31 +02:00
|
|
|
# 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"
|
|
|
|
|
2021-08-11 11:40:28 +02:00
|
|
|
return cell
|
|
|
|
|
2022-04-06 18:51:01 +02:00
|
|
|
def make_row(self, values: list, style=None, comments=None) -> list:
|
|
|
|
"build a row"
|
2021-08-22 13:24:36 +02:00
|
|
|
# TODO make possible differents styles in a row
|
|
|
|
if comments is None:
|
|
|
|
comments = [None] * len(values)
|
|
|
|
return [
|
|
|
|
self.make_cell(value, style, comment)
|
|
|
|
for value, comment in zip(values, comments)
|
|
|
|
]
|
2021-08-11 11:40:28 +02:00
|
|
|
|
2024-06-28 19:03:20 +02:00
|
|
|
def append_single_cell_row(self, value: any, style=None, prefix=None):
|
2021-08-11 11:40:28 +02:00
|
|
|
"""construit une ligne composée d'une seule cellule et l'ajoute à la feuille.
|
|
|
|
mêmes paramètres que make_cell:
|
|
|
|
value -- contenu de la cellule (texte ou numérique)
|
|
|
|
style -- style par défaut de la feuille si non spécifié
|
2024-06-28 19:03:20 +02:00
|
|
|
prefix -- cellules ajoutées au début de la ligne
|
2021-08-11 11:40:28 +02:00
|
|
|
"""
|
2024-06-28 19:03:20 +02:00
|
|
|
self.append_row((prefix or []) + [self.make_cell(value, style)])
|
2021-08-11 11:40:28 +02:00
|
|
|
|
|
|
|
def append_blank_row(self):
|
|
|
|
"""construit une ligne vide et l'ajoute à la feuille."""
|
|
|
|
self.append_row([None])
|
|
|
|
|
|
|
|
def append_row(self, row):
|
|
|
|
"""ajoute une ligne déjà construite à la feuille."""
|
|
|
|
self.rows.append(row)
|
|
|
|
|
2021-09-05 14:50:35 +02:00
|
|
|
def prepare(self):
|
2021-08-11 11:40:28 +02:00
|
|
|
"""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
|
|
|
|
"""
|
2021-09-18 11:08:04 +02:00
|
|
|
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]
|
2021-08-11 11:40:28 +02:00
|
|
|
for row in self.rows:
|
|
|
|
self.ws.append(row)
|
|
|
|
|
2024-06-28 19:03:20 +02:00
|
|
|
def generate(self, column_widths=None):
|
2021-08-11 11:40:28 +02:00
|
|
|
"""génération d'un classeur mono-feuille"""
|
2024-06-28 19:03:20 +02:00
|
|
|
# this method makes sense for standalone worksheet (else call workbook.generate())
|
2021-09-05 14:50:35 +02:00
|
|
|
if self.wb is None: # embeded sheet
|
|
|
|
raise ScoValueError("can't generate a single sheet from a ScoWorkbook")
|
|
|
|
|
2024-06-28 19:03:20 +02:00
|
|
|
# construction d'un flux
|
|
|
|
# https://openpyxl.readthedocs.io/en/stable/tutorial.html#saving-as-a-stream
|
2021-09-05 14:50:35 +02:00
|
|
|
self.prepare()
|
2024-06-28 19:03:20 +02:00
|
|
|
|
|
|
|
# largeur des colonnes
|
|
|
|
if column_widths:
|
|
|
|
for k, v in column_widths.items():
|
|
|
|
self.set_column_dimension_width(k, v)
|
|
|
|
|
2023-12-03 15:19:35 +01:00
|
|
|
if self.auto_filter is not None:
|
|
|
|
self.ws.auto_filter.ref = self.auto_filter
|
2021-08-11 11:40:28 +02:00
|
|
|
with NamedTemporaryFile() as tmp:
|
|
|
|
self.wb.save(tmp.name)
|
|
|
|
tmp.seek(0)
|
|
|
|
return tmp.read()
|
|
|
|
|
|
|
|
|
|
|
|
def excel_simple_table(
|
2023-03-02 22:55:25 +01:00
|
|
|
titles: list[str] = None,
|
|
|
|
lines: list[list[str]] = None,
|
|
|
|
sheet_name: str = "feuille",
|
|
|
|
titles_styles=None,
|
|
|
|
comments=None,
|
2021-08-11 11:40:28 +02:00
|
|
|
):
|
2023-03-02 22:55:25 +01:00
|
|
|
"""Export simple type 'CSV': 1ere ligne en gras, le reste tel quel."""
|
2021-08-11 11:40:28 +02:00
|
|
|
ws = ScoExcelSheet(sheet_name)
|
|
|
|
if titles is None:
|
|
|
|
titles = []
|
|
|
|
if lines is None:
|
|
|
|
lines = [[]]
|
|
|
|
if titles_styles is None:
|
|
|
|
style = excel_make_style(bold=True)
|
|
|
|
titles_styles = [style] * len(titles)
|
2021-08-22 13:24:36 +02:00
|
|
|
if comments is None:
|
|
|
|
comments = [None] * len(titles)
|
2021-08-11 11:40:28 +02:00
|
|
|
# ligne de titres
|
|
|
|
ws.append_row(
|
2021-08-22 13:24:36 +02:00
|
|
|
[
|
|
|
|
ws.make_cell(it, style, comment)
|
|
|
|
for (it, style, comment) in zip(titles, titles_styles, comments)
|
|
|
|
]
|
2021-08-11 11:40:28 +02:00
|
|
|
)
|
|
|
|
default_style = excel_make_style()
|
2021-08-23 17:47:49 +02:00
|
|
|
text_style = excel_make_style(number_format=FORMAT_GENERAL)
|
|
|
|
int_style = excel_make_style()
|
|
|
|
float_style = excel_make_style(number_format=FORMAT_NUMBER_00)
|
2021-08-11 11:40:28 +02:00
|
|
|
for line in lines:
|
|
|
|
cells = []
|
|
|
|
for it in line:
|
|
|
|
cell_style = default_style
|
2023-03-02 22:55:25 +01:00
|
|
|
if isinstance(it, float):
|
2021-08-23 17:47:49 +02:00
|
|
|
cell_style = float_style
|
2023-03-02 22:55:25 +01:00
|
|
|
elif isinstance(it, int):
|
2021-08-23 17:47:49 +02:00
|
|
|
cell_style = int_style
|
|
|
|
else:
|
2021-08-11 11:40:28 +02:00
|
|
|
cell_style = text_style
|
|
|
|
cells.append(ws.make_cell(it, cell_style))
|
|
|
|
ws.append_row(cells)
|
2021-09-05 14:50:35 +02:00
|
|
|
return ws.generate()
|
2021-08-11 11:40:28 +02:00
|
|
|
|
|
|
|
|
|
|
|
def excel_bytes_to_list(bytes_content):
|
2021-09-21 16:28:26 +02:00
|
|
|
try:
|
|
|
|
filelike = io.BytesIO(bytes_content)
|
2022-07-07 11:56:18 +02:00
|
|
|
except Exception as exc:
|
2021-09-28 07:22:23 +02:00
|
|
|
raise ScoValueError(
|
2021-09-29 20:08:18 +02:00
|
|
|
"""Le fichier xlsx attendu n'est pas lisible !
|
|
|
|
Peut-être avez-vous fourni un fichier au mauvais format (txt, xls, ..)
|
2021-09-28 07:22:23 +02:00
|
|
|
"""
|
2022-07-07 11:56:18 +02:00
|
|
|
) from exc
|
|
|
|
return _excel_to_list(filelike)
|
2021-08-11 11:40:28 +02:00
|
|
|
|
|
|
|
|
|
|
|
def excel_file_to_list(filename):
|
2021-09-21 16:28:26 +02:00
|
|
|
try:
|
|
|
|
return _excel_to_list(filename)
|
2022-07-07 11:56:18 +02:00
|
|
|
except Exception as exc:
|
2021-09-28 07:22:23 +02:00
|
|
|
raise ScoValueError(
|
2021-09-29 20:08:18 +02:00
|
|
|
"""Le fichier xlsx attendu n'est pas lisible !
|
|
|
|
Peut-être avez-vous fourni un fichier au mauvais format (txt, xls, ...)
|
|
|
|
"""
|
2022-07-07 11:56:18 +02:00
|
|
|
) from exc
|
2021-08-11 11:40:28 +02:00
|
|
|
|
|
|
|
|
2022-07-07 11:59:47 +02:00
|
|
|
def excel_workbook_to_list(filename):
|
2022-06-02 21:49:37 +02:00
|
|
|
try:
|
2022-07-07 11:59:47 +02:00
|
|
|
return _excel_workbook_to_list(filename)
|
2022-07-07 11:56:18 +02:00
|
|
|
except Exception as exc:
|
2022-06-02 21:49:37 +02:00
|
|
|
raise ScoValueError(
|
|
|
|
"""Le fichier xlsx attendu n'est pas lisible !
|
|
|
|
Peut-être avez-vous fourni un fichier au mauvais format (txt, xls, ...)
|
|
|
|
"""
|
2022-07-07 11:56:18 +02:00
|
|
|
) from exc
|
2022-06-02 21:49:37 +02:00
|
|
|
|
|
|
|
|
2022-07-07 11:56:18 +02:00
|
|
|
def _open_workbook(filelike, dump_debug=False) -> Workbook:
|
|
|
|
"""Open document.
|
|
|
|
On error, if dump-debug is True, dump data in /tmp for debugging purpose
|
2021-08-11 11:40:28 +02:00
|
|
|
"""
|
|
|
|
try:
|
2022-07-07 11:56:18 +02:00
|
|
|
workbook = load_workbook(filename=filelike, read_only=True, data_only=True)
|
|
|
|
except Exception as exc:
|
2021-08-11 11:40:28 +02:00
|
|
|
log("Excel_to_list: failure to import document")
|
2022-07-07 11:56:18 +02:00
|
|
|
if dump_debug:
|
|
|
|
dump_filename = "/tmp/last_scodoc_import_failure" + scu.XLSX_SUFFIX
|
|
|
|
log(f"Dumping problemetic file on {dump_filename}")
|
|
|
|
with open(dump_filename, "wb") as f:
|
|
|
|
f.write(filelike)
|
2021-08-11 11:40:28 +02:00
|
|
|
raise ScoValueError(
|
2022-07-07 11:56:18 +02:00
|
|
|
"Fichier illisible: assurez-vous qu'il s'agit bien d'un document Excel xlsx !"
|
|
|
|
) from exc
|
|
|
|
return workbook
|
|
|
|
|
|
|
|
|
|
|
|
def _excel_to_list(filelike):
|
|
|
|
"""returns list of list"""
|
|
|
|
workbook = _open_workbook(filelike)
|
2021-08-11 11:40:28 +02:00
|
|
|
diag = [] # liste de chaines pour former message d'erreur
|
2023-10-17 22:17:44 +02:00
|
|
|
if len(workbook.sheetnames) < 1:
|
2021-08-11 11:40:28 +02:00
|
|
|
diag.append("Aucune feuille trouvée dans le classeur !")
|
|
|
|
return diag, None
|
2022-07-07 11:56:18 +02:00
|
|
|
# n'utilise que la première feuille:
|
2023-10-17 22:17:44 +02:00
|
|
|
if len(workbook.sheetnames) > 1:
|
2021-08-11 11:40:28 +02:00
|
|
|
diag.append("Attention: n'utilise que la première feuille du classeur !")
|
2023-10-17 22:17:44 +02:00
|
|
|
sheet_name = workbook.sheetnames[0]
|
2022-07-07 11:56:18 +02:00
|
|
|
ws = workbook[sheet_name]
|
2022-07-07 15:30:50 +02:00
|
|
|
diag_sheet, matrix = _excel_sheet_to_list(ws, sheet_name)
|
2022-07-07 11:56:18 +02:00
|
|
|
diag += diag_sheet
|
|
|
|
return diag, matrix
|
|
|
|
|
|
|
|
|
|
|
|
def _excel_sheet_to_list(sheet: Worksheet, sheet_name: str) -> tuple[list, list]:
|
|
|
|
"""read a spreadsheet sheet, and returns:
|
|
|
|
- diag : a list of strings (error messages aimed at helping the user)
|
|
|
|
- a list of lists: the spreadsheet cells
|
|
|
|
"""
|
|
|
|
diag = []
|
2021-08-11 11:40:28 +02:00
|
|
|
# fill matrix
|
|
|
|
values = {}
|
2022-07-07 11:56:18 +02:00
|
|
|
for row in sheet.iter_rows():
|
2021-08-11 11:40:28 +02:00
|
|
|
for cell in row:
|
|
|
|
if cell.value is not None:
|
|
|
|
values[(cell.row - 1, cell.column - 1)] = str(cell.value)
|
|
|
|
if not values:
|
2022-07-07 11:56:18 +02:00
|
|
|
diag.append(f"Aucune valeur trouvée dans la feuille {sheet_name} !")
|
2021-08-11 11:40:28 +02:00
|
|
|
return diag, None
|
|
|
|
indexes = list(values.keys())
|
|
|
|
# search numbers of rows and cols
|
|
|
|
rows = [x[0] for x in indexes]
|
|
|
|
cols = [x[1] for x in indexes]
|
|
|
|
nbcols = max(cols) + 1
|
|
|
|
nbrows = max(rows) + 1
|
2022-07-07 11:56:18 +02:00
|
|
|
matrix = []
|
2021-08-11 11:40:28 +02:00
|
|
|
for _ in range(nbrows):
|
2022-07-07 11:56:18 +02:00
|
|
|
matrix.append([""] * nbcols)
|
2021-08-11 11:40:28 +02:00
|
|
|
|
|
|
|
for row_idx, col_idx in indexes:
|
|
|
|
v = values[(row_idx, col_idx)]
|
2022-07-07 11:56:18 +02:00
|
|
|
matrix[row_idx][col_idx] = v
|
|
|
|
diag.append(f'Feuille "{sheet_name}", {len(matrix)} lignes')
|
|
|
|
|
|
|
|
return diag, matrix
|
2021-08-11 11:40:28 +02:00
|
|
|
|
|
|
|
|
2022-07-07 11:56:18 +02:00
|
|
|
def _excel_workbook_to_list(filelike):
|
|
|
|
"""Lit un classeur (workbook): chaque feuille est lue
|
|
|
|
et est convertie en une liste de listes.
|
|
|
|
Returns:
|
|
|
|
- diag : a list of strings (error messages aimed at helping the user)
|
|
|
|
- a list of lists: the spreadsheet cells
|
2022-06-02 21:49:37 +02:00
|
|
|
"""
|
2022-07-07 11:56:18 +02:00
|
|
|
workbook = _open_workbook(filelike)
|
2022-06-02 21:49:37 +02:00
|
|
|
diag = [] # liste de chaines pour former message d'erreur
|
2023-10-17 22:17:44 +02:00
|
|
|
if len(workbook.sheetnames) < 1:
|
2022-06-02 21:49:37 +02:00
|
|
|
diag.append("Aucune feuille trouvée dans le classeur !")
|
|
|
|
return diag, None
|
2022-07-07 11:56:18 +02:00
|
|
|
matrix_list = []
|
2023-10-17 22:17:44 +02:00
|
|
|
for sheet_name in workbook.sheetnames:
|
2022-06-02 21:49:37 +02:00
|
|
|
# fill matrix
|
2022-07-07 11:56:18 +02:00
|
|
|
sheet = workbook.get_sheet_by_name(sheet_name)
|
2022-07-07 15:30:50 +02:00
|
|
|
diag_sheet, matrix = _excel_sheet_to_list(sheet, sheet_name)
|
2022-07-07 11:56:18 +02:00
|
|
|
diag += diag_sheet
|
|
|
|
matrix_list.append(matrix)
|
|
|
|
return diag, matrix_list
|
2022-06-02 21:49:37 +02:00
|
|
|
|
|
|
|
|
2021-08-11 11:40:28 +02:00
|
|
|
def excel_feuille_listeappel(
|
2021-09-17 10:26:20 +02:00
|
|
|
sem,
|
|
|
|
groupname,
|
|
|
|
lines,
|
|
|
|
partitions=None,
|
|
|
|
with_codes=False,
|
|
|
|
with_paiement=False,
|
|
|
|
server_name=None,
|
2021-08-11 11:40:28 +02:00
|
|
|
):
|
|
|
|
"""generation feuille appel"""
|
|
|
|
if partitions is None:
|
|
|
|
partitions = []
|
|
|
|
formsemestre_id = sem["formsemestre_id"]
|
|
|
|
sheet_name = "Liste " + groupname
|
|
|
|
|
|
|
|
ws = ScoExcelSheet(sheet_name)
|
|
|
|
ws.set_column_dimension_width("A", 3)
|
|
|
|
ws.set_column_dimension_width("B", 35)
|
|
|
|
ws.set_column_dimension_width("C", 12)
|
|
|
|
|
|
|
|
font1 = Font(name="Arial", size=11)
|
|
|
|
font1i = Font(name="Arial", size=10, italic=True)
|
|
|
|
font1b = Font(name="Arial", size=11, bold=True)
|
|
|
|
|
|
|
|
side_thin = Side(border_style="thin", color=COLORS.BLACK.value)
|
|
|
|
|
|
|
|
border_tbl = Border(top=side_thin, bottom=side_thin, left=side_thin)
|
|
|
|
border_tblr = Border(
|
|
|
|
top=side_thin, bottom=side_thin, left=side_thin, right=side_thin
|
|
|
|
)
|
|
|
|
|
|
|
|
style1i = {
|
|
|
|
"font": font1i,
|
|
|
|
}
|
|
|
|
|
|
|
|
style1b = {
|
|
|
|
"font": font1,
|
|
|
|
"border": border_tbl,
|
|
|
|
}
|
|
|
|
|
|
|
|
style2 = {
|
|
|
|
"font": Font(name="Arial", size=14),
|
|
|
|
}
|
|
|
|
|
|
|
|
style2b = {
|
|
|
|
"font": font1i,
|
|
|
|
"border": border_tblr,
|
|
|
|
}
|
|
|
|
|
|
|
|
style2t3 = {
|
|
|
|
"border": border_tblr,
|
|
|
|
}
|
|
|
|
|
|
|
|
style2t3bold = {
|
|
|
|
"font": font1b,
|
|
|
|
"border": border_tblr,
|
|
|
|
}
|
|
|
|
|
|
|
|
style3 = {
|
|
|
|
"font": Font(name="Arial", bold=True, size=14),
|
|
|
|
}
|
|
|
|
|
|
|
|
nb_weeks = 4 # nombre de colonnes pour remplir absences
|
|
|
|
|
|
|
|
# ligne 1
|
|
|
|
title = "%s %s (%s - %s)" % (
|
|
|
|
sco_preferences.get_preference("DeptName", formsemestre_id),
|
|
|
|
notesdb.unquote(sem["titre_num"]),
|
|
|
|
sem["date_debut"],
|
|
|
|
sem["date_fin"],
|
|
|
|
)
|
|
|
|
|
|
|
|
ws.append_row([None, ws.make_cell(title, style2)])
|
|
|
|
|
|
|
|
# ligne 2
|
|
|
|
ws.append_row([None, ws.make_cell("Discipline :", style2)])
|
|
|
|
|
|
|
|
# ligne 3
|
|
|
|
cell_2 = ws.make_cell("Enseignant :", style2)
|
2022-07-08 06:32:43 +02:00
|
|
|
cell_6 = ws.make_cell(f"Groupe {groupname}", style3)
|
2021-08-11 11:40:28 +02:00
|
|
|
ws.append_row([None, cell_2, None, None, None, None, cell_6])
|
|
|
|
|
|
|
|
# ligne 4: Avertissement pour ne pas confondre avec listes notes
|
|
|
|
cell_2 = ws.make_cell(
|
|
|
|
"Ne pas utiliser cette feuille pour saisir les notes !", style1i
|
|
|
|
)
|
|
|
|
ws.append_row([None, None, cell_2])
|
|
|
|
|
|
|
|
ws.append_blank_row()
|
|
|
|
ws.append_blank_row()
|
|
|
|
|
|
|
|
# ligne 7: Entête (contruction dans une liste cells)
|
|
|
|
cell_2 = ws.make_cell("Nom", style3)
|
|
|
|
cells = [None, cell_2]
|
|
|
|
for partition in partitions:
|
|
|
|
cells.append(ws.make_cell(partition["partition_name"], style3))
|
|
|
|
if with_codes:
|
|
|
|
cells.append(ws.make_cell("etudid", style3))
|
|
|
|
cells.append(ws.make_cell("code_nip", style3))
|
|
|
|
cells.append(ws.make_cell("code_ine", style3))
|
|
|
|
for i in range(nb_weeks):
|
|
|
|
cells.append(ws.make_cell("", style2b))
|
|
|
|
ws.append_row(cells)
|
|
|
|
|
|
|
|
n = 0
|
|
|
|
# pour chaque étudiant
|
|
|
|
for t in lines:
|
|
|
|
n += 1
|
|
|
|
nomprenom = (
|
2021-09-17 10:26:20 +02:00
|
|
|
t["civilite_str"] + " " + t["nom"] + " " + t["prenom"].lower().capitalize()
|
2021-08-11 11:40:28 +02:00
|
|
|
)
|
|
|
|
style_nom = style2t3
|
|
|
|
if with_paiement:
|
|
|
|
paie = t.get("paiementinscription", None)
|
|
|
|
if paie is None:
|
|
|
|
nomprenom += " (inscription ?)"
|
|
|
|
style_nom = style2t3bold
|
|
|
|
elif not paie:
|
|
|
|
nomprenom += " (non paiement)"
|
|
|
|
style_nom = style2t3bold
|
|
|
|
cell_1 = ws.make_cell(n, style1b)
|
|
|
|
cell_2 = ws.make_cell(nomprenom, style_nom)
|
|
|
|
cells = [cell_1, cell_2]
|
|
|
|
|
|
|
|
for partition in partitions:
|
|
|
|
if partition["partition_name"]:
|
|
|
|
cells.append(
|
|
|
|
ws.make_cell(t.get(partition["partition_id"], ""), style2t3)
|
|
|
|
)
|
|
|
|
if with_codes:
|
|
|
|
cells.append(ws.make_cell(t["etudid"], style2t3))
|
|
|
|
code_nip = t.get("code_nip", "")
|
|
|
|
cells.append(ws.make_cell(code_nip, style2t3))
|
|
|
|
code_ine = t.get("code_ine", "")
|
|
|
|
cells.append(ws.make_cell(code_ine, style2t3))
|
|
|
|
cells.append(ws.make_cell(t.get("etath", ""), style2b))
|
|
|
|
for i in range(1, nb_weeks):
|
|
|
|
cells.append(ws.make_cell(style=style2t3))
|
|
|
|
ws.append_row(cells)
|
|
|
|
|
|
|
|
ws.append_blank_row()
|
|
|
|
|
|
|
|
# bas de page (date, serveur)
|
|
|
|
dt = time.strftime("%d/%m/%Y à %Hh%M")
|
|
|
|
if server_name:
|
|
|
|
dt += " sur " + server_name
|
|
|
|
cell_2 = ws.make_cell(("Liste éditée le " + dt), style1i)
|
|
|
|
ws.append_row([None, cell_2])
|
|
|
|
|
2021-09-05 14:50:35 +02:00
|
|
|
return ws.generate()
|