在Python 3中使用xlsxwriter交替行颜色

有没有人在
Python3中使用xlsxwriter生成excel时实现交替行颜色?

data_format = workbook.add_format(
    {
        'bg_color': '#FFC7CE'
    })

worksheet.write(data_row, data_col + 1, row[1], data_format)

这将设置每列的颜色.

最佳答案 没有什么能阻止您手动设置格式,如下所示:

import xlsxwriter

workbook = xlsxwriter.Workbook('hello.xlsx')
worksheet = workbook.add_worksheet()

data_format1 = workbook.add_format({'bg_color': '#FFC7CE'})
data_format2 = workbook.add_format({'bg_color': '#00C7CE'})

for row in range(0, 10, 2):
    worksheet.set_row(row, cell_format=data_format1)
    worksheet.set_row(row + 1, cell_format=data_format2)
    worksheet.write(row, 0, "Hello")
    worksheet.write(row + 1, 0, "world")

workbook.close()

这将为您提供如下输出:

《在Python 3中使用xlsxwriter交替行颜色》

点赞