2017-09-18 61 views
1

因此,我有一些代码将条件格式应用于基于单元格值的Excel文件。我希望使用“包含”条件基于“文本”类型添加一些相同的范围。这些列填充了日期字符串,我想将格式应用于包含“2017”的日期。使用xlsx编写器进行条件格式生成具有“文本:包含”的损坏文件

这里是整个块,如果我注释掉条件格式文本正常工作与细胞的条件格式:

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}) 

错误说:“我们发现在文件中的一些内容有问题。你想尝试恢复多,我们可以吗?如果您信任该工作簿的来源,请单击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> 

谢谢!

+0

不确定为什么你使用'文本'如果'细胞'的作品。听起来像一个失败的日期,所以你尝试使用'日期'类型? – MrE

+0

“包含”不是为“单元格”列出的,仅限于“文本”。 http://xlsxwriter.readthedocs.io/working_with_conditional_formats.html?highlight=conditional – Korzak

+0

你尝试过约会吗? – MrE

回答

2

我认为问题是你想要匹配的字符串周围的双引号。试试这个:

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

这解决了它。之前的代码我没有工作,并在寻找解决方案时,我在另一篇文章中读到双引号是必要的。经过几次其他尝试后,我最终确定了这一点,但保留了双引号。拿出来解决这个问题。谢谢!!! – Korzak

相关问题