[英]Conditional formatting with xlsx writer generates corrupt file with “text:containing”
So I have some code that applies conditional formatting to an Excel file based on cell values. 因此,我有一些代码将基于单元格值的条件格式应用于Excel文件。 I wanted to add some to the same range based on the "text" type using the criteria "containing".
我想使用“包含”条件根据“文本”类型将一些添加到相同的范围。 These columns are filled with strings of dates, and I wanted to apply a format to dates that contain "2017".
这些列中填充了日期字符串,我想对包含“ 2017”的日期应用一种格式。
Here's the whole block, and it works fine with conditional formatting of the cells if I comment out the conditional formatting for the text: 这是整个块,如果我注释掉文本的条件格式,则可以很好地处理单元格的条件格式:
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})
If I enable the lines like the following, the code will run, but the Excel file will open, ask if I want to repair because it's corrupt; 如果启用以下行,则代码将运行,但是Excel文件将打开,询问是否由于损坏而要修复; if I say yes, then there's no formatting anywhere in the doc.
如果我说是的话,那么文档中的任何地方都不会格式化。
worksheet.conditional_format('A2:Z100', {'type':'text', 'criteria': 'containing', 'value':'2017','format':notFoundFormat})
The error says, "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" 错误消息为:“我们在FILE中发现某些内容存在问题。是否要尝试尽可能多地恢复?如果您信任此工作簿的来源,请单击“是”。
This is the error log that is returned: 这是返回的错误日志:
<?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>
Thank you! 谢谢!
I think the issue is the double quotes around the string you want to match. 我认为问题是您要匹配的字符串周围的双引号。 Try this instead:
尝试以下方法:
worksheet.conditional_format('A2:Z100',
{'type': 'text',
'criteria': 'containing',
'value':'2016-',
'format': foundFormat})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.