I have two columns containing 2017 values and 2018 values. I want to color column having 2018 values based on comparison with 2017 values
- 2018 cell as RED if 2018 cell value <2017 cell value
- 2018 cell as GREEN if 2018 cell value > 2017 cell value
- 2018 cell as ORANGE if 2018 cell value = 2017 cell value
I was able to use conditional formatting , but for that I had to create a new column which was comparing column A and column B
worksheet.write('C' + str(rownum + 1), np.where((float(data['2018 YTD'].values[0])>float(data['2017 YTD'].values[0])),2,np.where((float(data['2018 YTD'].values[0])>float(data['2017 YTD'].values[0])),0,1)), style)
worksheet.conditional_format('C2:C5',{'type': 'icon_set','icon_style': '3_arrows' })
But using this code I had to create one more column(Column C) here, instead I am looking to highlight Column B directly with colour codes.
Edit 1## Following Wald's advice:
ws = wb.active
redFill = PatternFill(start_color='FFFF0000', end_color='FFFF0000', fill_type='solid')
GreenFill = PatternFill(start_color='EE1111', end_color='EE1111', fill_type='solid')
if(ws['B2']>ws['A2']):
ws['B2'].fill=redFill
else(ws['B2']<ws['A2']):
ws['B2'].fill=GreenFill
wb.save("sample.xlsx")
But it gave the below error:
TypeError: '>' not supported between instances of 'Cell' and 'Cell'
It seems in this package > or < comparison is not supported