2 回答

TA貢獻(xiàn)1865條經(jīng)驗(yàn) 獲得超7個(gè)贊
正如@martineau 指出的那樣,并在文檔中突出顯示,將'type': 'cell'第二種格式更改為'type': 'blanks'應(yīng)將over格式應(yīng)用于所有沒有值的單元格。
您還需要?jiǎng)h除criteria和value鍵:
worksheet_budget.conditional_format('D2:D100', {'type': 'blanks',
'format': over})

TA貢獻(xiàn)1780條經(jīng)驗(yàn) 獲得超1個(gè)贊
幾乎每次我在 XlsxWriter 中回答有關(guān)條件格式的問題時(shí),我都會(huì)說同樣的話:首先弄清楚如何在 Excel 中進(jìn)行操作,然后將其應(yīng)用到 XlsxWriter。
將空白單元格視為零的條件格式問題似乎是 Excel 中的一個(gè)已知問題/功能。我遵循了這篇文章中有關(guān)該問題的方法之一的建議,并為空白單元格設(shè)置了額外的默認(rèn)格式。我還需要設(shè)置stop_if_true屬性。這是一個(gè)工作示例:
import xlsxwriter
workbook = xlsxwriter.Workbook('conditional_format.xlsx')
worksheet = workbook.add_worksheet()
# Add a format. Light red fill with dark red text.
format1 = workbook.add_format({'bg_color': '#FFC7CE',
'font_color': '#9C0006'})
# Add a format. Green fill with dark green text.
format2 = workbook.add_format({'bg_color': '#C6EFCE',
'font_color': '#006100'})
# Add a default format.
format3 = workbook.add_format()
# Some sample data to run the conditional formatting against.
data = [
[34, -75, None, 75, 66, 84, 86],
[6, 24, 1, 60, 3, 26, 59],
[None, 79, 97, -13, 22, 5, 14],
[-27, -71, None, 17, 18, 0, 47],
[88, 25, -33, 23, 67, "", 36],
['', 100, 20, 88, 54, 54, 88],
[6, 57, '', 28, 10, 41, 48],
[52, 78, -1, 96, 26, 0, ""],
[60, -54, 81, None, 81, 90, 55],
[70, 5, 46, 14, 71, 41, 21],
]
for row, row_data in enumerate(data):
worksheet.write_row(row + 2, 1, row_data)
worksheet.conditional_format('B3:H12', {'type': 'blanks',
'stop_if_true': True,
'format': format3})
worksheet.conditional_format('B3:H12', {'type': 'cell',
'criteria': 'between',
'minimum': 0,
'maximum': 100,
'format': format1})
worksheet.conditional_format('B3:H12', {'type': 'cell',
'criteria': '<',
'value': 0,
'format': format2})
workbook.close()
輸出:
添加回答
舉報(bào)