Registry / communication / premailer

premailer

JSON →
library3.10.0pypypi✓ verified 28d ago

Premailer is a Python library that converts HTML documents containing CSS `<style>` blocks or `<link>` tags into HTML with inline `style` attributes. It leverages `lxml` for parsing and is primarily used for preparing HTML emails, where external stylesheets are often unsupported. The library's latest stable version is 3.10.0 and it maintains an active development status.

pip install premailer
INSTALL
IMPORT
SIG · PREMAILER
P
premailer
communicationpythonv3.10.0
Install
3.3s avg
Import
599ms
Disk
36MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.9–3.13
musl
3.9–3.13
Install & Compatibility
Where this runs
tested against v3.10.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.10–3.95 runs
installs and imports cleanly · install 0.0s · import 0.618s · 36.9MB
glibc
py 3.10–3.95 runs
installs and imports cleanly · install 3.3s · import 0.580s · 40MB
36MB installed
● package 36MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

transform
✓ from premailer import transform
A shortcut function for quick, single-document transformations. Not recommended for batch processing due to performance implications.
Premailer
✓ from premailer import Premailer
The main class for advanced usage and improved performance when processing multiple HTML documents.

This quickstart demonstrates the most basic usage of `premailer` using the `transform` shortcut function. It takes an HTML string with embedded CSS and converts the styles into inline attributes. For more complex scenarios or better performance when processing multiple documents, it is recommended to use the `Premailer` class directly.

from premailer import transform html_content = """ <html> <head> <style type="text/css"> h1 { border:1px solid black } p { color:red;} </style> </head> <body> <h1 style="font-weight:bolder">Peter</h1> <p>Hej</p> </body> </html> """ # Transform the HTML to inline styles result_html = transform(html_content) print(result_html)
premailer --version
Debug
Known issues
breakingIn version 3.0.0, the default value for the `remove_classes` option changed from `True` to `False`. This means CSS class attributes are now kept in the output HTML by default.
fix
If you relied on classes being removed, explicitly set `remove_classes=True` in the `Premailer` constructor or `transform` function: `transform(html, remove_classes=True)`.
affects: >=3.0.0
gotchaUsing `premailer.transform` repeatedly in a loop for multiple HTML documents can lead to poor performance, as it creates a new `Premailer` instance with each call.
fix
For batch processing, instantiate the `Premailer` class once and reuse its `transform` method: `p = Premailer(base_url=MY_BASE_URL); for html_string in get_html_documents(): transformed = p.transform(html_string)`.
affects: All
gotchaBy default, `premailer` attempts to download external stylesheets specified by URLs over the network. This can introduce security risks or performance issues.
fix
To prevent network requests, set the `allow_network=False` option: `transform(html, allow_network=False)` or `Premailer(html, allow_network=False)`.
affects: All
gotchaThe underlying `cssutils` library, used by `premailer` for CSS parsing, uses Python's standard `logging` module to report issues. By default, these logs might not be captured or visible.
fix
To capture CSS parsing logs, pass a `cssutils_logging_handler` and `cssutils_logging_level` to the `Premailer` constructor. For example: `import logging; from io import StringIO; mylog = StringIO(); myhandler = logging.StreamHandler(mylog); p = Premailer(..., cssutils_logging_handler=myhandler, cssutils_logging_level=logging.INFO)`.
affects: All
Errors
Common errors & fixes
AttributeError: 'CSSMediaRule' object has no attribute 'style'
This usually happens when premailer encounters a CSS @media rule that it cannot process or that is malformed, leading to an attempt to access a 'style' attribute on a 'CSSMediaRule' object where it doesn't exist in that context.
fix
Ensure your CSS is well-formed and avoid complex or unsupported `@media` rules. Consider pre-processing CSS to simplify media queries or explicitly ignore them using `data-premailer='ignore'` if premailer is not intended to handle them.
TypeError: Can't pass html argument twice
This error occurs when the HTML content is provided both during the `Premailer` object instantiation and again as an argument to the `transform` method.
fix
Provide the HTML content either to the `Premailer` constructor (e.g., `p = Premailer(html_string, with_html_string=True)`) or as the first argument to the `transform` method (e.g., `p = Premailer(); p.transform(html_string)`), but not both.
ConnectionError: HTTPSConnectionPool(...) Max retries exceeded with url: ... Failed to establish a new connection
Premailer attempts to download external stylesheets referenced in the HTML (e.g., `<link rel='stylesheet' href='http://example.com/style.css'>`) but fails due to network issues, firewalls, or invalid URLs.
fix
Disable external stylesheet loading by setting `allow_network=False` in the `Premailer` constructor, or ensure network access is available and URLs are correct. If the stylesheets are local, use `base_url` or `base_path` to help premailer locate them without network requests.
lxml.etree.XMLSyntaxError: Document is empty
This error often indicates malformed HTML input that `lxml` cannot parse correctly, or an incompatibility between the `premailer` library and the installed `lxml` version, especially with `lxml > 4.9.4`.
fix
Ensure your input HTML is well-formed. If the issue persists, try pinning `lxml` to a compatible version (e.g., `pip install lxml==4.9.4`) or update `premailer` to a version that officially supports newer `lxml` releases if available.
Upgrade
Version history
3.10.0latest on PyPI · released Aug 2, 2021
Audit
Dependencies
lxmlrequiredCore HTML/XML parsing and manipulation.
cssutilsrequiredCSS parsing and handling.
cssselectrequiredCSS selector support for lxml.
requestsrequiredFor fetching external stylesheets if 'allow_network' is True.
cachetoolsrequiredCaching mechanism for performance optimizations.
Agent activity
16 hits · last 30 days
node
12
OpenAI (training)
1
Resources
premailer — pip install premailer · libregistry