Python 3 Script htmlmin Library Example to Compress or Minify HTML Markup Files Full Project For Beginners

Python 3 Script htmlmin Library Example to Compress or Minify HTML Markup Files Full Project For Beginners

 

Welcome folks today in this blog post we will be compressing or minifying html markup files using htmlmin library in python 3. All the full source code of the application is given below.

 

 

 

Get Started

 

 

 

In order to get started we need to install the following library using the pip command as shown below

 

pip install htmlmin

 

After installing this library you need to make an app.py file and copy paste the following code

 

app.py

 

import codecs
import htmlmin

def html_minify(src,dst):
    with codecs.open(src, 'r', 'utf-8') as src_file:
        minified = htmlmin.minify(src_file.read(), remove_comments=True, remove_empty_space=True)
        with codecs.open(dst, 'w', 'utf-8') as dst_file:
            dst_file.write(minified)
            
html_minify("index.html","minified.html")

 

 

Here in this python script you need to replace the input file and output file paths accordingly. Now run the python script by typing the below command

See also  Python 3 Script to Download Files & Images From Google Drive API By Shareable Link ID Using Requests Module Full Project For Beginners

 

python app.py

 

 

 

Leave a Reply