cleans a svg file from inkscape tags and attributes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import tkinter as tk | |
from tkinter import filedialog | |
from tkinter import messagebox | |
import re | |
def clean_svg_file(content): | |
replacement = content | |
for nsName in tags: | |
print(nsName) | |
reg_xmlns = 'xmlns:' + nsName + '=".*?"\\s*' | |
reg_tag = '<' + nsName + ':([^>]|[\\s])*?((\\/>)|(>[\\s\\S]*?<\\/' + nsName + ':.*?>))\\s*' | |
reg_attr = nsName + ':.*?=".*?"\\s*' | |
replacement = re.sub(reg_xmlns, '', replacement) | |
replacement = re.sub(reg_tag, '', replacement) | |
replacement = re.sub(reg_attr, '', replacement) | |
return replacement | |
def file_save(text): | |
f = filedialog.asksaveasfile(mode='w', defaultextension=".svg") | |
if f is None: # asksaveasfile return `None` if dialog closed with "cancel". | |
return | |
f.write(text) | |
f.close() | |
messagebox.showinfo("Information", "File has been cleaned.") | |
def file_open(): | |
file_path = filedialog.askopenfilename(defaultextension=".svg") | |
with open(file_path) as f: | |
read_data = f.read() | |
f.close() | |
return read_data | |
try: | |
root = tk.Tk() | |
root.withdraw() | |
tags = ['inkscape', 'sodipodi', 'rdf', 'cc', 'dc', 'metadata'] | |
file_data = file_open() | |
question = messagebox.askokcancel("Clean file", "Do you want to clean the file from " | |
"'inkscape', 'sodipodi', 'rdf', 'cc', 'dc', 'metadata") | |
if question: | |
cleaned = clean_svg_file(file_data) | |
file_save(cleaned) | |
sys.exit(0) | |
except: | |
print(sys.exc_info()[0]) | |
messagebox.showerror("Error", "Could not clean file\n" + str(sys.exc_info()[0])) | |
raise | |
sys.exit(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment