Skip to content

Commit d79a4f9

Browse files
Fix spreadsheet export
1 parent f9ae163 commit d79a4f9

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

cardinal_pythonlib/excel.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ def convert_for_openpyxl(x: Any) -> Any:
8383
if isinstance(x, DateTime):
8484
return pendulum_to_datetime(x)
8585
However, conversion of pendulum.datetime.Datetime to datetime.datetime is
86-
insufficient, because you can still end up with this error from
87-
openpyxl/utils/datetime.py, line 97, in to_excel:
86+
insufficient, because with openpyxl==3.0.7 you can still end up with this
87+
error from openpyxl/utils/datetime.py, line 97, in to_excel:
8888
days = (dt - epoch).days
8989
TypeError: can't subtract offset-naive and offset-aware datetimes
9090
The "epoch" variable does NOT have a timezone attribute. So we need to
@@ -96,7 +96,9 @@ def convert_for_openpyxl(x: Any) -> Any:
9696
information, preserving all data but letting the user sort out the meaning.
9797
Since ``convert_for_pyexcel_ods3`` was already converting
9898
pendulum.datetime.DateTime and datetime.datetime values to a standard
99-
string, via strftime, let's do that too.
99+
string, via strftime, let's do that too. Note that this also anticipates
100+
the deprecation of timezone-aware dates from openpyxl==3.0.7
101+
(https://foss.heptapod.net/openpyxl/openpyxl/-/issues/1645).
100102
"""
101103
if isinstance(x, (DateTime, datetime.datetime)):
102104
return x.strftime(ISO8601_STRFTIME_FORMAT)
@@ -117,12 +119,19 @@ def convert_for_pyexcel_ods3(x: Any) -> Any:
117119
- ``None``
118120
- :class:`numpy.float64`
119121
- :class:`uuid.UUID`
122+
- subclasses of `str`
120123
121124
Args:
122125
x: a data value
123126
124127
Returns:
125128
the same thing, or a more suitable value!
129+
130+
2025-03-06 update: With pyexcel-ods3==0.6.0, we were getting a KeyError
131+
from pyexcel_ods3/odsw.py, in ODSSheetWriter.write_row. It does this:
132+
value_type = service.ODS_WRITE_FORMAT_COVERSION[type(cell)]
133+
and we had a cell that looked like 'aq' but had the type <class
134+
'sqlalchemy.sql.elements.quoted_name'>, a subclass of str.
126135
"""
127136
if isinstance(x, (DateTime, datetime.datetime)):
128137
return x.strftime(ISO8601_STRFTIME_FORMAT)
@@ -132,5 +141,7 @@ def convert_for_pyexcel_ods3(x: Any) -> Any:
132141
return str(x)
133142
elif isinstance(x, float64):
134143
return float(x)
144+
elif isinstance(x, str):
145+
return str(x)
135146
else:
136147
return x

0 commit comments

Comments
 (0)