- Python 3.14 on Windows 2025 v24H2
Is it ok to post something that works here? This is how to get the background/fill colors of an Excel spreadsheet.
The openpyxl routine I have for testing:
def readexcel(xlfn):
r"""Color results are inconsistent.
-xlfn: Excel filename
"""
from openpyxl import load_workbook
procname = str(inspect.stack()[0][3]) + ":"
rownum = 1
colnum = 1
try:
# Load workbook with all tabs.
wb = load_workbook(xlfn, data_only=True)
except Exception as e:
print(f"{procname} ERROR-openss: {e}")
ws = wb["Sheet1"] # Get worksheet.
for row in ws.iter_rows():
for cell in row:
# Cell contents: cell.value
fill = cell.fill
color = None
if fill and fill.start_color:
if fill.start_color.type == "rgb":
color = fill.start_color.rgb # like #rrggbb
elif fill.start_color.type == "theme":
color = f"theme:{fill.start_color.theme}"
elif fill.start_color.type == "indexed":
color = f"indexed:{fill.start_color.indexed}"
print(f"Cell={cell.coordinate}, bgcolor={color}")
colnum += 1
if colnum > 10: # Stop after 2 columns
colnum = 1
break
print("-----")
rownum += 1
if rownum > 2: # Stop after 2 rows
break
The output I’m getting.
Print highlight colors in Excel cells v2025-10-31.01
Cell=A1, bgcolor=00000000
Cell=B1, bgcolor=theme:4
Cell=C1, bgcolor=theme:4
Cell=D1, bgcolor=theme:9
Cell=E1, bgcolor=theme:9
Cell=F1, bgcolor=theme:9
Cell=G1, bgcolor=00000000
Cell=H1, bgcolor=00000000
Cell=I1, bgcolor=00000000
Cell=J1, bgcolor=00000000
-----
Cell=A2, bgcolor=00000000
Cell=B2, bgcolor=theme:4
Cell=C2, bgcolor=theme:4
Cell=D2, bgcolor=theme:9
Cell=E2, bgcolor=theme:9
Cell=F2, bgcolor=theme:9
Cell=G2, bgcolor=00000000
Cell=H2, bgcolor=00000000
Cell=I2, bgcolor=00000000
Cell=J2, bgcolor=00000000
Rows 1 and 2 have the same colors in each column. White must be “00000000”. One source says the first 2 characters “00” is the alpha channel (opacity).
Thank you.