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

时间:2013-09-26 16:43:02

标签: python excel xlwt

我正在尝试将数据写入excel文档,其中一些列完全由我想要格式化的日期/数字数据组成。我可以单独设置每个单元格的格式,但这似乎过多。列,对象上有一个set_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'))

1 个答案:

答案 0 :(得分:0)

希望这有助于: Similar to your question

我已经看到这在循环中使用,以便有效使用。