From c1e154bee1fb49a102250048204b4e01c3def313 Mon Sep 17 00:00:00 2001 From: Christophe Monniez Date: Thu, 6 Jul 2017 14:00:09 +0200 Subject: [PATCH] [FIX] tools: ensure xlsx worksheet names are valid When xlsx is used, the worksheet name may be longer than 31 chars and raise an exception. This fix will troncate the name up to 31 chars and will remove invalid chars. Similar patch was already done at 0122c05eff95 for xlwt xlsxwriter 0.9.8 has added a parameter worksheet_class=None so adding **kw to be compatible with installs with the latest version (requirements.txt asks for version 0.9.3) Closes #18077 opw-751157 --- odoo/tools/misc.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/odoo/tools/misc.py b/odoo/tools/misc.py index c54af653a00..cb0c662ca49 100644 --- a/odoo/tools/misc.py +++ b/odoo/tools/misc.py @@ -363,6 +363,28 @@ except ImportError: xlwt = None +try: + import xlsxwriter + + # add some sanitizations to respect the excel sheet name restrictions + # as the sheet name is often translatable, can not control the input + class PatchedXlsxWorkbook(xlsxwriter.Workbook): + + # TODO when xlsxwriter bump to 0.9.8, add worksheet_class=None parameter instead of kw + def add_worksheet(self, name=None, **kw): + # invalid Excel character: []:*?/\ + name = re.sub(r'[\[\]:*?/\\]', '', name) + + # maximum size is 31 characters + name = name[:31] + return super(PatchedXlsxWorkbook, self).add_worksheet(name, **kw) + + xlsxwriter.Workbook = PatchedXlsxWorkbook + +except ImportError: + xlsxwriter = None + + class UpdateableStr(local): """ Class that stores an updateable string (used in wizards) """