Skip to content

Commit

Permalink
ods: add styling on exported time and datetime fields
Browse files Browse the repository at this point in the history
  • Loading branch information
pascalfree committed Jul 10, 2024
1 parent 0bbb847 commit 06be74d
Showing 1 changed file with 47 additions and 7 deletions.
54 changes: 47 additions & 7 deletions src/tablib/formats/_ods.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,52 @@
fontweightcomplex="bold",
))


def set_date_style(style):
style.addElement(number.Year(style="long"))
style.addElement(number.Text(text="-"))
style.addElement(number.Month(style="long"))
style.addElement(number.Text(text="-"))
style.addElement(number.Day(style="long"))


def set_time_style(style):
style.addElement(number.Hours(style="long"))
style.addElement(number.Text(text=":"))
style.addElement(number.Minutes(style="long"))
style.addElement(number.Text(text=":"))
style.addElement(number.Seconds(style="long", decimalplaces="0"))


date_style = number.DateStyle(name="date-style1")
date_style.addElement(number.Year(style="long"))
date_style.addElement(number.Text(text="-"))
date_style.addElement(number.Month(style="long"))
date_style.addElement(number.Text(text="-"))
date_style.addElement(number.Day(style="long"))
set_date_style(date_style)
ds = style.Style(
name="ds1",
datastylename="date-style1",
parentstylename="Default",
family="table-cell",
)

time_style = number.DateStyle(name="time-style1")
set_time_style(time_style)
ts = style.Style(
name="ts1",
datastylename="time-style1",
parentstylename="Default",
family="table-cell",
)

datetime_style = number.DateStyle(name="datetime-style1")
set_date_style(datetime_style)
datetime_style.addElement(number.Text(text=" "))
set_time_style(datetime_style)
dts = style.Style(
name="dts1",
datastylename="datetime-style1",
parentstylename="Default",
family="table-cell",
)


class ODSFormat:
title = 'ods'
Expand All @@ -42,6 +75,10 @@ def export_set(cls, dataset):
wb.automaticstyles.addElement(bold)
wb.styles.addElement(date_style)
wb.automaticstyles.addElement(ds)
wb.styles.addElement(time_style)
wb.automaticstyles.addElement(ts)
wb.styles.addElement(datetime_style)
wb.automaticstyles.addElement(dts)

ws = table.Table(name=dataset.title if dataset.title else 'Tablib Dataset')
wb.spreadsheet.addElement(ws)
Expand Down Expand Up @@ -173,14 +210,17 @@ def dset_sheet(cls, dataset, ws):
cell = table.TableCell(valuetype="float", value=col)
elif isinstance(col, dt.datetime):
cell = table.TableCell(
valuetype="date", value=col.strftime('%Y-%m-%dT%H:%M:%S')
valuetype="date", value=col.strftime('%Y-%m-%dT%H:%M:%S'), stylename=dts
)
cell.addElement(text.P(text=col.strftime('%Y-%m-%d %H:%M:%S')))
elif isinstance(col, dt.date):
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'))
time_value = col.strftime('%H:%M:%S')
cell = table.TableCell(valuetype="time", timevalue=time_value, stylename=ts)
cell.addElement(text.P(text=time_value))
elif col is None:
cell = table.TableCell(valuetype="void")
else:
Expand Down

0 comments on commit 06be74d

Please sign in to comment.