2014-06-21 39 views
2

我想只打印specifig列的行有條件的小區打印,比方說colmn B,到目前爲止好:的Python/Excel中 - 與xlrd

import xlrd 
file_location = "/home/myuser/excel.xls" 
workbook = xlrd.open_workbook(file_location) 
sheet = workbook.sheet_by_index(0) 
data = [[sheet.cell_value(r, c) for c in range(sheet.ncols)] for r in range(sheet.nrows)] 

for r in data: 
    print r[1] 

現在我想打印出來只有那些細胞值,它們具有黃色背景。 我發現這個link,但未能熟練使用我的代碼。有人能幫我嗎?

回答

2

如果您知道具有黃色背景的單元格的特定顏色索引,則可以檢查單元格樣式的background.pattern_colour_index值。注意,通過formatting_info=Trueopen_workbook()是很重要的:

import xlrd 

file_location = "/home/myuser/excel.xls" 
workbook = xlrd.open_workbook(file_location, formatting_info=True) 
sheet = workbook.sheet_by_index(0) 

for row in range(sheet.nrows): 
    cell = sheet.cell(row, 1) 
    style = workbook.xf_list[cell.xf_index] 
    color = style.background.pattern_colour_index 
    if color == 43: # on of yellows 
     print cell.value 

實施例:

對於含有2個細胞與黃色背景的文件:

enter image description here

上述代碼版畫:

test2 
test4 
+0

酷!非常感謝你! – royskatt