HTML Minifier

0.1.12 · maintenance · verified Sat Apr 11

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

Install

Imports

Quickstart

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.

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>

view raw JSON →