Crosstips.org

My fun Crossword solver project. Crosstips.org & Krysstips.se

Kung Fu

Fujian White Crane Kung Fu

Fry-IT

Fry-IT is the company I work for

Photos

Photoalbum, both old and new.

Zope

What I have and am doing with Zope

Receptsamlingen

In Swedish only. About my "Collection of Recipes" website.

Contact me

My contact details and how to contact me.

 

KungFuPeople.com
Do you train Kung Fu?
Or know someone who does?
Then check out KungFuPeople.com


Mobile version of this page Mobile version of this page


 

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


style text, premailer, css, style, lxml, test_premailer, emails, newsletter

11th of July 2009

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



Comment

Walter Dörwald - 11th July 2009  [«« Reply to this]
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 - 12th July 2009  [«« Reply to this]
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 - 13th July 2009   [«« Reply to this]
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 - 3rd August 2009  [«« Reply to this]
funny I have done the same a few weeks ago. I'll compare with your module if there is anything I can add.
 
Name:
Email:
hide my email address.

Your email address will be encoded to prevent email-extraction spiders from reading it so you won't get spammed if you decide to show your email address.