Alternating row color using xlsxwriter in Python 3

前端 未结 2 1980
孤街浪徒
孤街浪徒 2021-02-20 03:36

Has anybody implemented alternating row color while generating excel using xlsxwriter in Python3?

data_format = workbook.add_format(
    {
        \'bg_color\':          


        
2条回答
  •  遇见更好的自我
    2021-02-20 03:46

    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()
    

提交回复
热议问题