Xlsxwriter条件格式问题

问题描述 投票:2回答:2

我有这个条件格式如下所示:

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,并使用警告格式。我只希望具有归属于它的会计编号的字段具有两种格式之一。如果它是空的,它应该没有格式。

python conditional-formatting xlsxwriter
2个回答
1
投票

正如@martineau指出的那样,并且在the documentation中突出显示,将第二种格式的'type': 'cell'更改为'type': 'blanks'应该将over格式应用于所有没有值的单元格。

您还需要删除criteriavalue键:

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

1
投票

几乎每次我在XlsxWriter中回答有关条件格式的问题时,我都会说同样的事情:首先弄清楚如何在Excel中执行此操作然后将其应用于XlsxWriter。

似乎将空白单元格视为零的条件格式问题是Excel中的已知问题/特征。我按照this post中关于该问题的方法之一的建议,并为空白单元格设置了一个额外的默认格式。我还需要设置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()

输出:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.