简体   繁体   English

使用xlwt编写日期格式

[英]Writing date format using xlwt

I'm writing a date using xlwt like this: 我正在使用xlwt写一个日期,如下所示:

date_format = XFStyle()
date_format.num_format_str = 'dd/MM/yyyy'
plan.write(1,4,'01/01/2014', date_format)

The worksheet is saving fine, with no errors. 工作表保存正常,没有错误。 But even this cell is formatted as date, excel not recognized it as a date until I manually double click it on the excel. 但即使这个单元格被格式化为日期,excel也不会将其识别为日期,直到我在excel上手动双击它。 But I have more than 1.000 dates and I can't do that all the time. 但我有超过1000个日期,我不能一直这样做。 Is there a way to save as real date, with no need to manually update the cell? 有没有办法保存为实际日期,而无需手动更新单元格?

Thanks 谢谢

Excel date is a float number in a cell formatted as a date. Excel日期是格式化为日期的单元格中的浮点数。

You're trying to write a string into cell. 你正在尝试将字符串写入单元格。 xlwt should convert a datetime object to float but it's not going to implicitly convert a string to Excel date. xlwt应该将datetime对象转换为float,但它不会将字符串隐式转换为Excel日期。

from datetime import datetime

date_format = XFStyle()
date_format.num_format_str = 'dd/MM/yyyy'
plan.write(1, 4, datetime.strptime("01/01/2014", "%d/%M/%Y"), date_format)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM