#! /bin/python import os,re # Delete the spritesheet.svg file if it exists if os.path.exists("spritesheet.svg"): os.remove("spritesheet.svg") # Open the output file and write the opening tag with open("spritesheet.svg", "w") as f: f.write('\n') # List all the SVG files in the current directory for filename in os.listdir(): if filename.endswith(".svg") and filename != 'spritesheet.svg': # Read the SVG file with open(filename, "r") as f: svg = f.read() # Extract the viewbox attribute from the SVG file viewbox = re.search(r'viewBox="(.*?)"', svg).group(1) # Extract the content within the tag start = svg.find('', start) + len('') svg = svg[start:end] # Remove stroke and fill attributes from the children svg = re.sub(r'height="(.*?)"', '', svg,1) svg = re.sub(r'width="(.*?)"', '', svg,1) svg = svg.replace("fill:#000000;","") svg = svg.replace("stroke:#000000;","") svg = svg.replace("fill:none","fill:none !important;") svg = re.sub(r'showgrid="(.*?)"','',svg) svg = re.sub(r'id="(.*?)"','',svg) svg = re.sub(r'inkscape:.*?=".*?"', '', svg) svg = re.sub(r'','',svg,flags=re.S) svg = re.sub(r'','',svg,flags=re.S) svg = re.sub(r'sodipodi:.*?=".*?"', '', svg) svg = re.sub(r'xmlns.*?=".*?"', '', svg) svg = re.sub(r'version="(.*?)"', '', svg) # Replace the closing tag with the closing tag svg = svg.replace('', '', 1) # Insert the id and viewbox attributes in the opening tag id = os.path.splitext(filename)[0] svg = f' tag to the output file with open("spritesheet.svg", "a") as f: f.write('\n')