HTML Minifier
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.
Warnings
- breaking The `htmlmin` library implicitly relies on Python's built-in `cgi` module, which was deprecated in Python 3.11 and removed in Python 3.13. Attempting to use `htmlmin` on Python 3.13 or newer will result in a `ModuleNotFoundError`.
- gotcha Using `remove_all_empty_space=True` can lead to unexpected visual or functional breakage, especially with inline HTML elements. This option removes all whitespace between tags, potentially collapsing space where it is semantically important (e.g., between `<i>X</i> <i>Y</i>` becoming `<i>X</i><i>Y</i>`).
- gotcha The original `htmlmin` library has not seen updates since late 2017, indicating it is no longer actively maintained. This means it may not receive bug fixes, security patches, or compatibility updates for newer Python versions or HTML standards.
- gotcha To prevent `htmlmin` from minifying specific blocks of HTML, you can add a `pre` attribute to the HTML tag wrapping the content. By default, `htmlmin` will remove this `pre` attribute from the output.
Install
-
pip install htmlmin
Imports
- minify
import htmlmin htmlmin.minify(html_string)
- Minifier
from htmlmin import Minifier minifier = Minifier() minified_html = minifier.minify(html_string)
Quickstart
import htmlmin
input_html = '''
<html>
<head>
<title> My Page </title>
</head>
<body>
<!-- This is a comment -->
<h1> Welcome! </h1>
<p> This is some text. </p>
<pre> Preformatted text </pre>
</body>
</html>
'''
minified_html = htmlmin.minify(
input_html,
remove_comments=True,
remove_empty_space=True
)
print(minified_html)
# Expected output: <html><head><title>My Page</title></head><body><h1>Welcome!</h1><p>This is some text.</p><pre> Preformatted text </pre></body></html>