1

I have a small program, that looks into an excel file to say every cell its position (row, column) and its background color. the outprint is like:

row, col is: 1 1 Szakasz azonosító (192, 192, 192) row, col is: 1 2 67 None

I want to expand it with an if statement, to get only the grey cells. Something like if the pattern_colour is 128,128,128. I got trouble with the type of pattern_colour, becausethe white backgounded cells' type are 'NoneType', but the cloured are tuple. So the if statement gives back an error :

TypeError: argument of type 'NoneType' is not iterable

How can I solve this?

Here is the code:

import xlrd
book = xlrd.open_workbook("67_uj.xls", formatting_info=True)
sheets = book.sheet_names()
print "sheets are:", sheets

for index, sh in enumerate(sheets):
    sheet = book.sheet_by_index(index)
    print "Sheet:", sheet.name
    rows, cols = sheet.nrows, sheet.ncols
    print "Number of rows: %s   Number of cols: %s" % (rows, cols)
    for row in range(rows):
        for col in range(cols):
            print "row, col is:", row+1, col+1,
            thecell = sheet.cell(row, col)
            # could get 'dump', 'value', 'xf_index'
            print thecell.value,
            xfx = sheet.cell_xf_index(row, col)
            xf = book.xf_list[xfx]
            bgx = xf.background.pattern_colour_index
            pattern_colour = book.colour_map[bgx]
            #print bgx
            print pattern_colour
            print(type(pattern_colour))
            #aktiv
            if "128" in pattern_colour:
                print("YES it is")
krszt
  • 63
  • 1
  • 7

0 Answers0