From bf83d8475ac6034b9117b6891b4acbba48219618 Mon Sep 17 00:00:00 2001 From: Place Jean-Marie Date: Tue, 5 Oct 2021 05:38:04 +0200 Subject: [PATCH] fix excel export with datetime.date values (formsemestre_description with evals) --- app/scodoc/sco_excel.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/app/scodoc/sco_excel.py b/app/scodoc/sco_excel.py index 6904062cf..9698256f1 100644 --- a/app/scodoc/sco_excel.py +++ b/app/scodoc/sco_excel.py @@ -35,7 +35,7 @@ from enum import Enum from tempfile import NamedTemporaryFile import openpyxl.utils.datetime -from openpyxl.styles.numbers import FORMAT_NUMBER_00, FORMAT_GENERAL +from openpyxl.styles.numbers import FORMAT_NUMBER_00, FORMAT_GENERAL, FORMAT_DATE_DDMMYY from openpyxl.comments import Comment from openpyxl import Workbook, load_workbook from openpyxl.cell import WriteOnlyCell @@ -283,10 +283,6 @@ class ScoExcelSheet: style -- style par défaut (dictionnaire cf. excel_make_style) de la feuille si non spécifié """ cell = WriteOnlyCell(self.ws, value or "") - if not (isinstance(value, int) or isinstance(value, float)): - cell.data_type = "s" - # if style is not None and "fill" in style: - # toto() if style is None: style = self.default_style if "font" in style: @@ -308,6 +304,14 @@ class ScoExcelSheet: lines = comment.splitlines() cell.comment.width = 7 * max([len(line) for line in lines]) cell.comment.height = 20 * len(lines) + # test datatype at the end so that datetime format may be overwritten + 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 make_row(self, values: list, style=None, comments=None):