简体   繁体   中英

How to read the Font and Background color in excel using xlrd version 1.1.0

Actually I am using xlrd module 1.1.0 version, but I don't know how to read cell properties like background color, font, and whether cell is locked.

I tried to use

import xlrd
book = xlrd.open_workbook("sample.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_`enter code here`xf_index(row, col)
            xf = book.xf_list[xfx]
            bgx = xf.background.pattern_colour_index
            print bgx

t raises an error saying formatting information needs to be set while reading wb, but if I had that parameter then it shows it is still not implemented.

Is there another module or how can this module itself be made to read cell properties?

python xlrd Thank you in advance

DOCUMENTATION

You need to use xf_index to get xlrd.formatting.XF object. Than use various indexes to get information from book object itself. Cause majority of actual styling information (like colors) are stored inside book. All other elements just have indexes pointing to book's data lists or dictionaries:

Like, colour_map : http://xlrd.readthedocs.io/en/latest/api.html#xlrd.book.Book.colour_map

Or, font_list : http://xlrd.readthedocs.io/en/latest/api.html#xlrd.book.Book.font_list

CODE

I think you are looking for something like that:

import xlrd


book = xlrd.open_workbook("sample.xls", formatting_info=True)

def get_front_color(xf):
    font = book.font_list[xf.font_index]
    if not font:
        return None

    return get_color(font.colour_index)


def get_back_color(xf):
    if not xf.background:
        return None

    return get_color(xf.background.background_colour_index)


def get_color(color_index):
    return book.colour_map.get(color_index)


def get_if_protected(xf):
    if not xf.protection:
        return False

    return xf.protection.cell_locked


sheets = book.sheet_names()
for index, sh in enumerate(sheets):
    sheet = book.sheet_by_index(index)
    print "Sheet:", sheet.name
    rows, cols = sheet.nrows, sheet.ncols
    for row in range(rows):
        for col in range(cols):
            c = sheet.cell(row, col)
            xf = book.xf_list[c.xf_index]

            print u'{},{}:{:>6}: FRONT: {:>20} | BACK: {:>20} | LOCKED: {}'.format(
                row, col, c.value, get_front_color(xf), get_back_color(xf), get_if_protected(xf)
            )

Warning: I am not sure about locked flag though. I can not fully test it since I am using Libre Office and documentation mentions some problems with Open Office derivatives: http://xlrd.readthedocs.io/en/latest/api.html#xlrd.formatting.XFProtection

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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