2016-06-15 33 views
0

我正在嘗試將工作腳本寫入Excel中,以便將excel寫入excel。我不得不格式化列的大小和類型,但是我需要其中一列有條件顏色。
ex./ A列有百分比。 對於所有細胞< 0%,我們所需要的細胞是紅色 對於所有細胞= 0%,我們所需要的細胞是黃 對於所有細胞> 0%,我們所需要的細胞是綠色python中的條件背景顏色 - python中的新增功能

而且那麼,每列的第一行需要是藍色的。

我發現腳本類似,但它們不是有條件的。

我試過的

# Light red fill. 
format1 = name.add_format({'bg_color': '#FFC7CE'}) 

# Light yellow fill. 
format2 = name.add_format({'bg_color': '#FFEB9C'}) 

# Green fill. 
format3 = name.add_format({'bg_color': '#C6EFCE'}) 

try: 
    sheet1.conditional_format('A:A', {'type': 'cell', 
             'criteria': '<', 
             'value': 0, 
             'format': format1}) 


    sheet1.conditional_format('A:A', {'type': 'cell', 
             'criteria': '==', 
             'value': 0, 
             'format': format2}) 

    sheet1.conditional_format('A:A', {'type': 'cell', 
             'criteria': '>', 
             'value': 0, 
             'format': format3}) 

except AttributeError: 
    sheet1.conditional_format = None 

except TypeError: 
    type = None 

有一些變化,但不會改變顏色

,我已經試過的

while 'BU:BU' != " " : 
    if 'BU:BU' < '0%': 
     sheet1.style = redFill 
    elif 'BU:BU' == '0%': 
     sheet1.style = yellowFill 
    elif 'BU:BU' > '0%': 
     sheet1.style = '#C6EFCE' 
    else: 
     pass 

continue 

有一些變化,但是,保持陷入無限循環。

謝謝

回答

0

我想出了問題。爲了這個工作,三種格式不能在同一個單元格中。除了例外情況以外,每一個都需要獨立。

此外,邊界需要放在它運行多少個單元格上。如果這些東西會隨着行數的變化而多次運行,您可以拍攝高點。

一個小區:

# Light red fill. 
format1 = name.add_format({'bg_color': '#FFC7CE'}) 

try: 
    sheet1.conditional_format('A1:A900', {'type': 'cell', 
             'criteria': '<', 
             'value': 0, 
             'format': format1}) 

except AttributeError: 
    sheet1.conditional_format = None 

except TypeError: 
    type = None 

另一個細胞:

# Light yellow fill. 
format2 = name.add_format({'bg_color': '#FFEB9C'}) 

...