Search code examples
pythonexcelxlrd

Python / Excel - Conditional cell printing with xlrd


I want to print only the rows of a specifig column, let's say colmn B, so far so good:

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]

Now I want to print out only those cell values, which have a yellow colored background. I found this link but failed to adept it to my code. Could anybody help me out?


Solution

  • If you know the specific color index of the cells with yellow background, you can check the background.pattern_colour_index value of the cell style. Note that it is important to pass formatting_info=True to the open_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
    

    Example:

    For the file containing 2 cells with yellow background:

    enter image description here

    The code above prints:

    test2
    test4