less copy protection, more size visualization
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 

38 行
1.2 KiB

  1. import sys
  2. import re
  3. from xml.etree import ElementTree
  4. from pathlib import Path
  5. import os
  6. def add_bounds(directory):
  7. for path in Path(directory).rglob("*.svg"):
  8. if path.name.startswith("z_"):
  9. print("Renamed {0}".format(path))
  10. renamed = path.with_name(path.name[2:])
  11. if os.path.exists(renamed):
  12. os.unlink(renamed)
  13. os.rename(path, renamed)
  14. for path in Path(directory).rglob('*.svg'):
  15. tree = ElementTree.ElementTree()
  16. parsed = tree.parse(path)
  17. if "width" not in parsed.attrib:
  18. print(f"Rewriting {path}")
  19. width, height = parsed.attrib["viewBox"].split(" ")[-2:]
  20. # I'd rather avoid mangling the XML files by parsing them first
  21. # XML is awful and we never should have invented it :(
  22. with open(path, "r", encoding="utf-8") as file:
  23. data = file.read()
  24. data = re.sub('viewBox="0 0', f'width="{width}" height="{height}" viewBox="0 0', data)
  25. with open(path, "w", encoding="utf-8") as file:
  26. file.write(data)
  27. if __name__ == "__main__":
  28. add_bounds(sys.argv[1])