2016-09-26 36 views
1

这是我的代码:xlsxwriter条件格式:找到准确值

 import xlsxwriter 

     for val in arr: 
     sheet.conditional_format('B2:B2000', {'type':  'text', 
             'criteria': 'containingwith', 
             'value': val, 
             'format': formatOk}) 

,我的问题是:我如何得到准确值。例如:
如果我在Excel中的值是:aaa,aab,aa。我的val是:aa,所以结果只会是aa。 和其他问题:我如何得到这部分中的列数:B2:B2000,并且没有写入随机值。 谢谢。

回答

0

类似下面应该工作:

worksheet1.conditional_format('B2:K12', {'type':  'cell', 
             'criteria': '==', 
             'value': '"aa"', 
             'format': format1}) 

更新:工作例如:

import xlsxwriter 

workbook = xlsxwriter.Workbook('conditional_format.xlsx') 
worksheet = workbook.add_worksheet() 

# Add a format. 
my_format = workbook.add_format({'bg_color': '#FFC7CE', 
           'font_color': '#9C0006'}) 

# Add some data to the sheet. 
data = [ 
    ['Foo', 'Mood', 'Fool', 'Foo'], 
    ['Boo', 'Mood', 'Bool', 'Boo'], 
    ['Goo', 'Good', 'Gool', 'Foo'], 
    ['Foo', 'Food', 'Fool', 'Zoo'], 
] 

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

# Add a conditional format. 
worksheet.conditional_format('A1:D4', {'type':  'cell', 
             'criteria': '==', 
             'value': '"Foo"', 
             'format': my_format}) 

# Close the file. 
workbook.close() 

输出:

enter image description here

+0

它不工作好。 – ravit

+0

它应该工作。看到我上面的工作示例。如果它不适合你,你应该像我一样发布一个[Minimal,Complete,and Verifiable example](http://stackoverflow.com/help/mcve)。 – jmcnamara

+0

感谢它的工作:) – ravit