premailer
Turns CSS blocks into style attributes
What it is and what it does
Premailer is a Python library that transforms HTML documents by inlining CSS styles into HTML element attributes. It parses HTML, extracts CSS from style blocks and external stylesheets, and rewrites the DOM to apply those styles as inline style attributes—a requirement for most email clients, which strip out style tags for security reasons.
The package handles relative URL rewriting (converting relative links to absolute ones using a base URL), CSS selector parsing via cssutils and cssselect, and HTML parsing via lxml. It offers both a simple transform() function for basic use and a full Premailer class for advanced configuration. It can preserve or strip certain CSS features (pseudoclasses, !important declarations, class attributes) and supports command-line usage for batch processing.
Use it for:
- Prepare HTML newsletters and transactional emails for delivery by inlining all styles before sending.
- Convert relative URLs in email templates to absolute URLs using a base_url parameter.
- Strip !important declarations and pseudoclasses from CSS to ensure compatibility with email clients.
- Process HTML templates from a file or stdin via the command-line interface for CI/CD pipelines.
- Preserve specific CSS or links (e.g., handlebar syntax, cid: attachments) while inlining other styles.
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
Converts CSS style blocks into inline style attributes on HTML elements, making HTML suitable for email clients that don't support style tags.
Yes, if you are building email systems. Premailer solves a real, specific problem—email clients require inline styles—and does it reliably. The dormant maintenance is a minor concern given the stable classifier and zero known vulnerabilities, but verify that its CSS and HTML parsing behavior still meets your email client compatibility needs before adopting it for new projects.
Install
premailer on PyPI
pip
pip install premaileruv
uv add premailerpoetry
poetry add premailerInstalling premailer
Before you install
Low friction: pure Python wheel with five straightforward runtime dependencies (lxml, cssselect, cssutils, requests, cachetools). Maintenance is dormant—last release was 2021-08-02 and last commit 2023-12-16—but the package is marked Production/Stable and has no known vulnerabilities.
License in practice
Licensed under the Python Software Foundation License, a permissive license. No restrictions on use, modification, or distribution in commercial or private projects.
Quickstart
pip install premailer
from premailer import transform
html = '<style>p { color: red; }</style><p>Hello</p>'
result = transform(html)
print(result)
By default, premailer attempts to download external stylesheets over the network; use allow_network=False to prevent this if needed.
Verify before relying
- Whether the dormant maintenance status (last release 2021-08-02) affects compatibility with modern email clients or CSS specifications.
- Performance characteristics when processing large HTML documents or many external stylesheets.
Package facts
| License | Python (permissive) |
| Python support | not specified |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | 5 — lxml, cssselect, cssutils, requests, cachetools |
| Maintenance | dormant — 1,838 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 5,013,987/month — #2,181 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: premailer-3.10.0-py2.py3-none-any.whl
Keywords: html, lxml, email, mail, style
Tags
More Python Modules packages
Converts domain names between Unicode and…
permissive · top 100 on PyPI
setuptoolsSetuptools is a Python build backend and…
permissive · top 100 on PyPI
PyYAMLPyYAML parses and emits YAML 1.1 data format,…
permissive · top 100 on PyPI
pydanticPydantic validates Python data structures…
permissive · top 100 on PyPI
annotated-typesProvides reusable metadata objects for use with…
permissive · top 100 on PyPI
typing-inspectionProvides runtime tools to inspect and…
permissive · top 100 on PyPI
django-premailerA Django template tag that converts CSS blocks…
permissive · top 15,000 on PyPI
css-inlineInlines CSS from style and link tags directly…
permissive · top 5,000 on PyPI
pynlinerConverts CSS stylesheets to inline styles in…
permissive · top 15,000 on PyPI
email-toWraps Python's standard email modules to…
permissive · top 15,000 on PyPI
cssutilscssutils parses and builds CSS stylesheets as a…
copyleft · top 5,000 on PyPI
emailsBuild and send HTML and plain-text emails with…
permissive · top 15,000 on PyPI
prettierfierReformats HTML and XML strings with intelligent…
permissive · top 15,000 on PyPI
pretty-html-tableConverts pandas DataFrames into styled HTML…
permissive · top 5,000 on PyPI
webcolorsConverts and normalizes HTML and CSS color…
permissive · top 1,000 on PyPI
djangocms-admin-styleProvides styled CSS templates and admin…
permissive · top 15,000 on PyPI