简体   繁体   English

如何使用xlrd版本1.1.0在Excel中读取字体和背景色

[英]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. 实际上,我使用的是xlrd模块1.1.0版本,但我不知道如何读取单元格属性,例如背景色,字体以及单元格是否被锁定。

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. t引发错误,表示在读取wb时需要设置格式设置信息,但是如果我有该参数,则表明仍未实现。

Is there another module or how can this module itself be made to read cell properties? 是否有另一个模块,或者该模块本身如何读取单元格属性?

python xlrd Thank you in advance python xlrd预先谢谢

DOCUMENTATION 文件资料

You need to use xf_index to get xlrd.formatting.XF object. 您需要使用xf_index来获取xlrd.formatting.XF对象。 Than use various indexes to get information from book object itself. 比使用各种索引从book对象本身获取信息。 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 colour_map一样: http : colour_map

Or, font_list : http://xlrd.readthedocs.io/en/latest/api.html#xlrd.book.Book.font_list 或者, font_listhttp : 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 由于我使用的是Libre Office,因此我无法完全测试它,并且文档中提到了Open Office衍生产品的一些问题: http : //xlrd.readthedocs.io/en/latest/api.html#xlrd.formatting.XFProtection

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用pythons xlrd模块从Excel工作表中读取 - How to read from an excel sheet using pythons xlrd module 如何使用xlrd在python中按列名读取Excel数据 - How to read Excel data by column name in python using xlrd 使用 xlrd 在 excel 中读取特殊字符 - Read special chars in an excel using xlrd 如何从excel文件和python中具有复杂表结构的数据中读取和识别颜色编码数据(字体和背景)? - How to read and identify color coded data(font and background) from excel file and data with complex table structure in python? 如何使用xlrd将Excel文件读入Python? 它可以读取更新的Office格式吗? - How do I read an Excel file into Python using xlrd? Can it read newer Office formats? 如何使用 xlrd 将 excel 转换为嵌套的 JSON? - How to convert an excel to a nested JSON using xlrd? 使用 XLRD 包识别 Excel Sheet 单元格颜色代码 - Identifying Excel Sheet cell color code using XLRD package 使用 xlrd 读取包含中文和/或印地语字符的 Excel xls 文件 - Using xlrd to read Excel xls file containing Chinese and/or Hindi characters Python:使用 xlrd 从 excel 中读取百分比值 - Python: read a percentage value from excel using xlrd 使用xlrd以时间格式而不是浮点数从excel表中读取时间 - Read time from excel sheet using xlrd, in time format and not in float
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM