如何使用xlwt设置文本的颜色

时间:2013-03-26 23:43:39

标签: python xlrd xlwt

我无法找到有关如何设置文字颜色的文档。如何在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
???

我尝试过的另一种方法是,我可以设置字体颜色而不是背景颜色:

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

7 个答案:

答案 0 :(得分:7)

这是有用的:

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

答案 1 :(得分:6)

设置“colour_index”属性。

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

答案 2 :(得分:2)

对于字体颜色 font.color 应该在单元格背景颜色的位置进行类似的问题:

python xlwt set custom background colour of a cell

答案 3 :(得分:2)

我建议使用以下代码:

from xlwt import Workbook,XFStyle,Borders, Pattern, Font, easyxf

styleOK = easyxf('pattern: fore_colour light_blue;'
                          'font: colour green, bold True;')

答案 4 :(得分:2)

替代解决方案:

如果您可以使用colors defined in xlwt,请转到http://www.colorhexa.com/90ee90等颜色信息网站,获取最接近的匹配。

答案 5 :(得分:0)

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

答案 6 :(得分:0)

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)