开发者

Does Python have a module to convert CSS styles to inline styles for emails?

开发者 https://www.devze.com 2023-01-20 19:47 出处:网络
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.I had to do the same thing a while back and put the module I made f

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.


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


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

  • Premailer
  • Inline Styler


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))


If anyone visiting this in 2022 then the other option would be : https://pypi.org/project/css-inline/

As per the site response time is below:

css_inline 0.8.2 - 22.42 us
premailer 3.10.0 - 332.02 us (x14.81)
toronado 0.1.0 - 1.59 ms (x71.17)
inlinestyler 0.2.5 - 2.35 ms (x105.07)
pynliner 0.8.0 - 2.79 ms (x124.80)


http://blog.doughellmann.com/2008/10/pymotw-smtplib.html great example from doug

0

精彩评论

暂无评论...
验证码 换一张
取 消