Merge pull request 'fix excel export with datetime.date values (formsemestre_description with evals)' (#145) from jmplace/ScoDoc-Lille:fix_date_xls into master

Reviewed-on: https://scodoc.org/git/viennet/ScoDoc/pulls/145
This commit is contained in:
Emmanuel Viennet 2021-10-05 12:35:39 +02:00
commit 1fe814a674

View File

@ -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):