簡體   English   中英

xlsxwriter 條件格式問題

[英]Xlsxwriter conditional formatting problem

我有如下所示的條件格式:

worksheet_budget.conditional_format('D2:D100', {'type': 'cell',
                                            'criteria': 'between',
                                            'minimum': 0,
                                            'maximum': 100,
                                            'format': caution})

worksheet_budget.conditional_format('D2:D100', {'type': 'cell',
                                           'criteria': '<',
                                           'value': 0,
                                           'format': over})

它的意圖是對於每個小於 0 的值,將格式切換到大於和 0 - 100 之間,到警告格式。 然而,最終發生的是,對於沒有值的字段,它會將其視為 0 並使用警告格式。 我只希望有會計編號的字段具有兩種格式之一。 如果它是空的,它應該沒有格式。

正如@martineau 指出的那樣,並在文檔中突出顯示, 第二種格式中'type': 'blanks' 'type': 'cell'更改為'type': 'blanks'應該將over格式應用於沒有值的所有單元格。

您還需要刪除criteriavalue鍵:

worksheet_budget.conditional_format('D2:D100', {'type': 'blanks',
                                       'format': over})

幾乎每次我在 XlsxWriter 中回答有關條件格式的問題時,我都會說同樣的話:首先弄清楚如何在 Excel 中進行操作,然后將其應用到 XlsxWriter。

似乎條件格式將空白單元格視為零的問題是 Excel 中的一個已知問題/功能。 我遵循了這篇文章中有關該問題的方法之一的建議,並為空白單元格設置了額外的默認格式。 我還需要設置stop_if_true屬性。 這是一個工作示例:

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

輸出:

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM