Fix: export excel avec caractères invalides

This commit is contained in:
Emmanuel Viennet 2025-02-20 15:52:46 +01:00
parent 845b0e9363
commit 8ddab219e1
2 changed files with 14 additions and 3 deletions

View File

@ -40,7 +40,9 @@ from openpyxl.styles.numbers import FORMAT_NUMBER_00, FORMAT_GENERAL, FORMAT_DAT
from openpyxl.comments import Comment
from openpyxl import Workbook, load_workbook
from openpyxl.cell import WriteOnlyCell
from openpyxl.cell.cell import ILLEGAL_CHARACTERS_RE
from openpyxl.styles import Font, Border, Side, Alignment, PatternFill
from openpyxl.utils.exceptions import IllegalCharacterError
from openpyxl.worksheet.worksheet import Worksheet
import app.scodoc.sco_utils as scu
@ -366,8 +368,17 @@ class ScoExcelSheet:
value = value.replace(tzinfo=None)
# création de la cellule
cell = WriteOnlyCell(self.ws, value)
try:
cell = WriteOnlyCell(self.ws, value)
except IllegalCharacterError as exc:
if isinstance(value, str):
# chaine contenant un caractère interdit dans Excel: élimine
value = ILLEGAL_CHARACTERS_RE.sub("", value)
cell = WriteOnlyCell(self.ws, value)
else:
raise ScoValueError(
f"Valeur de cellule non autorisée: {value}"
) from exc
# recopie des styles
if style is None:
style = self.default_style

View File

@ -3,7 +3,7 @@
"Infos sur version ScoDoc"
SCOVERSION = "9.7.63"
SCOVERSION = "9.7.64"
SCONAME = "ScoDoc"