Skip to content

Commit

Permalink
display date as date when opening ods
Browse files Browse the repository at this point in the history
  • Loading branch information
pascalfree committed Jul 9, 2024
1 parent cd0ce71 commit 21e30a0
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/tablib/formats/_ods.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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'
Expand All @@ -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)
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 21e30a0

Please sign in to comment.