在python(xlwt)中设置excel列样式

在python(xlwt)中设置excel列样式,python,excel,xlwt,Python,Excel,Xlwt,我试图将数据写入excel文档,其中一些列完全由我要格式化的日期/数字数据组成。我可以单独设置每个单元格的格式,但这似乎太过分了。在列、对象上有一个set\u style方法,但出于某种原因,它似乎什么都不做 import xlwt from datetime import date book = xlwt.Workbook('ascii') sheet = book.add_sheet('Sheet1') # cells in first column, they end up with no

我试图将数据写入excel文档,其中一些列完全由我要格式化的日期/数字数据组成。我可以单独设置每个单元格的格式,但这似乎太过分了。在列、对象上有一个
set\u style
方法,但出于某种原因,它似乎什么都不做

import xlwt
from datetime import date
book = xlwt.Workbook('ascii')
sheet = book.add_sheet('Sheet1')
# cells in first column, they end up with no formatting
sheet.write(0, 0, date(2012, 1, 1))
sheet.write(1, 0, date(2012, 1, 1))
sheet.write(2, 0, date(2012, 2, 1))
style = xlwt.easyxf(num_format_str='YYYY-MM-DD')
sheet.col(0).set_style(style)
# cells in second column are formatted correctly
sheet.write(0, 1, date(2012, 1, 1), style)
sheet.write(1, 1, date(2012, 1, 1), style)
sheet.write(2, 1, date(2012, 2, 1), style)
book.save(open('foo.xls', 'wb'))
希望这有助于:


我已经在循环中看到了这一点,以便高效使用。

这很有帮助。事实证明,您无法按照预期的方式在XSL文件中设置列样式。我遇到的问题是样式太多,但结果是单元格样式只是引用,因此如果将同一个样式对象传递给不同的单元格,就不会超过样式限制。我是通过谷歌发现这个问题的。如果你编辑你的答案来回答问题,而不是仅仅是链接,我会对你的答案进行更新。以防Vivek的答案因为仅仅是链接而被删除:这里有一个技巧说是有帮助的。