premailer.py - Transform CSS into line style attributes with lxml.html

11 July 2009   9 comments   Python

Mind That Age!

This blog post is 9 years old! Most likely, its content is outdated. Especially if it's technical.

By blogging about it I can pretty much guarantee that someone will comment and say "Hey, why didn't you use the pypi/alreadyexists package which does the same thing but better". I couldn't find one after a quick search and I felt the hacker mood creeping up on my begging me to (re)invent it.

premailer.py takes a HTML page, finds all CSS blocks and transforms these into style attributes. For example, from this:

<html>
  <head>
    <title>Test</title>
    <style>
    h1, h2 { color:red; }
    strong {
      text-decoration:none
    }
    </style>
  </head>
  <body>
    <h1>Hi!</h1>
    <p><strong>Yes!</strong></p>
  </body>
</html>

You get this:

<html>
  <head>
    <title>Test</title>
  </head>
  <body>
    <h1 style="color:red">Hi!</h1>
    <p><strong style="text-decoration:none">Yes!</strong></p>
  </body>
</html>

Why is this useful? When you're writing HTML emails. Like this newsletter app that I'm working on.

I just wrote it late yesterday and it needs lots of work to impress but for the moment it works for me. If I take the time to tidy it up properly I'll turn it into a package. Assuming there isn't one already :)

UPDATE

No available on github.com and as a PyPi package

UPDATE #2

Two new copy-cats have been released:

Might be worth poking around at these if my premailer isn't good enough.

Comments

Walter Dörwald
XIST includes a function for doing this. An online version of the CSS inliner is available from http://lltools.appspot.com/CSSInliner.html
Marius Gedminas
I've been looking for something like this to use as a PyBlosxom plugin to inline styles in the RSS feed. And when I say "looking", I mean "posting a question and waiting for lazyweb to come up with an answer". So, thanks ;-)

What is the licence of your code?
Peter Bengtsson
No licence yet but I'm guessing something very forgiving. I've made a much improved version but haven't uploaded it yet.
I guess I'll stick it on pypi
Lionel Barret
funny I have done the same a few weeks ago. I'll compare with your module if there is anything I can add.
astro
This seems to be exactly what am I looking for. I installed it, but how can I use it? Can you post some instructions or tell us where to find them?
Anonymous
Very nice, thank you.
Alex Hayes
I've created a django template tag that can be used with premailer - see https://github.com/alexhayes/django-premailer
Peter Bengtsson
That is great!

Your email will never ever be published


Related posts

Previous:
My first iPhone web app - Crosstips iPhone interface 01 July 2009
Next:
setuptools usability - not good, what can be done? 15 July 2009
Related by Keyword:
csso and django-pipeline 28 February 2018
minimalcss 0.6.2 now strips all unused font faces 22 January 2018
CSS selector simplifier regular expression in JavaScript 20 December 2017
Ultrafast loading of CSS 01 September 2017
Premailer 3.0.0 - classes kept by default 07 June 2016
Related by Text:
Be very careful with your add_header in Nginx! You might make your site insecure 11 February 2018
jQuery and Highslide JS 08 January 2008
I'm back! Peterbe.com has been renewed 05 June 2005
Anti-McCain propaganda videos 12 August 2008
I'm Prolog 01 May 2007