开发者

xlwt: Is it possible to adjust the style of a cell after creating it?

开发者 https://www.devze.com 2023-03-27 20:40 出处:网络
After you do: sheet.write(0, 1, \'whatevs\') Is it still possible to adjust the style of cell 0,1. The reason I am asking is that I have a list of errors I loop and I would like to color all the ce

After you do:

sheet.write(0, 1, 'whatevs')

Is it still possible to adjust the style of cell 0,1. The reason I am asking is that I have a list of errors I loop and I would like to color all the cells red that have an error.

I cou开发者_C百科ld do it when I write the cell but it will make my code a little more complex.


There is no API exposed to do it, you could look at the source code and come up with a way:

rows = ws.get_rows()
rows[0]._Row_cells[0].xf_idx = styleindex 

You can get the styleindex by adding a style you created.

style0 = xlwt.easyxf('font: name Times New Roman, color-index red, bold on',
                 num_format_str='#,##0.00')
styleindex = wb.add_style(style0)

wb is a Workbook object and ws your Worksheet.

Beware: this isn't meant to be done this way, but I couldn't find another.

0

精彩评论

暂无评论...
验证码 换一张
取 消