Minimize CSS using Python
I'm going to share a simply Python script that will minimize your CSS file using the cssutils library.
I'm going to share a simply Python script that will minimize your CSS file using the cssutils
library.
import cssutils
# Load the CSS file
with open('style.css', 'r') as f:
css_text = f.read()
# Parse the CSS
sheet = cssutils.parseString(css_text)
# Remove any unnecessary whitespace and comments
sheet.cssText = cssutils.ser.parsetree(sheet.cssText, keepComments=False).cssText
# Write the minimized CSS to a file
with open('style.min.css', 'w') as f:
f.write(sheet.cssText)
You should ensure to change the file name of the source CSS file on line four and the distribution CSS file on line 14.
You can run this script by saving it to a file as minimize_css.py
and then running python minimize_css.py
in your terminal.