3.0.7: Exception during save for offset-aware datetimes
With version 3.0.7, if you use a datetime with a timezone, you get the following exception:
File "\lib\site-packages\openpyxl\utils\datetime.py", line 97, in to_excel
days = (dt - epoch).days
TypeError: can't subtract offset-naive and offset-aware datetimes
Here is the replication code:
import openpyxl
from datetime import datetime, timezone
def main():
create_wb_with_timezone_aware_datetime()
def create_wb_with_timezone_aware_datetime():
wb = openpyxl.Workbook()
#wb.iso_dates = True # If this line is enabled, Excel reports corrupted output.
ws = wb.active
dt = datetime.now(timezone.utc) # Remove the timzone.utc and it will pass
ws.cell(row=1, column=1, value=dt)
wb.save('openpyxl_timezone_aware_fails.xlsx')
print('This will never print. Fails on previous line')
if __name__ == '__main__':
main()
It is possible to add the new wb.iso_dates = True
option. This makes the "save" pass, but Excel complains that the spreadsheet is corrupt.
Edited by Bryce Poole