Python – xlwt: Is it possible to adjust the style of cells after creation?

xlwt: Is it possible to adjust the style of cells after creation?… here is a solution to the problem.

xlwt: Is it possible to adjust the style of cells after creation?

After :

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

Whether you can still adjust the style of cells 0,1. The reason I ask is that I have a looping error list and I want to paint all cells with errors red.

I can do this while writing cells, but it makes my code a bit more complicated.

Solution

There is no public API to do this, you can look at the source code and come up with a method:

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

You can get the style index by adding styles that you create.

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 is your worksheet.

Note: This shouldn’t be done this way, but I can’t find another one.

Related Problems and Solutions