Merge pull request 'correction affichage sur 2 décimales dans les fichiers xlsx générés' (#114) from jmplace/ScoDoc-Lille:fix_excel_numbers into master

Reviewed-on: https://scodoc.org/git/viennet/ScoDoc/pulls/114
This commit is contained in:
Emmanuel Viennet 2021-08-23 23:06:50 +02:00
commit 671467663d
3 changed files with 23 additions and 16 deletions

View File

@ -695,10 +695,10 @@ class SeqGenTable(object):
def excel(self): def excel(self):
"""Export des genTables dans un unique fichier excel avec plusieurs feuilles tagguées""" """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(): for (_, gt) in self.genTables.items():
gt.excel(wb=book) # Ecrit dans un fichier excel gt.excel(wb=book) # Ecrit dans un fichier excel
return book.savetostr() return book.generate()
# ----- Exemple d'utilisation minimal. # ----- Exemple d'utilisation minimal.

View File

@ -38,6 +38,7 @@ import openpyxl.utils.datetime
from openpyxl import Workbook, load_workbook from openpyxl import Workbook, load_workbook
from openpyxl.cell import WriteOnlyCell from openpyxl.cell import WriteOnlyCell
from openpyxl.styles import Font, Border, Side, Alignment, PatternFill from openpyxl.styles import Font, Border, Side, Alignment, PatternFill
from openpyxl.styles.numbers import FORMAT_NUMBER_00, FORMAT_GENERAL
from openpyxl.comments import Comment from openpyxl.comments import Comment
import app.scodoc.sco_utils as scu import app.scodoc.sco_utils as scu
@ -169,7 +170,7 @@ def excel_make_style(
bgcolor: COLORS = None, bgcolor: COLORS = None,
halign=None, halign=None,
valign=None, valign=None,
format_number=None, number_format=None,
font_name="Arial", font_name="Arial",
size=10, size=10,
): ):
@ -180,7 +181,7 @@ def excel_make_style(
bgcolor -- la couleur de fond bgcolor -- la couleur de fond
halign -- alignement horizontal ("left", "right", "center") halign -- alignement horizontal ("left", "right", "center")
valign -- alignement vertical ("top", "bottom", "center") valign -- alignement vertical ("top", "bottom", "center")
format_number -- formattage du contenu ("General", "@", ...) number_format -- formattage du contenu ("General", "@", ...)
font_name -- police font_name -- police
size -- taille de police size -- taille de police
""" """
@ -204,10 +205,10 @@ def excel_make_style(
"center": "center", "center": "center",
}[valign] }[valign]
style["alignment"] = al style["alignment"] = al
if format_number is None: if number_format is None:
style["format_number"] = "General" style["number_format"] = FORMAT_GENERAL
else: else:
style["format_number"] = format_number style["number_format"] = number_format
return style return style
@ -400,16 +401,18 @@ def excel_simple_table(
] ]
) )
default_style = excel_make_style() 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: for line in lines:
cells = [] cells = []
for it in line: 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 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 cell_style = text_style
cells.append(ws.make_cell(it, cell_style)) cells.append(ws.make_cell(it, cell_style))
ws.append_row(cells) ws.append_row(cells)
@ -472,7 +475,7 @@ def excel_feuille_saisie(e, titreannee, description, lines):
} }
style_notes = { style_notes = {
"font": font_bold, "font": font_bold,
"number_format": "General", "number_format": FORMAT_GENERAL,
"fill": fill_light_yellow, "fill": fill_light_yellow,
"border": border_top, "border": border_top,
} }

View File

@ -29,6 +29,8 @@
""" """
import time import time
from openpyxl.styles.numbers import FORMAT_NUMBER_00
import app.scodoc.sco_utils as scu import app.scodoc.sco_utils as scu
from app.scodoc import sco_abs from app.scodoc import sco_abs
from app.scodoc import sco_groups from app.scodoc import sco_groups
@ -162,9 +164,11 @@ def feuille_preparation_jury(formsemestre_id, REQUEST):
style_moy = sco_excel.excel_make_style( style_moy = sco_excel.excel_make_style(
bold=True, halign="center", bgcolor=sco_excel.COLORS.LIGHT_YELLOW 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( 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 # Première ligne