2013-03-26 1649 views
6

我一直無法找到有關如何設置文本顏色的文檔。在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;') 

回答

8

這是什麼工作:

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

設置「colour_index」屬性。

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

我會建議使用下面的代碼:

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

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

row1.write(4, 'KO' ,styleKO) – Germain 2014-07-03 18:38:52

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

作爲解釋的一兩個句子會很好。 – 2017-03-17 08:28:12

+0

爲了讓我們知道我們可以用第一句創建表單,easyfx可以幫助您修復單元格。有必要運行st = xlwt.easyxf('pattern:pattern solid;'),然後您可以選擇模式的屬性。對於我不確定的顏色範圍,但是您可以在0至100之間打印它們並選擇一個。然後,您選擇要寫入的單元格並應用所創建的樣式。 – AlexB 2017-03-17 09:14:08

0

風格= xlwt.easyxf( '圖案:圖案固體,fore_colour light_blue;' '字體:白色,粗體真;')

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