Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

xlwt

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 could do it when I write the cell but it will make my code a little more complex.

like image 642
Pickels Avatar asked Nov 17 '25 09:11

Pickels


1 Answers

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.

like image 200
Jacob Avatar answered Nov 19 '25 22:11

Jacob