-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmerge.py
53 lines (42 loc) · 1.74 KB
/
merge.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import re
from pathlib import Path
import argparse
def main():
"""
Simple script to convert multiple svgs into one.
Especially useful when generating Affinity Designer assets
from multiple svg files (like an icon pack).
Tested with feather icons.
Usage: python3 merge.py -o merged.svg icon_dir
Then open merged.svg in Affinity Designer, select all the layers
(e.g. with shift), and from Assets menu select 'Add from selection'.
"""
parser = argparse.ArgumentParser()
parser.add_argument("dir")
parser.add_argument("--out", "-o", required=True, help="Output file to write to")
args = parser.parse_args()
dir = Path(args.dir)
outfile = Path(args.out)
merged_svgs = ""
for file in dir.iterdir():
with open(file, "r") as in_file:
svg = in_file.read()
name = re.sub(r"\.svg$", "", file.name)
_svg = re.sub(r'(<svg.*?) xmlns=".*?"(.*?>)', r"\1\2", svg)
_svg = re.sub(r'(<svg.*?) width="[0-9]*"(.*?>)', r"\1\2", _svg)
_svg = re.sub(r'(<svg.*?) height="[0-9]*"(.*?>)', r"\1\2", _svg)
_svg = re.sub(r'(<svg.*?) viewBox="[0-9 ]*"(.*?>)', r"\1\2", _svg)
_svg = re.sub(r"<svg( ?)", r"<g\1", _svg)
_svg = re.sub(r"</svg>", "</g>", _svg)
svg_group = re.sub(r"<g([ >])", f'<g id="{name}"\\1', _svg)
merged_svgs = "\n".join((merged_svgs, svg_group))
final_svg = (
f'<?xml version="1.0" encoding="UTF-8"?>\n'
f'<svg width="100px" height="100px" viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">\n'
f"{merged_svgs}\n"
f"</svg>\n"
)
with open(outfile, "w") as out_file:
out_file.write(final_svg)
if __name__ == "__main__":
main()