【问题标题】:Using xlrd to read dates and xlsxwriter to write them in Python使用 xlrd 读取日期并使用 xlsxwriter 在 Python 中编写它们
【发布时间】:2014-05-01 22:23:26
【问题描述】:

我正在使用 xlrd 从 Excel 电子表格中读取一堆原始数据,进行各种计算和重新格式化,然后使用 xlsxwriter 将结果写入新工作簿。

我能够使用 xlrd 正确读取日期数据并转换为 datetime 对象,但是当我尝试使用 xlsxwriter 编写它时出现错误。我已经阅读了 xlsxwriter 上的所有 SO 帖子以及 excel 如何格式化数据等,并在 Google 上搜索过,但似乎无法弄清楚。

我的代码是:

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'

完整的 Traceback 错误是(不包括我的 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(),我只希望它正确显示在输出表单元格中。

非常感谢您的帮助!

【问题讨论】:

  • 我的代码中的最后一个打印调用产生的结果是'datetime.datetime',它应该是由 write_datetime() 函数支持的。

标签: python datetime xlsxwriter


【解决方案1】:

我安装了最新版本的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)

最后:

当我只调用普通的“out_sheet.write”时,生成的电子表格会在单元格中显示一堆“######”,但是当我单击单元格时,它会显示日期和时间,因为我想要,

这是 Excel 表示值太大而无法在单元格中显示的标准方式(因为在上面的示例中它具有相当长的日期格式)。如果您使用worksheet.set_column() 加宽列宽,您应该会看到预期值。

【讨论】:

  • 感谢您的帮助,不知道为什么我会收到错误但您没有,但它现在可以正常工作,我根据您的建议摆脱了“#”。再次感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-10-21
  • 1970-01-01
  • 2015-02-21
  • 1970-01-01
  • 1970-01-01
  • 2011-06-20
  • 2020-12-27
相关资源
最近更新 更多