14
votes

I know this exists in other languages, but I want it for Python to I can send emails that'll show up in GMail, etc.

4
I don't know of any such module. I think it requires some programming effort. CSS matching problems that come to mind are: (1) class="class1 class2" (2) multiple qualifiers (like div.class, id.class, class.class, etc.). If your CSS has only one-segment matching rules, your translation will be much easier. - hughdbrown

4 Answers

13
votes

I had to do the same thing a while back and put the module I made for it up on GitHub https://github.com/rennat/pynliner

10
votes

There are now many choices for this. Here are two more in what seems to be decreasing order of activity:

0
votes

toronado is Fast lxml-based CSS stylesheet inliner.

Installation : pip install toronado

Example :

import toronado
document = """<html>
<head>
   <style type="text/css">
      h1 { color: red; }
   </style>
</head>
<body><h1>Hello, world.</h1></body>
</html>"""
print(toronado.from_string(document))