From ef15897cb0f2f33684af17228bb6a672081efd39 Mon Sep 17 00:00:00 2001 From: jean-marie Place Date: Mon, 23 Aug 2021 17:47:49 +0200 Subject: [PATCH] =?UTF-8?q?correction=20affichage=20sur=202=20d=C3=A9cimal?= =?UTF-8?q?es=20dans=20les=20fichiers=20xlsx=20g=C3=A9n=C3=A9r=C3=A9s?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/scodoc/gen_tables.py | 4 ++-- app/scodoc/sco_excel.py | 27 +++++++++++++++------------ app/scodoc/sco_prepajury.py | 8 ++++++-- 3 files changed, 23 insertions(+), 16 deletions(-) diff --git a/app/scodoc/gen_tables.py b/app/scodoc/gen_tables.py index 5a0ba97068..d93ca57452 100644 --- a/app/scodoc/gen_tables.py +++ b/app/scodoc/gen_tables.py @@ -695,10 +695,10 @@ class SeqGenTable(object): def excel(self): """Export des genTables dans un unique fichier excel avec plusieurs feuilles tagguées""" - book = sco_excel.Workbook() # pylint: disable=no-member + book = sco_excel.ScoExcelBook() # pylint: disable=no-member for (_, gt) in self.genTables.items(): gt.excel(wb=book) # Ecrit dans un fichier excel - return book.savetostr() + return book.generate() # ----- Exemple d'utilisation minimal. diff --git a/app/scodoc/sco_excel.py b/app/scodoc/sco_excel.py index 8d52669c55..34495ac80a 100644 --- a/app/scodoc/sco_excel.py +++ b/app/scodoc/sco_excel.py @@ -38,6 +38,7 @@ import openpyxl.utils.datetime from openpyxl import Workbook, load_workbook from openpyxl.cell import WriteOnlyCell from openpyxl.styles import Font, Border, Side, Alignment, PatternFill +from openpyxl.styles.numbers import FORMAT_NUMBER_00, FORMAT_GENERAL from openpyxl.comments import Comment import app.scodoc.sco_utils as scu @@ -169,7 +170,7 @@ def excel_make_style( bgcolor: COLORS = None, halign=None, valign=None, - format_number=None, + number_format=None, font_name="Arial", size=10, ): @@ -180,7 +181,7 @@ def excel_make_style( bgcolor -- la couleur de fond halign -- alignement horizontal ("left", "right", "center") valign -- alignement vertical ("top", "bottom", "center") - format_number -- formattage du contenu ("General", "@", ...) + number_format -- formattage du contenu ("General", "@", ...) font_name -- police size -- taille de police """ @@ -204,10 +205,10 @@ def excel_make_style( "center": "center", }[valign] style["alignment"] = al - if format_number is None: - style["format_number"] = "General" + if number_format is None: + style["number_format"] = FORMAT_GENERAL else: - style["format_number"] = format_number + style["number_format"] = number_format return style @@ -400,16 +401,18 @@ def excel_simple_table( ] ) default_style = excel_make_style() - text_style = excel_make_style(format_number="@") + text_style = excel_make_style(number_format=FORMAT_GENERAL) + int_style = excel_make_style() + float_style = excel_make_style(number_format=FORMAT_NUMBER_00) for line in lines: cells = [] for it in line: - # safety net: allow only str, int and float - # TODO Plus de type Long en Python 3 ? - # if isinstance(it, long): # XXX - # it = int(it) # assume all ScoDoc longs fits in int ! cell_style = default_style - if type(it) not in (int, float): # XXX A REVOIR + if type(it) == float: + cell_style = float_style + elif type(it) == int: + cell_style = int_style + else: cell_style = text_style cells.append(ws.make_cell(it, cell_style)) ws.append_row(cells) @@ -472,7 +475,7 @@ def excel_feuille_saisie(e, titreannee, description, lines): } style_notes = { "font": font_bold, - "number_format": "General", + "number_format": FORMAT_GENERAL, "fill": fill_light_yellow, "border": border_top, } diff --git a/app/scodoc/sco_prepajury.py b/app/scodoc/sco_prepajury.py index f1f20c1eae..c20d11034e 100644 --- a/app/scodoc/sco_prepajury.py +++ b/app/scodoc/sco_prepajury.py @@ -29,6 +29,8 @@ """ import time +from openpyxl.styles.numbers import FORMAT_NUMBER_00 + import app.scodoc.sco_utils as scu from app.scodoc import sco_abs from app.scodoc import sco_groups @@ -162,9 +164,11 @@ def feuille_preparation_jury(formsemestre_id, REQUEST): style_moy = sco_excel.excel_make_style( bold=True, halign="center", bgcolor=sco_excel.COLORS.LIGHT_YELLOW ) - style_note = sco_excel.excel_make_style(halign="right", format_number="General") + style_note = sco_excel.excel_make_style( + halign="right", number_format=FORMAT_NUMBER_00 + ) style_note_bold = sco_excel.excel_make_style( - halign="right", bold=True, format_number="General" + halign="right", bold=True, number_format="General" ) # Première ligne