wilsonzlin/minify-html : Extremely fast and smart HTML + JS + CSS minifier, available for Rust, Deno, Java, Node.js, Python, Ruby, and WASM

The Python library on pypi.

Using the Python library:

# code/example_minifyhtml.py
#!/usr/bin/env python
 
import minify_html
 
with open("output/index.html") as f:
    t = f.read()
    print(t)
    o = minify_html.minify(
        t,
        do_not_minify_doctype=True,
        keep_spaces_between_attributes=True,
        ensure_spec_compliant_unquoted_attribute_values=True,
    )
    print(o)