Given a row and a column (plus sheet name) I need a way to extract the rgb value for a cell, as reported within excel, from a python script.
I've been using openpyxl. The following do not work consistently:
cell.fill.fgColor.rgb
cell.fill.bgColor.rgb
The following give the wrong value:
openpyxl.styles.colors.COLOR_INDEX[cell.fill.fgColor.index]
openpyxl.styles.colors.COLOR_INDEX[cell.fill.fgColor.index]
So I'm looking for another way to do it. Possible solutions need not be limited to the openpyxl library, as I'm willing to use another one.
Consider win32com
library if using Python for Windows as it can access the Excel Object Library and use any of its methods and properties such as Workbooks.Open
, Interior.Color
, Workbook.Close
.
Using this interesting link for RGB value extraction, you can translate VBA to Python. Specifically, VBA's integer division operator, the backslash, \
, translates to Python's double forward slash, //
and the modulo operator in VBA mod
becomes Python's %
Below outputs the RGB of a yellow-colored cell. Both cell reference types are included. Entire routine is wrapped in a try/except/finally
to close background processes and release resources with or without a runtime error.
import win32com.client as win32
def getRGB(xlCell):
C = xlCell.Interior.Color
R = C % 256
G = C // 256 % 256
B = C // 65536 % 256
return "R={}, G={}, B={}".format(R, G, B)
try:
xlApp = win32.gencache.EnsureDispatch('Excel.Application')
wb = xlApp.Workbooks.Open('C:\\Path\\To\\Workbook.xlsx')
ws = wb.Worksheets('RESULTS')
print(getRGB(ws.Range("A2"))) # A1 Style Reference
# R=255, G=255, B=0 (yellow-colored cell)
print(getRGB(ws.Cells(2,1))) # R1C1 Style Reference
# R=255, G=255, B=0 (yellow-colored cell)
wb.Close(False)
xlApp.Visible = False
except Exception as e:
print(e)
finally:
xlApp.Quit
ws = None
wb = None
xlApp = None
Aside, do note VBA is NOT part of MS Excel but an external component (by default connected to the software). It is just another language to connect to Excel's object library as any programming language demonstrated here with Python can make a COM interface to this library.