繁体   English   中英

使用 XLRD 包识别 Excel Sheet 单元格颜色代码

[英]Identifying Excel Sheet cell color code using XLRD package

我正在编写一个 python 脚本来使用xlrd从 Excel 工作表中读取数据。 工作表的几个单元格用不同的颜色突出显示,我想识别单元格的颜色代码。 有没有办法做到这一点? 一个例子将不胜感激。

这是处理此问题的一种方法:

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_xf_index(row, col)
            xf = book.xf_list[xfx]
            bgx = xf.background.pattern_colour_index
            print bgx

有关Python-Excel Google Group 的更多信息。

JMax 建议的解决方案仅适用于xls文件,不适用于xlsx文件。 这会引发NotImplementedError: formatting_info=True not yet implemented Xlrd库仍未更新为适用于xlsx文件。 因此,您每次都必须Save As并更改格式,这可能对您不起作用。
这是使用openpyxl库的xlsx文件的解决方案。 A2是我们需要找出其颜色代码的单元格。

import openpyxl
from openpyxl import load_workbook
excel_file = 'color_codes.xlsx' 
wb = load_workbook(excel_file, data_only = True)
sh = wb['Sheet1']
color_in_hex = sh['A2'].fill.start_color.index # this gives you Hexadecimal value of the color
print ('HEX =',color_in_hex) 
print('RGB =', tuple(int(color_in_hex[i:i+2], 16) for i in (0, 2, 4))) # Color in RGB

此函数以元组形式返回单元格背景的 rgb 值。

def getBGColor(book, sheet, row, col):
    xfx = sheet.cell_xf_index(row, col)
    xf = book.xf_list[xfx]
    bgx = xf.background.pattern_colour_index
    pattern_colour = book.colour_map[bgx]

    #Actually, despite the name, the background colour is not the background colour.
    #background_colour_index = xf.background.background_colour_index
    #background_colour = book.colour_map[background_colour_index]

    return pattern_colour

有问题
“”“行,cols = sheet.nrows,sheet.ncols”“”

行数超过了电子表格中的行数。 有很多重复的行。

问题是什么? 谢谢。

# say you have an Excel file called workbook
 and inside it there is a worksheet called worksheet

# inside that worksheet is cell say C1 that is highlighted
 and you want to get the color value of the highlighted cell

# Trying the openpyxl package
import openpyxl

# Importing all modules from the openpyxl package
from openpyxl import *

# reading ev2 excel workbook through load_workbook function
workbook = load_workbook("C:PATHTO/workbook.xlsx")

# Accessing existing worksheets
worksheet = workbook ["worksheet"]

## METHOD1 ##

# Getting the highlight property of the cell
highlight=str(worksheet ['C1'].fill)

# Printing out the value of the color
index=int(highlight.find("rgb='"))
print(highlight[index+5:index+13])

## METHOD 2 ##

print(worksheet ['C11'].fill.start_color.index)

暂无
暂无

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

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