Color entire row using xlwt

3,446 views
Skip to first unread message

Vinod k

unread,
Mar 28, 2011, 9:06:31 PM3/28/11
to python-excel
The util talks about a funtion called this

style0 = xlwt.easyxf(
'font: name Times New Roman, colour_index red'
)

there is color red instead of color_index too .

Is there any provision to color the entire row?

John Machin

unread,
Mar 29, 2011, 5:44:07 PM3/29/11
to python...@googlegroups.com
On Tue, March 29, 2011 12:06 pm, Vinod k wrote:
> The util talks about a funtion called this

"util"? Do you mean "tutorial"?

> style0 = xlwt.easyxf(
> 'font: name Times New Roman, colour_index red'
> )
>
> there is color red instead of color_index too .

color, colour, color_index, and colour_index are synonyms

> Is there any provision to color the entire row?

For "color the entire row", you may want the "pattern" colour (background)
rather than the font colour (the colour used for the displayed text). In
any case, you need to set up a style with what you want, use that for
every write that you do in the target row, AND set the row default to that
style as well (so that empty cells also get the treatment). The folloing
example illustrates this:

import xlwt
special_style = xlwt.easyxf(
"pattern: fore_colour red, pattern solid;"
"font: colour yellow;"
)
default_style = xlwt.easyxf("")
wb = xlwt.Workbook()
ws = wb.add_sheet("x")
for rowx, style in enumerate((special_style, special_style, default_style,
default_style)):
ws.write(rowx, 0, "A", style)
ws.write(rowx, 1, None, style) # column B is "blank" (formatting but
no data)
ws.write(rowx, 2, "C", style)
# column D is empty (no data, no formatting)
ws.write(rowx, 4, "E", style)
if rowx % 2:
ws.row(rowx).set_style(special_style)
wb.save('row_colour.xls')


Reply all
Reply to author
Forward
0 new messages