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

风流意气都作罢 提交于 2020-04-11 11:37:25

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/47216433/how-to-read-the-font-and-background-color-in-excel-using-xlrd-version-1-1-0

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!