How to set text color using xlwt

I could not find the documentation on how to set the text color. How to do the following in xlwt?

style = xlwt.XFStyle() # bold font = xlwt.Font() font.bold = True style.font = font # background color pattern = xlwt.Pattern() pattern.pattern = xlwt.Pattern.SOLID_PATTERN pattern.pattern_fore_colour = xlwt.Style.colour_map['pale_blue'] style.pattern = pattern # color of text ??? 

Another way I tried where I could set the font color but not the background color:

 style = xlwt.easyxf('font: bold 1, color red;') 
+6
source share
7 answers

This is what worked:

 style = xlwt.easyxf('pattern: pattern solid, fore_colour light_blue;' 'font: colour white, bold True;') 
+8
source

Set the attribute "color_index".

  style.font.colour_index = xlwt.Style.colour_map['white'] 
+5
source

For font color font.color , you should do this where a similar question arises for the background color of the cell:

python xlwt sets custom cell background color

+2
source

I would recommend using the following code:

 from xlwt import Workbook,XFStyle,Borders, Pattern, Font, easyxf styleOK = easyxf('pattern: fore_colour light_blue;' 'font: colour green, bold True;') 
+2
source

Alternative solution:

If you can leave with the colors defined in xlwt , go to a site with color information, for example http://www.colorhexa.com/90ee90 and get the closest match.

+2
source
 from xlwt import * import xlwt w = Workbook() sheet1= w.add_sheet('Test') st = xlwt.easyxf('pattern: pattern solid;') st.pattern.pattern_fore_colour = 20 #random color number sheet1.write(0, 0,'Random Text',st) w.save('Random_xls.xls') 
0
source

style = xlwt.easyxf ('pattern: pattern solid, fore_colour light_blue;' 'font: color white, bold True;')

  sheet.write(row, 0, "Code", style) sheet.write(row, 1, "Name", style) sheet.write(row, 2, "bottle",style) 
0
source

All Articles