简体   繁体   English

使用Python的xlsxwriter在Excel中将字符串条件格式设置为“等于”

[英]String conditional formatting “equal to” in Excel using Python's xlsxwriter

I have relatively big Excel spreadsheets, where I am applying conditional formatting. 我有相对较大的Excel电子表格,我在那里应用条件格式。 However, the content of a cell is relatively short (max 3 letters). 但是,单元格的内容相对较短(最多3个字母)。 So, I need to match exactly a string. 所以,我需要完全匹配一个字符串。 For example: 'A' should be formatted but nothing more containing 'A' ('ABC', 'BCA', 'BAC', etc.). 例如:'A'应格式化,但不能再包含'A'('ABC','BCA','BAC'等)。

I tried different options using 'text' and 'cell' options but I failed miserably. 我使用'text'和'cell'选项尝试了不同的选项,但我失败了。 Here is my test case: 这是我的测试用例:

import xlsxwriter

workbook = xlsxwriter.Workbook('conditional_format4.xlsx')
worksheet1 = workbook.add_worksheet()

format1 = workbook.add_format({'bg_color': '#FFC7CE',
                               'font_color': '#9C0006'})

data = [
    ['ABC', 'BCA', 38, 30, 75, 48, 75, 66, 84, 86],
    [6, 24, 1, 84, 54, 62, 60, 3, 26, 59],
    [28, 79, 97, 13, 85, 93, 93, 22, 5, 14],
    [27, 'BAC', 40, 17, 18, 79, 90, 93, 29, 47],
    [88, 'ABC', 33, 23, 67, 1, 59, 79, 47, 36],
    [24, 'A', 20, 88, 29, 33, 38, 54, 54, 88],
    [6, 'BCA', 88, 28, 10, 26, 37, 7, 41, 48],
    [52, 78, 1, 96, 26, 45, 47, 33, 96, 36],
    [60, 54, 81, 66, 81, 90, 80, 93, 12, 55],
    [70, 5, 46, 14, 71, 19, 66, 36, 41, 21],
]

for row, row_data in enumerate(data):
    worksheet1.write_row(row, 0, row_data)


worksheet1.conditional_format('A1:J10', {'type': 'text',
                                         'criteria': 'containing',
                                         'value': 'A',
                                         'format': format1})

workbook.close()

So, I want to match only one cell. 所以,我想只匹配一个单元格。 I run out of options/ideas. 我没有选择/想法。 It seems trivial but I am getting lots of errors that my Excel files are incorrect. 这似乎微不足道,但我收到很多错误,我的Excel文件不正确。 Hope someone found solution for this problem. 希望有人找到解决这个问题的方法。

It's done in Excel 2010 (Conditional Formatting -> Highlight Cells Rules -> Equal to: type A in textbox). 它在Excel 2010中完成(条件格式 - >突出显示单元格规则 - >等于:在文本框中键入A)。

The following XlsxWriter conditional format should work: 以下XlsxWriter条件格式应该有效:

worksheet1.conditional_format('A1:J10', {'type': 'cell',
                                         'criteria': '==',
                                         'value': '"A"',
                                         'format': format1})

Note that, as in Excel, the conditional type should be cell and the value should be a string (with quotes). 请注意,与在Excel中一样,条件类型应为cell ,值应为字符串(带引号)。

If I understand correctly, you want Python to match a single character. 如果我理解正确,您希望Python匹配单个字符。 So for example you want to match 'A' only and reject something like 'AB' and 'AA', before formatting. 因此,例如,您希望仅匹配“A”并在格式化之前拒绝类似“AB”和“AA”的内容。

For that you need to match character and length. 为此,您需要匹配字符和长度。

'A' in string and len('A') == len(string)

Then you can apply the formatting to the ones that match that criteria. 然后,您可以将格式应用于符合该条件的格式。

If you want to use xlswriter conditional_format, according to the documentation , you can imitate equal to by doing: 如果你想使用xlswriter conditional_format,根据文档 ,你可以模仿等于:

worksheet.conditional_format('B3:K12', {'type':     'cell',
                                        'criteria': '==',
                                        'value':    '"A"',
                                        'format':   format2})

EDITED error in value (that should be a string as pointed out by @jmcnamara) 值中的EDITED错误(应该是@jmcnamara指出的字符串)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM