Domanda

I'm trying to write data to an excel document, some of the columns consist entirely of date/numeric data that I want to format. I could set a format on each cell individually, but that seems excessive. There's a set_style method on the column, object, but for some reason it doesn't seem to do anything.

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'))
È stato utile?

Soluzione

Hope this could help: Similar to your question

I have seen this used in loops, for efficient usage.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top