Why openpyxl 3.1.5 silently misses Excel theme color changes (and how to catch silent AI spreadsheet edits)

AI disclosure: I built and ran all Python experiments, regression tests, and
openpyxl 3.1.5checks locally on Windows 11 (Python3.14.0). I used an AI assistant to help draft and edit this post, and verified every code snippet and output against our local test suite.
If you write Python tests to check AI-edited .xlsx files before shipping a company report, you probably check cell values, SUM formulas, and cell fill colors.
I built a small openpyxl diff script to catch those silent edits on synthetic workbooks. It caught solid RGB fill changes (#D9E1F2 switched to #FF0000) right away.
Then I changed a cell's Excel theme color, tint, and indexed color in a test file. My diff script returned [PASS] with zero warnings.
Why fgColor.rgb or fgColor.theme silently fails
My first version looked harmless:
def fill_sig(cell): fill = cell.fill if not fill or not fill.fill_type: return "none" rgb = getattr(fill.fgColor, "rgb", None) or getattr(fill.fgColor, "theme", None) or "" return f"{fill.fill_type}:{rgb}" I assumed fill.fgColor.rgb would return None when a cell used an Excel theme color, so Python's or would fall back to fill.fgColor.theme.
Running a one-liner in openpyxl 3.1.5 showed why that assumption broke:
from openpyxl.styles import Color c = Color(theme=4) print(type(c.rgb), repr(c.rgb), bool(c.rgb)) # <class 'openpyxl.styles.colors.RGB'> Values must be of type <class 'str'> True Reading .rgb on a theme or indexed Color object does not raise an exception and does not return None. It returns openpyxl's RGB descriptor object, whose repr() prints a type hint (Values must be of type <class 'str'>) and whose boolean value is True.
Because bool(c.rgb) is True, the or chain short-circuited before ever checking fill.fgColor.theme. Two completely different theme shades (theme=4, tint=0.2 and theme=4, tint=0.6) produced the exact same signature, so the diff missed the change.
The fix: branch explicitly on color.type
Checking color.type first, rounding tint for theme colors, and applying the same helper to font.color closed the gap:
def color_sig(color): if color is None: return "none" ctype = getattr(color, "type", None) if ctype == "rgb": return f"rgb:{color.rgb}" if ctype == "theme": tint = round(float(getattr(color, "tint", 0.0) or 0.0), 4) return f"theme:{color.theme}:{tint}" if ctype == "indexed": return f"indexed:{color.indexed}" return f"{ctype or 'none'}" def fill_sig(cell): fill = cell.fill if not fill or not fill.fill_type: return "none" return f"{fill.fill_type}:{color_sig(fill.fgColor)}" def font_sig(cell): font = cell.font if not font: return (False, "none") return (bool(font.bold), color_sig(font.color)) In our regression tests on a synthetic two-sheet workbook (47 populated cells, 17 formulas), all three cases now fail immediately as STYLE_CHANGED with exact cell coordinates:
-
D9fill changed fromColor(theme=4, tint=0.2)toColor(theme=4, tint=0.6) -
B9fill changed fromColor(indexed=10)toColor(indexed=12) -
C9font color changed toColor(theme=5, tint=0.0)
A second openpyxl trap: empty cached formula values
Even on a completely clean edit, reopening the saved .xlsx file with load_workbook(..., data_only=True) returned None for all 17 formula cells.
Because openpyxl writes XML without running Excel's calculation engine, cached formula results stay empty until you open and re-save the workbook once in Excel or LibreOffice. If your workflow feeds Python-edited .xlsx files straight into pandas or a BI tool, formula columns can quietly show up as nulls.
Full local test harness
If you want to run the complete synthetic workbook generator and diff script locally (checking expected target values, overwritten SUM formulas, untouched neighbor cells, merged ranges, and RGB/theme/indexed styles), the full walkthrough is up on our free hub:
Scope note: the auditor checks cell values, formulas, merged ranges, number formats, font weight/color, and fill color settings stored in workbook XML. It does not check hidden rows, print areas, or sheet ordering.
Have you run into other openpyxl descriptor or cached-value gotchas when automating spreadsheet tests?
