Pre-release
This release is a pre-release and may not be stable for production use.
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 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 currently expects rules with exactly one formula.
- Formula evaluation expects references that resolve to single cells.
- Non-boolean formula results are ignored.
- Style extraction supports:
- Borders: top/right/bottom/left
- Alignment: horizontal/vertical
- Fill: solid fills (or
DifferentialStyle.fill.bgColor) - Font: size, color, bold, italic, underline
- The package provides building blocks; no CLI is included.
Development
Run tests:
pytest
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.
Metadata
Release files for condif2css 0.13.0b6021601
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| condif2css-0.13.0b6021601.tar.gz | 25.6 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| condif2css-0.13.0b6021601-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 46.1 kB
Release files / condif2css-0.13.0b6021601.tar.gz
| Download URL | condif2css-0.13.0b6021601.tar.gz |
|---|---|
| Size | 25.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
bbcf908325a4002485b1c824d2f8099a06fed9092ddbbd5469e912718f5a7ea2
|
|
BLAKE2b-256 checksum How to use checksums |
6ba39774791af4549bc70d7af36980bfe220e88e82627e668bd34569ceaab926
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
pdm/2.26.6 CPython/3.14.3 Darwin/24.6.0
|
Release files / condif2css-0.13.0b6021601-py3-none-any.whl
| Download URL | condif2css-0.13.0b6021601-py3-none-any.whl |
|---|---|
| Size | 20.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
ae654843b8e509033bec7a514d145ade8162588858cdaeff2a0b429873da55dc
|
|
BLAKE2b-256 checksum How to use checksums |
7c26b945eba54c120a2c6b4b5528d3a8e6b3024ce0399c117a150a8b4fe60eb7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
pdm/2.26.6 CPython/3.14.3 Darwin/24.6.0
|