Pynliner is a Python library that converts CSS styles in an HTML document to inline styles, primarily designed for crafting HTML emails to ensure consistent rendering across various email clients. It leverages BeautifulSoup for HTML parsing and cssutils for CSS processing. The current stable version is 0.8.0, released in 2019, indicating an infrequent release cadence and a current state of maintenance rather than active development.
pip install pynlinerVerified import paths — ran on the pinned version, not inferred.
Initialize Pynliner and apply CSS styles from an HTML string into inline 'style' attributes. This is the primary method for inlining CSS for email clients.
Ensure all CSS intended for inlining is embedded directly within <style> tags in the HTML document's <head> section.
Consolidate all CSS into a single <style> block, avoiding @import rules for external stylesheets.
Test thoroughly with target email clients. For cutting-edge CSS, consider alternative, more actively maintained tools or simplify designs to what's well-supported by email clients.
Carefully manage CSS specificity and the use of `!important`. Test the output to ensure styles are applied as intended, especially when combining authored CSS with existing inline styles.
Review and validate your CSS syntax. Use a CSS linter or validator to identify and correct any malformed rules or properties before passing the HTML to Pynliner.
Ensure your HTML is well-formed and valid. Check for unclosed tags, incorrect nesting, or other structural issues that might confuse the HTML parser.
Verify that all CSS is embedded in <style> tags. Avoid @import. Simplify complex CSS selectors if possible. Remember Pynliner processes styles in order, so later rules might override earlier ones based on specificity and document order.