{"id":1860,"library":"premailer","title":"Premailer","description":"Premailer is a Python library that converts HTML documents containing CSS `<style>` blocks or `<link>` tags into HTML with inline `style` attributes. It leverages `lxml` for parsing and is primarily used for preparing HTML emails, where external stylesheets are often unsupported. The library's latest stable version is 3.10.0 and it maintains an active development status.","status":"active","version":"3.10.0","language":"en","source_language":"en","source_url":"https://github.com/peterbe/premailer","tags":["html","css","email","inline-styles","lxml","email-marketing"],"install":[{"cmd":"pip install premailer","lang":"bash","label":"Install with pip"}],"dependencies":[{"reason":"Core HTML/XML parsing and manipulation.","package":"lxml","optional":false},{"reason":"CSS parsing and handling.","package":"cssutils","optional":false},{"reason":"CSS selector support for lxml.","package":"cssselect","optional":false},{"reason":"For fetching external stylesheets if 'allow_network' is True.","package":"requests","optional":false},{"reason":"Caching mechanism for performance optimizations.","package":"cachetools","optional":false}],"imports":[{"note":"A shortcut function for quick, single-document transformations. Not recommended for batch processing due to performance implications.","symbol":"transform","correct":"from premailer import transform"},{"note":"The main class for advanced usage and improved performance when processing multiple HTML documents.","symbol":"Premailer","correct":"from premailer import Premailer"}],"quickstart":{"code":"from premailer import transform\n\nhtml_content = \"\"\"\n<html>\n<head>\n<style type=\"text/css\">\nh1 { border:1px solid black }\np { color:red;}\n</style>\n</head>\n<body>\n<h1 style=\"font-weight:bolder\">Peter</h1>\n<p>Hej</p>\n</body>\n</html>\n\"\"\"\n\n# Transform the HTML to inline styles\nresult_html = transform(html_content)\nprint(result_html)","lang":"python","description":"This quickstart demonstrates the most basic usage of `premailer` using the `transform` shortcut function. It takes an HTML string with embedded CSS and converts the styles into inline attributes. For more complex scenarios or better performance when processing multiple documents, it is recommended to use the `Premailer` class directly."},"warnings":[{"fix":"If you relied on classes being removed, explicitly set `remove_classes=True` in the `Premailer` constructor or `transform` function: `transform(html, remove_classes=True)`.","message":"In version 3.0.0, the default value for the `remove_classes` option changed from `True` to `False`. This means CSS class attributes are now kept in the output HTML by default.","severity":"breaking","affected_versions":">=3.0.0"},{"fix":"For batch processing, instantiate the `Premailer` class once and reuse its `transform` method: `p = Premailer(base_url=MY_BASE_URL); for html_string in get_html_documents(): transformed = p.transform(html_string)`.","message":"Using `premailer.transform` repeatedly in a loop for multiple HTML documents can lead to poor performance, as it creates a new `Premailer` instance with each call.","severity":"gotcha","affected_versions":"All"},{"fix":"To prevent network requests, set the `allow_network=False` option: `transform(html, allow_network=False)` or `Premailer(html, allow_network=False)`.","message":"By default, `premailer` attempts to download external stylesheets specified by URLs over the network. This can introduce security risks or performance issues.","severity":"gotcha","affected_versions":"All"},{"fix":"To capture CSS parsing logs, pass a `cssutils_logging_handler` and `cssutils_logging_level` to the `Premailer` constructor. For example: `import logging; from io import StringIO; mylog = StringIO(); myhandler = logging.StreamHandler(mylog); p = Premailer(..., cssutils_logging_handler=myhandler, cssutils_logging_level=logging.INFO)`.","message":"The underlying `cssutils` library, used by `premailer` for CSS parsing, uses Python's standard `logging` module to report issues. By default, these logs might not be captured or visible.","severity":"gotcha","affected_versions":"All"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}