在XlsxWriter中使用条件格式时,最好先弄清楚要在Excel中执行什么操作,然后将其传输到XlsxWriter。在
在这种情况下,Excel不支持单元格与字符串相等。相反,你必须使用“文本”条件(或者可能是一个公式)。在
下面是代码的简化版本,它修复了一些小问题并执行了您想要的操作:import xlsxwriter
alldata = [['My Total', 'Data Matched!', '$824,499', '$824,499'],
['Second Total', 'Data Matched!', '$824,532', '$824,532'],
['Featured Articles', 'Data Matched!', '$391,153', '$391,610'],
['Ads Revenue', 'Data Not Matched!', '$825,513', '$825,492'],
['Company 1 Revenue', 'Data Not Matched!', '$824,263', '$824,965'],
['Company 2 Revenue', 'Data Not Matched!', '$176,711', '$239,801']]
workbook = xlsxwriter.Workbook('test.xlsx')
worksheet = workbook.add_worksheet()
worksheet.set_column('C:C', 14)
cell_format = workbook.add_format()
cell_format.set_font_color('red')
for row, row_data in enumerate(alldata):
worksheet.write_row(row + 1, 1, row_data)
worksheet.conditional_format('C2:C7', {'type': 'text',
'criteria': 'begins with',
'value': 'Data Matched!',
'format': cell_format})
workbook.close()
输出: