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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15649482/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 20:37:27  来源:igfitidea点击:

How to set color of text using xlwt

pythonxlrdxlwt

提问by David542

I haven't been able to find documentation on how to set the color of text. How would the following be done in 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
???

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

回答by Vassilis

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

对于字体颜色font.color应该这样做,对于单元格背景颜色有一个类似的问题:

python xlwt set custom background colour of a cell

python xlwt 设置单元格的自定义背景颜色

回答by David542

This is what worked:

这是有效的:

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

回答by user1554987

Set "colour_index" attribute.

设置“color_index”属性。

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

回答by Germain

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

回答by Sandeep

Alternative solution:

替代解决方案:

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

如果您可以使用xlwt 中定义颜色,请访问诸如http://www.colorhexa.com/90ee90 之类的颜色信息站点并获取最接近的匹配项。

回答by AlexB

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

回答by Alice

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

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)