From 5c52c665fb81379539e531e7f09d4f2b47887534 Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Thu, 15 Nov 2018 16:34:14 +0100 Subject: [PATCH] correctly export numbers to ODS (fixes #28058) --- bijoe/visualization/ods.py | 14 +++++++++++++- bijoe/visualization/utils.py | 2 +- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/bijoe/visualization/ods.py b/bijoe/visualization/ods.py index fe81930..b9e54aa 100644 --- a/bijoe/visualization/ods.py +++ b/bijoe/visualization/ods.py @@ -16,6 +16,8 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . +import sys + import zipfile import xml.etree.ElementTree as ET @@ -25,6 +27,13 @@ TEXT_NS = 'urn:oasis:names:tc:opendocument:xmlns:text:1.0' XLINK_NS = 'http://www.w3.org/1999/xlink' +def is_number(x): + if sys.version_info >= (3, 0): + return isinstance(x, (int, float)) + else: + return isinstance(x, (int, long, float)) + + class Workbook(object): def __init__(self, encoding='utf-8'): self.sheets = [] @@ -94,6 +103,9 @@ class WorkSheet(object): class WorkCell(object): def __init__(self, worksheet, value, hint=None): + self.value_type = 'string' + if is_number(value): + self.value_type = 'float' if value is None: value = '' if type(value) is not unicode: @@ -104,7 +116,7 @@ class WorkCell(object): def get_node(self): root = ET.Element('{%s}table-cell' % TABLE_NS) - root.attrib['{%s}value-type' % OFFICE_NS] = 'string' + root.attrib['{%s}value-type' % OFFICE_NS] = self.value_type p = ET.SubElement(root, '{%s}p' % TEXT_NS) if self.hint == 'uri': base_filename = self.value.split('/')[-1] diff --git a/bijoe/visualization/utils.py b/bijoe/visualization/utils.py index 8c1b9f8..476f9cb 100644 --- a/bijoe/visualization/utils.py +++ b/bijoe/visualization/utils.py @@ -253,7 +253,7 @@ class Visualization(object): sheet.write(0, 0, full_title) for j, row in enumerate(table.table()): for i, value in enumerate(row): - sheet.write(j + 1, i, unicode(0 if value is None else value)) + sheet.write(j + 1, i, 0 if value is None else value) return workbook def title(self): -- 2.18.0