使用 XLRD 包识别 Excel 工作表单元格颜色代码

发布于 2024-12-13 05:22:22 字数 172 浏览 0 评论 0原文

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

I am writing a python script to read data from an excel sheet using xlrd. Few of the cells of the the work sheet are highlighted with different color and I want to identify the color code of the cell. Is there any way to do that ? An example would be really appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

删除→记忆 2024-12-20 05:22:22

处理此问题的一种方法如下:有关

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 群组

Here is one way to handle this:

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

More info on the Python-Excel Google Group.

岁吢 2024-12-20 05:22:22

JMax 建议的解决方案仅适用于 xls 文件,不适用于 xlsx 文件。这会引发 NotImplementedError:formatting_info=True not Yet ImplementedXlrd 库仍未更新以适用于 xlsx 文件。因此,您必须每次都另存为并更改格式,这可能不适合您。
这是使用 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

The Solution suggested by JMax works only for xls file, not for xlsx file. This raises a NotImplementedError: formatting_info=True not yet implemented. Xlrd library is still not updated to work for xlsx files. So you have to Save As and change the format every time which may not work for you.
Here is a solution for xlsx files using openpyxl library. A2 is the cell whose color code we need to find out.

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
与君绝 2024-12-20 05:22:22

此函数返回元组中单元格背景的 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

This function returns cell background's rgb value in tuple.

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
总攻大人 2024-12-20 05:22:22
# 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)

# 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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文