Premailer

3.10.0 · active · verified Thu Apr 09

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.

Warnings

Install

Imports

Quickstart

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)

view raw JSON →