Evaluate Excel conditional formatting rules and generate reusable CSS classes from openpyxl styles.
Project description
condif2css
condif2css helps translate Excel conditional formatting into reusable CSS classes.
The library is designed to work with openpyxl workbooks and is currently focused on:
- Evaluating conditional-format formulas per cell (
processor.process) - Resolving workbook/theme/indexed colors to aRGB (
create_themed_css_color_resolver) - Building deduplicated CSS rules from
Cell/DifferentialStyleobjects (css.py)
What It Does
- Reads conditional-format rules from an
openpyxlworksheet. - Evaluates each rule formula against each target cell (with relative/absolute ref offsets).
- Returns matching
(sheet, cell, priority, dxf_id, stop_if_true)metadata. - Converts style objects into CSS declarations and stable class names.
Installation
Install from PyPI:
pip install condif2css
If the latest published version is a pre-release, use:
pip install --pre condif2css
Local development install:
pip install -e .
Or with pdm (including dev dependencies):
pdm sync --group dev
Or with uv (including dev dependencies):
uv sync --group dev
Quick Example
from openpyxl import load_workbook
from condif2css import (
CssBuilder,
CssRulesRegistry,
argb_to_css,
create_get_css_from_cell,
create_themed_css_color_resolver,
get_differential_style,
get_theme_colors,
process_conditional_formatting,
)
wb = load_workbook("input.xlsx", data_only=True)
ws = wb["Sheet1"]
theme_colors = get_theme_colors(wb)
get_argb = create_themed_css_color_resolver(theme_colors)
def get_css_color(color):
argb = get_argb(color)
return argb_to_css(argb) if isinstance(argb, str) else None
css_builder = CssBuilder(get_css_color)
css_registry = CssRulesRegistry(prefix="cf")
get_css_from_cell = create_get_css_from_cell(css_registry, css_builder)
# { "Sheet1\\!A1": ("Sheet1", "A1", priority, dxf_id, stop_if_true), ... }
matched_rules = process_conditional_formatting(ws)
# Apply each matched differential style and collect class names per cell
cell_classes = {}
for code, (_, _, _, dxf_id, _) in matched_rules.items():
dxf = get_differential_style(wb, dxf_id)
if dxf is None:
continue
cell_classes[code] = sorted(get_css_from_cell(dxf, is_important=True))
# Final CSS text
css_text = "\n".join(css_registry.get_rules())
Public API (Current)
condif2css.create_themed_css_color_resolver(theme_colors)condif2css.get_theme_colors(workbook, strict=False)condif2css.process_conditional_formatting(sheet, fail_ok=True)condif2css.get_differential_style(workbook, dxf_id)condif2css.ThemeColorsErrorcondif2css.CssBuildercondif2css.CssRulesRegistrycondif2css.create_get_css_from_cell(...)condif2css.color.argb_to_css(...)and color/tint utility helpers
Current Scope and Limitations
- Conditional-format evaluation supports:
expressionrules with exactly one formulacellIsrules (equal,notEqual,greaterThan,greaterThanOrEqual,lessThan,lessThanOrEqual,between,notBetween)containsText,notContainsText,beginsWith, andendsWith
- Formula evaluation expects references that resolve to single cells.
- Non-boolean formula results are ignored.
- Other conditional formatting rule types are currently skipped with a warning.
- Style extraction supports:
- Borders: top/right/bottom/left
- Alignment: horizontal/vertical
- Fill: solid fills and approximated pattern fills
- Font: size, color, bold, italic, underline
- The package provides building blocks; no CLI is included.
Development
Run tests:
pdm run pytest
Build and check distributions:
pdm build
python -m pip install --upgrade twine
python -m twine check dist/*
Releases
- Versioning is SCM-driven from Git tags.
- Stable releases must use SemVer tags in the format
vMAJOR.MINOR.PATCH(example:v1.4.0). - GitHub Actions is configured to publish on matching
v*tags and validates the strict stable SemVer format before publishing.
Acknowledgements
- Theme color extraction is based on public-domain work by Mike-honey: https://gist.github.com/Mike-Honey/b36e651e9a7f1d2e1d60ce1c63b9b633
License
Licensed under either of:
- Apache License, Version 2.0 (
LICENSE_APACHEor https://www.apache.org/licenses/LICENSE-2.0) - MIT license (
LICENSE_MITor https://opensource.org/licenses/MIT)
at your option.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file condif2css-0.15.0rc1.tar.gz.
File metadata
- Download URL: condif2css-0.15.0rc1.tar.gz
- Upload date:
- Size: 26.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
354fa0c89feb2fc11c81f28b81323de243d5da8d24c6ffff2eb7c811c3018686
|
|
| MD5 |
1bfd481afe59af0d489a11dab5e32f9e
|
|
| BLAKE2b-256 |
cda94b435d675b0249427dc3673a9f324eadd18af8637837dbd111ea0535b251
|
Provenance
The following attestation bundles were made for condif2css-0.15.0rc1.tar.gz:
Publisher:
publish.yml on gocova/condif2css
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
condif2css-0.15.0rc1.tar.gz -
Subject digest:
354fa0c89feb2fc11c81f28b81323de243d5da8d24c6ffff2eb7c811c3018686 - Sigstore transparency entry: 966531907
- Sigstore integration time:
-
Permalink:
gocova/condif2css@fedc5057203d56ca711b87848e0a04b23fda6acc -
Branch / Tag:
refs/tags/v0.15.0rc1 - Owner: https://github.com/gocova
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@fedc5057203d56ca711b87848e0a04b23fda6acc -
Trigger Event:
push
-
Statement type:
File details
Details for the file condif2css-0.15.0rc1-py3-none-any.whl.
File metadata
- Download URL: condif2css-0.15.0rc1-py3-none-any.whl
- Upload date:
- Size: 22.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7fc07b6c89c5ffe464a6e07744ee85be108bb608fb61928d817278391d656079
|
|
| MD5 |
97fa3ab92dc2bef74a6b53a1e343c946
|
|
| BLAKE2b-256 |
d656a2bbdad89801f9a53d6ac61954c9a38342a6138906484e7e3ee6a2eea843
|
Provenance
The following attestation bundles were made for condif2css-0.15.0rc1-py3-none-any.whl:
Publisher:
publish.yml on gocova/condif2css
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
condif2css-0.15.0rc1-py3-none-any.whl -
Subject digest:
7fc07b6c89c5ffe464a6e07744ee85be108bb608fb61928d817278391d656079 - Sigstore transparency entry: 966531977
- Sigstore integration time:
-
Permalink:
gocova/condif2css@fedc5057203d56ca711b87848e0a04b23fda6acc -
Branch / Tag:
refs/tags/v0.15.0rc1 - Owner: https://github.com/gocova
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@fedc5057203d56ca711b87848e0a04b23fda6acc -
Trigger Event:
push
-
Statement type: