David542
David542

Reputation: 110173

How to set color of text using xlwt

I haven't been able to find documentation on how to set the color of text. How would the following be done 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 have tried, where I've been able to set the font color but not the background color is:

style = xlwt.easyxf('font: bold 1, color red;')

Upvotes: 7

Views: 33441

Answers (7)

Cat
Cat

Reputation: 364

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

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

Upvotes: 0

AlexB
AlexB

Reputation: 1

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')

Upvotes: 0

Sandeep
Sandeep

Reputation: 29341

Alternative solution:

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

Upvotes: 3

Germain
Germain

Reputation: 21

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;')

Upvotes: 2

user1554987
user1554987

Reputation: 71

Set "colour_index" attribute.

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

Upvotes: 7

David542
David542

Reputation: 110173

This is what worked:

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

Upvotes: 8

Vassilis
Vassilis

Reputation: 2841

For the font color font.color should do it where for the cell background color there is a similar question:

python xlwt set custom background colour of a cell

Upvotes: 2

Related Questions