我正在使用 xlrd 从 excel 电子表格中读取一堆原始数据,进行各种计算和重新格式化,然后使用 xlsxwriter 将我的结果写入一个新的工作簿。
我能够使用 xlrd 正确读取日期数据并将其转换为日期时间对象,但是当我尝试使用 xlsxwriter 编写此数据时出现错误。我已经阅读了所有关于 xlsxwriter 的 SO 帖子以及 excel 格式化数据等,并用谷歌搜索,但似乎无法弄明白。
我的代码是:
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'
完整的回溯错误是(不包括我的 py 文件中的第一个调用):
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
当我只调用普通的“out_sheet.write”时,生成的电子表格会在单元格中显示一堆“######”,但是当我单击单元格时,它会显示日期和时间想要它,但不确定如何在我这样做时摆脱这些“####”。我不关心使用 write_datetime() 或 write(),我只是希望它在输出表单元格中正确显示。
非常感谢您的帮助!
最佳答案
我安装了最新版本的 xlrd
(0.9.3) 和 xlsxwriter
(0.5.3) 并且能够毫无错误地运行您的示例程序:
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()
请注意,我在末尾添加了 workbook.close()
以避免任何文件关闭问题并使任何错误消息更清晰。这运行并生成了预期的 xlsx 文件和输出:
$ python so01.py
2014-05-02 00:00:00
<type 'datetime.datetime'>
请注意,从版本 0.93 开始,xlrd
还支持 xldate_as_datetime()
函数。所以你可以更简单地进行转换,如下所示:
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)
最后:
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,
这是 Excel 的标准方式,表示值太大而无法在单元格中显示(因为在上面的示例中它具有相当长的日期格式)。如果使用 worksheet.set_column()
加宽列宽您应该会看到预期值。
关于python - 使用 xlrd 读取日期并使用 xlsxwriter 在 Python 中写入日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23417661/