Question

I am using xlrd to read a bunch of raw data from an excel spreadsheet, do various calculations and reformatting, and then write my results to a new workbook using xlsxwriter.

I'm able to read in the date data correctly using xlrd and convert to a datetime object, but when I try to write this using xlsxwriter I get errors. I've read all the SO posts on xlsxwriter and how excel formats data, etc., and googled it, but can't seem to figure it out.

My code is:

in_wb = xlrd.open_workbook("in_book.xlsx")
in_sheet = in_wb.sheet_by_name("in_sheet")

out_wb = xlsxwriter.Workbook("out_book.xlsx")
out_sheet = out_wb.add_worksheet("out_sheet")
date_format = out_wb.add_format({'num_format': 'YYYY-MM-DD HH:DD:SS'})

as_tuple = xlrd.xldate_as_tuple(in_sheet.cell_value(0, 0), in_wb.datemode)
as_datetime = datetime.datetime(as_tuple[0], as_tuple[1], as_tuple[2] , as_tuple[3], as_tuple[4], as_tuple[5])

out_sheet.write_datetime(0, 0, as_datetime, date_format)

#print details just to be sure
print as_datetime #prints it in exactly the format I want
print type(as_datetime) #says it is of type 'datetime.datetime'

The full Traceback error is (excluding the very first call from my py file):

  File "C:\Python27\lib\site-packages\xlsxwriter\worksheet.py", line 57, in cell_wrapper
return method(self, *args, **kwargs)
  File "C:\Python27\lib\site-packages\xlsxwriter\worksheet.py", line 668, in write_datetime
number = self._convert_date_time(date)
  File "C:\Python27\lib\site-packages\xlsxwriter\worksheet.py", line 3267, in _convert_date_time
return datetime_to_excel_datetime(dt_obj, self.date_1904)
  File "C:\Python27\lib\site-packages\xlsxwriter\utility.py", line 576, in datetime_to_excel_datetime
raise TypeError("Unknown or unsupported datetime type")
  TypeError: Unknown or unsupported datetime type
  Exception LookupError: 'unknown encoding: utf-8' in <bound method Workbook.__del__ of <xlsxwriter.workbook.Workbook object at 0x030BAB50>> ignored

When I call just the ordinary 'out_sheet.write' instead, the resulting spreadsheet shows a bunch of '######' in the cell, but when I click on the cell it shows the date and time as I wanted it, not sure how to get ride of these '####' when I do it this way. I don't care about using write_datetime() or just write(), I just want it to show up correctly in the output sheet cells.

Thanks very much for your help!

Was it helpful?

Solution

I installed the latest versions of xlrd (0.9.3) and xlsxwriter (0.5.3) and was able to run your sample program without any error:

import xlrd
import xlsxwriter
import datetime

in_wb = xlrd.open_workbook("in_book.xlsx")
in_sheet = in_wb.sheet_by_name("in_sheet")

out_wb = xlsxwriter.Workbook("out_book.xlsx")
out_sheet = out_wb.add_worksheet("out_sheet")
date_format = out_wb.add_format({'num_format': 'YYYY-MM-DD HH:DD:SS'})

as_tuple = xlrd.xldate_as_tuple(in_sheet.cell_value(0, 0), in_wb.datemode)
as_datetime = datetime.datetime(as_tuple[0], as_tuple[1], as_tuple[2],
                                as_tuple[3], as_tuple[4], as_tuple[5])

out_sheet.write_datetime(0, 0, as_datetime, date_format)


print as_datetime
print type(as_datetime)

out_wb.close()

Note, I added a workbook.close() to the end to avoid any file closing issues and to make any error messages cleaner. This ran and generated the expected xlsx file and output:

$ python so01.py
2014-05-02 00:00:00
<type 'datetime.datetime'> 

Note, as of version 0.93 xlrd also supports a xldate_as_datetime() function. So you could do the conversion more simply as follows:

as_datetime = xlrd.xldate.xldate_as_datetime(in_sheet.cell_value(0, 0), 
                                             in_wb.datemode)

out_sheet.write_datetime(0, 0, as_datetime, date_format)

And finally:

When I call just the ordinary 'out_sheet.write' instead, the resulting spreadsheet shows a bunch of '######' in the cell, but when I click on the cell it shows the date and time as I wanted it,

This is Excel's standard way of saying that the value is too big to display in the cell (since it has quite a long date format in the example above). If you widen the column width with worksheet.set_column() you should see the expected value.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top