To streamline the process of preparing SVG icons so their colors can be dynamically changed using CSS, specifically converting certain elements to use currentColor.
Step 1: Create and Export SVG Icons in Adobe Illustrator
Step 2: Prepare the Python Script
Save the file as “replace_svg_color.py”.
import os
directory = '.' # Current directory
for filename in os.listdir(directory):
if filename.endswith(".svg"):
filepath = os.path.join(directory, filename)
with open(filepath, 'r', encoding='utf-8') as file:
content = file.read()
# Replace specific fill colors with currentColor
content = content.replace('fill="#231f20"', 'fill="currentColor"')
content = content.replace('fill="#fff"', 'fill="currentColor"')
with open(filepath, 'w', encoding='utf-8') as file:
file.write(content)
print("All SVG files have been updated.")
Step 3: Execute the Python Script
cd "/Users/MrBobHunter-MacPro/Temp/Elementor Number Icons"
Run the script
python3 replace_svg_color.py
To import the modified SVG files, the following must be added to the Child Theme’s functions.php file:
function add_svg_to_upload_mimes( $file_types ) {
$new_filetypes = array();
$new_filetypes['svg'] = 'image/svg+xml';
$file_types = array_merge( $file_types, $new_filetypes );
return $file_types;
}
add_filter( 'upload_mimes', 'add_svg_to_upload_mimes' );