Htmlmin is a configurable Python library designed for minifying HTML, offering safety features and various options to control the minification process. It removes extra whitespace, comments, and other unneeded characters to reduce page size and improve load times. The current version is 0.1.12, released in December 2017. The original project appears to be in maintenance mode or largely inactive, leading to the creation of several forks (e.g., `htmlmin2`, `htmlmin4`) to address compatibility with newer Python versions and continued development.
pip install htmlminVerified import paths — ran on the pinned version, not inferred.
This example demonstrates how to use the `htmlmin.minify` function to minify a basic HTML string, removing comments and empty spaces. The `pre` tag content is preserved by default.
Consider using a maintained fork like `htmlmin2` or `htmlmin4` which address this compatibility issue, or port your code to use an alternative, actively maintained HTML minification library.
Use `remove_empty_space=True` (which is safer as it primarily targets newlines) or carefully test with `remove_all_empty_space=True` and only use if you are confident in the result. Ensure essential spaces are preserved within content or explicitly protected.
For new projects or if encountering issues with modern Python, evaluate actively maintained forks such as `htmlmin2` (latest 0.1.13, March 2023) or `htmlmin4` (latest 0.2.15, March 2025) or other HTML minification alternatives.
Wrap the content you wish to protect in an element with the `pre` attribute (e.g., `<div pre>...</div>`). If you need to keep the `pre` attribute in the output, use the `keep_pre=True` option in the `minify` function or `Minifier` class. You can also specify custom attributes via `pre_attr` and additional pre-defined tags via `pre_tags`.
No dependency data recorded yet.