From 21e30a029b2ad65eec1a39f0ccdeaa58eef2005f Mon Sep 17 00:00:00 2001 From: pascalfree Date: Tue, 9 Jul 2024 03:45:19 +0200 Subject: [PATCH] display date as date when opening ods --- src/tablib/formats/_ods.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/tablib/formats/_ods.py b/src/tablib/formats/_ods.py index 34b47a0d..dc956386 100644 --- a/src/tablib/formats/_ods.py +++ b/src/tablib/formats/_ods.py @@ -5,7 +5,7 @@ import numbers from io import BytesIO -from odf import opendocument, style, table, text +from odf import opendocument, style, table, text, number import tablib @@ -16,6 +16,14 @@ fontweightcomplex="bold", )) +date_style = number.DateStyle(name="date-style1") +date_style.addElement(number.Year(style="long")) +date_style.addElement(number.Text(text=u"-")) +date_style.addElement(number.Month(style="long")) +date_style.addElement(number.Text(text=u"-")) +date_style.addElement(number.Day(style="long")) +ds = style.Style(name="ds1", datastylename="date-style1", parentstylename="Default", family="table-cell") + class ODSFormat: title = 'ods' @@ -27,6 +35,8 @@ def export_set(cls, dataset): wb = opendocument.OpenDocumentSpreadsheet() wb.automaticstyles.addElement(bold) + wb.styles.addElement(date_style) + wb.automaticstyles.addElement(ds) ws = table.Table(name=dataset.title if dataset.title else 'Tablib Dataset') wb.spreadsheet.addElement(ws) @@ -161,7 +171,9 @@ def dset_sheet(cls, dataset, ws): valuetype="date", value=col.strftime('%Y-%m-%dT%H:%M:%S') ) elif isinstance(col, dt.date): - cell = table.TableCell(valuetype="date", datevalue=col.strftime('%Y-%m-%d')) + date_value = col.strftime('%Y-%m-%d') + cell = table.TableCell(valuetype="date", datevalue=date_value, stylename=ds) + cell.addElement(text.P(text=date_value)) elif isinstance(col, dt.time): cell = table.TableCell(valuetype="time", timevalue=col.strftime('%H:%M:%S')) elif col is None: