How to change the color based on text in excel using Python?

后端 未结 2 1467
感动是毒
感动是毒 2021-01-22 18:22

In Excel cell text will vary from Pass to Fail.I have to give background color green for Pass(pass/Passed/passed) and red for Fail(fail/Failed/failed) respectively. How to chang

2条回答
  •  忘掉有多难
    2021-01-22 18:57

    You'll need to put in a if statement to seperate pased on pass fail.

    Then, you'll use that to make a color string, something like 'fore-colour grey25'. Look in Style.py for lists of all possible colors and options (github page: https://github.com/python-excel/xlwt/blob/master/xlwt/Style.py). Since red and green both work, and back_color also works, you can do:

    passed = xlwt.easyxf('back_color green')
    failed = xlwt.easyxf('back_color red')
    
    color = (passed if passorfail in ['pass','Passed','passed'] else
        (failed if passorfail in ['fail','Failed','failed'] else xlwt.easyxf()))
    worksheet.write_merge(6, 6, 3, 3,passorfail, style = color)
    

提交回复
热议问题