Skip to main content

Turns CSS blocks into style attributes

Project description

Turns CSS blocks into style attributes

When you send HTML emails you can’t used style tags but instead you have to put inline style attributes on every element. So from this:

<style type="text/css">
h1 { border:1px solid black }
p { color:red;}
p::first-letter { float:left; }
<h1 style="font-weight:bolder">Peter</h1>

You want this:

<h1 style="font-weight:bolder; border:1px solid black">Peter</h1>
<p style="{color:red} ::first-letter{float:left}">Hej</p>

premailer does this. It parses an HTML page, looks up style blocks and parses the CSS. It then uses the lxml.html parser to modify the DOM tree of the page accordingly.

Getting started

If you havena’t already done so, install premailer first:

$ pip install premailer

Next, the most basic use is to use the shortcut function, like this:

>>> from premailer import transform
>>> print transform("""
...         <html>
...         <style type="text/css">
...         h1 { border:1px solid black }
...         p { color:red;}
...         p::first-letter { float:left; }
...         </style>
...         <h1 style="font-weight:bolder">Peter</h1>
...         <p>Hej</p>
...         </html>
... """)
<h1 style="{font-weight:bolder; border:1px solid black}::first-lette\
        <p style="{color:red} ::first-letter{float:left}">Hej</p>

For more advanced options, check out the code of the Premailer class and all its options in its constructor.

Turning relative URLs into absolute URLs

Another thing premailer can do for you is to turn relative URLs (e.g. “/some/page.html” into “”). It does this to all href and src attributes that don’t have a :// part in it. For example, turning this:

<a href="/">Home</a>
<a href="page.html">Page</a>
<a href="">External</a>
<img src="/folder/">Folder</a>

Into this:

<a href="">Home</a>
<a href="">Page</a>
<a href="">External</a>
<img src="">Folder</a>

HTML attributes created additionally

Certain HTML attributes are also created on the HTML if the CSS contains any ones that are easily translated into HTML attributes. For example, if you have this CSS: td { background-color:#eee; } then this is transformed into style=”background-color:#eee” AND as an HTML attribute bgcolor=”#eee”.

Having these extra attributes basically as a “back up” for really shit email clients that can’t even take the style attributes. A lot of professional HTML newsletters such as Amazon’s use this.

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Filename, size & hash SHA256 hash help File type Python version Upload date
premailer-1.13.tar.gz (11.1 kB) Copy SHA256 hash SHA256 Source None

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page