Python xlrd读取Excel背景色

**

Python xlrd读取Excel背景色

**
使用Python的xlrd模块读取Excel的单元格背景色。
由于在读取Excel中数据时,根据表格中背景色不同颜色的数据,需要分类处理,所以此时需要读取单元格的背景色并加以判断。
1、 在通过Python的xlrd模块读取Excel单元格背景色之前,先说明能处理的Excel类型。
原网址:https://blog.csdn.net/weixin_42326802/article/details/101520242
获取单个cell单元格详细格式信息的情况。
想要读取格式信息,在打开Excel文档时要将formatting_info设置为True(默认为False),设置为True则会读取Excel文档详细的格式信息,这样程序会消耗更多的内存,所以在不必要的情况下一般设置为False。

book = xlrd.open_workbook('your-file.xls', formatting_info=True)

目前的xlrd模块只支持以formatting_info=True打开xls格式文件,不支持xlsx格式,否则会抛出异常:
raise NotImplementedError(“formatting_info=True not yet implemented”)
2、 如果我们要操作的文件刚好是xlsx格式,又想保存其原有的格式该怎么办呢?
原网址:https://www.cnblogs.com/Detector/p/8709362.html
2.1. 修改为xlsx为xls(推荐)
将xlsx另存为xls,然后再进行后续操作,亲测有效,能正常保存Excel原有格式, 不用修改代码。
在这里插入图片描述
2.2. 改用 openpyxl
coding尝试读取文件,处理速度真的很慢…而且规则和宏全部丢失。

2.3. 使用pywin32
这是用于Win32 (pywin32)扩展的Python扩展库,它提供了对许多来自Python的Windows api的访问。

2.4. 使用老旧的版本 xlrd-0.6.1
使用xlrd-0.6.1可以读取,没有异常抛出。直到我传入其他几个xls文件,出现Expected BOF record; found 0x4b50 错误,原因是xlrd-0.6.1不支持office2007
3、 最后便可以通过Python xlrd模块读取Excel数据及其单元格信息了。
原网址:https://www.penwatch.net/cms/excel_cell_bg_color/

def cell_background_is_yellow ( workbook, cell ):
    # Returns TRUE if the given cell from the given workbook has a yellow RGB (255,255,0) background.
    # Note that the workbook must be opened with formatting_info = True, i.e.
    #     xlrd.open_workbook(xls_filename, formatting_info=True)
    assert type (cell) is xlrd.sheet.Cell
    assert type (workbook) is xlrd.book.Book

    xf_index = cell.xf_index
    test.append(xf_index)
    if xf_index!=None:
        xf_style = workbook.xf_list[xf_index]
        xf_background = xf_style.background

        fill_pattern = xf_background.fill_pattern
        pattern_colour_index = xf_background.pattern_colour_index
        background_colour_index = xf_background.background_colour_index

        pattern_colour = workbook.colour_map[pattern_colour_index]
        background_colour = workbook.colour_map[background_colour_index]

        # If the cell has a solid cyan background, then:
        #  - fill_pattern will be 0x01
        #  - pattern_colour will be cyan (0,255,255)
        #  - background_colour is not used with fill pattern 0x01. (undefined value)
        #    So despite the name, for a solid fill, the background colour is not actually the background colour.
        # Refer https://www.openoffice.org/sc/excelfileformat.pdf S. 2.5.12 'Patterns for Cell and Chart Background Area'
        if fill_pattern == 0x01 and pattern_colour == (255,255,0):
            return True
    return False
book = xlrd.open_workbook('your-file.xls', formatting_info=True)
table=data.sheet_by_index(0)
list_one=[]
list_two=[]
for row in range(table.nrows):
    if row>0:
        get_line=table.row_values(row)
        if get_line[0]!='' and get_line[0] not in door_system:
            if cell_background_is_yellow(data,table.cell(row,0)):
                list_one.append(get_line[0])
            else:
                list_two.append(get_line[0])

嗯,大概就这么多吧,亲测有效。

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值