Skip to content

Instantly share code, notes, and snippets.

@brandonhimpfen
Created April 24, 2026 03:24
Show Gist options
  • Select an option

  • Save brandonhimpfen/5fa9de2cfe3c92945b5c3780dcccc613 to your computer and use it in GitHub Desktop.

Select an option

Save brandonhimpfen/5fa9de2cfe3c92945b5c3780dcccc613 to your computer and use it in GitHub Desktop.
Minimize your HTML file with a command line interface (CLI) using Python.
import argparse
from bs4 import BeautifulSoup
# parse command line arguments
parser = argparse.ArgumentParser(description='Minimize HTML code')
parser.add_argument('input_file', type=str, help='path to the input HTML file')
parser.add_argument('output_file', type=str, help='path to the output HTML file')
args = parser.parse_args()
# read the HTML file
with open(args.input_file, 'r') as f:
html = f.read()
# parse the HTML using BeautifulSoup
soup = BeautifulSoup(html, 'html.parser')
# remove whitespace
for element in soup.recursiveChildGenerator():
if hasattr(element, 'strip') and callable(element.strip):
element.strip()
# minimize the HTML
minimized_html = str(soup).replace('\n', '').replace('\t', '')
# write the minimized HTML to a file
with open(args.output_file, 'w') as f:
f.write(minimized_html)

Run the script using the following command:

python minimize_html.py src.html dist.html

Where src.html is the input file and dist.html is the output file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment