python-2.7 - 仅使用 xlwt 将日期格式应用于日期列

标签 python-2.7 export-to-excel xlwt

我得到了数据库搜索的结果,其中某些列有日期,但其他列没有。我需要帮助来写作

data = [['556644', 'Mr', 'So', 'And', 'So', Decimal('0.0000'), datetime.datetime(2012, 2, 25, 0, 0), '', False, datetime.datetime(2013, 6, 30, 0, 0)],...]

导入 Excel 电子表格,以便

easyxf(num_format_str='DD/MM/YYYY')

仅适用于日期时间列。我对 Python 还很陌生,这几天我一直在思考这个问题。谢谢!

最佳答案

简化你的 self 回答后:

date_xf = easyxf(num_format_str='DD/MM/YYYY') # sets date format in Excel
data = [list(n) for n in cursor.fetchall()]
for row_index, row_contents in enumerate(data):
    for column_index, cell_value in enumerate(row_contents):
        if isinstance(cell_value, datetime.date):
            sheet1.write(row_index+1, column_index, cell_value, date_xf)
        else:
            sheet1.write(row_index+1, column_index, cell_value)

关于python-2.7 - 仅使用 xlwt 将日期格式应用于日期列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11185104/

相关文章:

python-2.7 - PYSNMP for SNMP v3 支持 TRAP 和 Inform Receiver 吗?

python - 在 Windows 上使用 IDLE 安装 python 模块/包

python - 实例化一个类——带括号和不带括号的区别

c# - 我如何使用 EPPlus 将子对象导出为 Excel

ruby-on-rails - 在 Ruby 中编写具有动态背景颜色的 Excel 工作表

python - 在 Excel 中打印输出

python - 在 Python 中将 ppt 文件转换为 pptx

C# - 需要关于执行 SQL 和导出到 excel 的想法

python - 使用 xlwt python 合并两个以上单元格

python - 尝试使用 python 在 xlwt 中设置单元格颜色时丢失属性错误