diff --git a/src/tablib/formats/_ods.py b/src/tablib/formats/_ods.py index 0d6ee6b2..b9063a9a 100644 --- a/src/tablib/formats/_ods.py +++ b/src/tablib/formats/_ods.py @@ -16,12 +16,25 @@ 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", @@ -29,6 +42,26 @@ 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' @@ -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) @@ -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: