excel - how to use python xlsxwriter to conditionally format on cell value -
i output conditional formatted excel worksheets each column condition dependent on value in first row. using shown data frame cells a2, a4, b3, b4, c4, c5, d4, , e4 highlighted red.
import xlsxwriter data = [ [1, 1, 5, 'a', 1], [1, 4, 4, 'b', 0], [2, 1, 5, 2, 2], [1, 1, 5, 'a', 1], ] wb = xlsxwriter.workbook('testout.xlsx') ws = wb.add_worksheet() formatyellow = wb.add_format({'bg_color':'#f7fe2e'}) formatred = wb.add_format({'bg_color':'#ff0000'}) i=0 row, row_data in enumerate(data): print (row) print (row_data) ws.write_row(row, 0, row_data) += 1 ws.conditional_format(0,1,4,5, {'type':'text', 'criteria':'containing', 'value':'b', 'format':formatyellow}) ws.conditional_format(0,1,4,5, {'type':'cell', 'criteria':'==', 'value':'a$1', 'format':formatred})
is there way of getting done using xlsxwriter?
i able produce desired result after changing indexing in ws.conditional_format
. remember xslxwriter
uses 0 indexing, row=0
, col=0
corresponds cell a0
. also, had add wb.close()
end of fuction
import xlsxwriter data = [ [1, 1, 5, 'a', 1], [1, 4, 4, 'b', 0], [2, 1, 5, 2, 2], [1, 1, 5, 'a', 1], ] wb = xlsxwriter.workbook('testout.xlsx') ws = wb.add_worksheet() formatyellow = wb.add_format({'bg_color':'#f7fe2e'}) formatred = wb.add_format({'bg_color':'#ff0000'}) i=0 row, row_data in enumerate(data): print (row) print (row_data) ws.write_row(row, 0, row_data) += 1 ws.conditional_format(1,0,3,4, {'type':'text', #start row 1 ("b") , column 0 (1) 'criteria':'containing', 'value':'b', 'format':formatyellow}) ws.conditional_format(1,0,3,4, {'type':'cell', 'criteria':'==', 'value':'a$1', 'format':formatred}) wb.close()
output:
Comments
Post a Comment