因此,我有一些代碼將條件格式應用於基於單元格值的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>
謝謝!
不確定爲什麼你使用'文本'如果'細胞'的作品。聽起來像一個失敗的日期,所以你嘗試使用'日期'類型? – MrE
「包含」不是爲「單元格」列出的,僅限於「文本」。 http://xlsxwriter.readthedocs.io/working_with_conditional_formats.html?highlight=conditional – Korzak
你嘗試過約會嗎? – MrE