在URL
http://xlsxwriter.readthedocs.org/en/latest/example_conditional_format.html上修改XLSXWriter示例代码
我建议您在数据中进行迭代时使用测试中的行值进行格式化。例如…
写入数据时:
################################################################################# Example 9.#caption = ('Rows with odd numbers are in light red. ''Rows with even numbers are in light green.')# Write the data.worksheet9.write('A1', caption)for row, row_data in enumerate(data): if row%2 == 0: worksheet9.write_row(row + 2, 1, row_data, format1) else: worksheet9.write_row(row + 2, 1, row_data, format2)写入数据后:
################################################################################# Example 10.##caption = ('Rows with odd numbers are in light red. ''Rows with even numbers are in light green.')## Write the data.worksheet10.write('A1', caption)##for row, row_data in enumerate(data): worksheet10.write_row(row + 2, 1, row_data)### Write a conditional format over a range.for row, row_data in enumerate(data): if row%2 == 0: worksheet10.set_row(row + 2, None, format1) else: worksheet10.set_row(row + 2, None, format2)根据https://support.office.com/zh-CN/article/Apply-shading-to-alternate-rows-
in-a-
worksheet-a443b0f5-2025-42f6-9099-5de09c05e880,Microsoft提供了两种方法来实现交替行格式:条件格式或表样式“绑定行”。
使用公式的条件格式
=MOD(ROW(),2)=0已完成,但是Excel 2013无法解释它。
# Write a conditional format over a range DOES NOT WORKworksheet1.conditional_format('A1:K12', {'type': 'cell', 'criteria': '=MOD(ROW(),2)', 'value': 0, 'format': format1})# Write another conditional format over the same range also DOES NOT WORKworksheet1.conditional_format('A1:K12', {'type': 'cell', 'criteria': '=MOD(ROW(),2)', 'value': 1, 'format': format2})


