Has anybody implemented alternating row color while generating excel using xlsxwriter in Python3?
data_format = workbook.add_format(
{
\'bg_color\':
I think this is cleaner - if just want fill-in the cells with two-colors alternation
import xlsxwriter
workbook = xlsxwriter.Workbook('sample.xlsx')
worksheet = workbook.add_worksheet()
bg_format1 = workbook.add_format({'bg_color': '#78B0DE'}) # blue cell background color
bg_format2 = workbook.add_format({'bg_color': '#FFFFFF'}) # white cell background color
for i in range(10): # integer odd-even alternation
worksheet.set_row(i, cell_format=(bg_format1 if i%2==0 else bg_format2))
# (or instead) if you want write and paint at once
# worksheet.write(i, 0, "sample cell text", (bg_format1 if i%2==0 else bg_format2))
workbook.close()