使用 xlsx writer 的条件格式生成带有 "text:containing" 的损坏文件

Conditional formatting with xlsx writer generates corrupt file with "text:containing"

所以我有一些代码可以根据单元格值将条件格式应用于 Excel 文件。我想使用条件 "containing" 添加一些到基于 "text" 类型的相同范围。这些列充满了日期字符串,我想对包含“2017”的日期应用一种格式。

这是整个块,如果我注释掉文本的条件格式,它可以很好地处理单元格的条件格式:

mtbook = mytrials_writer.book
header_format = mtbook.add_format({'bg_color': '#7e98f7','bold': True})
notFoundFormat = sitebook.add_format({'bg_color':'red'})
notExpFormat = sitebook.add_format({'bg_color':'silver'})
foundFormat = sitebook.add_format({'bg_color':'lime'})
for worksheet in mtbook.worksheets():
    # for every column
    for i in range(len(subreportCols)):
        # write the value of the first cell in the column to the first cell of that column
        worksheet.write(0, i, subreportCols[i], header_format)
        worksheet.set_column(0, 50, 17)
        worksheet.set_row(0, 25, None)
        worksheet.conditional_format('A2:Z100', {'type':'text', 'criteria': 'containing', 'value':'2017','format':notFoundFormat})
        #worksheet.conditional_format('A2:Z100', {'type':'text', 'criteria': 'containing', 'value':'"2016-"','format':foundFormat})
        #worksheet.conditional_format('A2:Z100', {'type':'text', 'criteria': 'containing', 'value':'"2015-"','format':foundFormat})
        worksheet.conditional_format('A2:Z100', {'type':'cell', 'criteria': '==', 'value':'"Miss/Inc"','format':notFoundFormat})
        worksheet.conditional_format('A2:Z100', {'type':'cell', 'criteria': '==', 'value':'"NotExp."','format':notExpFormat})

如果我启用如下行,代码将 运行,但 Excel 文件将打开,询问我是否要修复,因为它已损坏;如果我说是,那么文档中的任何地方都没有格式。

worksheet.conditional_format('A2:Z100', {'type':'text', 'criteria': 'containing', 'value':'2017','format':notFoundFormat})

错误说,"We found a problem with some content in FILE. Do you want to try and recover as much as we can? If you trust the source of this workbook, click Yes"

这是返回的错误日志:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<recoveryLog xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"><logFileName>error255040_01.xml</logFileName><summary>Errors were detected in file 'C:\Users\mgancsos\Documents\Data Sources\Python\Testing\TQ_MyTrials_upload.xlsx'</summary><repairedRecords><repairedRecord>Repaired Records: Conditional formatting from /xl/worksheets/sheet1.xml part</repairedRecord><repairedRecord>Repaired Records: Conditional formatting from /xl/worksheets/sheet2.xml part</repairedRecord><repairedRecord>Repaired Records: Conditional formatting from /xl/worksheets/sheet3.xml part</repairedRecord><repairedRecord>Repaired Records: Conditional formatting from /xl/worksheets/sheet4.xml part</repairedRecord><repairedRecord>Repaired Records: Conditional formatting from /xl/worksheets/sheet5.xml part</repairedRecord><repairedRecord>Repaired Records: Conditional formatting from /xl/worksheets/sheet6.xml part</repairedRecord><repairedRecord>Repaired Records: Conditional formatting from /xl/worksheets/sheet7.xml part</repairedRecord><repairedRecord>Repaired Records: Conditional formatting from /xl/worksheets/sheet8.xml part</repairedRecord><repairedRecord>Repaired Records: Conditional formatting from /xl/worksheets/sheet9.xml part</repairedRecord><repairedRecord>Repaired Records: Conditional formatting from /xl/worksheets/sheet10.xml part</repairedRecord><repairedRecord>Repaired Records: Conditional formatting from /xl/worksheets/sheet11.xml part</repairedRecord><repairedRecord>Repaired Records: Conditional formatting from /xl/worksheets/sheet12.xml part</repairedRecord><repairedRecord>Repaired Records: Conditional formatting from /xl/worksheets/sheet13.xml part</repairedRecord><repairedRecord>Repaired Records: C</repairedRecord><repairedRecord>Repaired Records: C</repairedRecord><repairedRecord>Repaired Records: C</repairedRecord><repairedRecord xml:space="preserve">Repaired Records: </repairedRecord></repairedRecords></recoveryLog>

谢谢!

我认为问题在于您要匹配的字符串周围的双引号。试试这个:

worksheet.conditional_format('A2:Z100', 
    {'type': 'text', 
     'criteria': 'containing', 
     'value':'2016-',
     'format': foundFormat})