inkscape-convert.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import os
  2. import pathlib
  3. import sys
  4. import subprocess
  5. inkscape = None
  6. if os.name == 'nt':
  7. # Possible paths to check for the installation
  8. inkscapePaths = [
  9. r"C:\Program Files\Inkscape\bin\inkscape.exe",
  10. r"C:\Program Files\Inkscape\inkscape.exe",
  11. r"C:\Program Files (x86)\Inkscape\bin\inkscape.exe",
  12. r"C:\Program Files (x86)\Inkscape\inkscape.exe",
  13. ]
  14. for path in inkscapePaths:
  15. if pathlib.Path(path).exists():
  16. inkscape = path
  17. if not inkscape:
  18. print("Can't find Inkscape installation, aborting.")
  19. sys.exit()
  20. elif os.name == 'posix':
  21. inkscape = 'inkscape'
  22. print("Conversion started")
  23. for f in sys.argv[1:]:
  24. fullpath = pathlib.Path(f)
  25. if fullpath.exists() and fullpath.suffix == '.svg':
  26. print(f"Converting {fullpath} to PNG")
  27. p = subprocess.run([
  28. inkscape,
  29. "--export-background-opacity=1",
  30. "--export-type=png",
  31. "--export-dpi=400",
  32. f"--export-filename={fullpath.with_suffix('.png')}",
  33. fullpath,
  34. ],
  35. stdin=subprocess.PIPE,
  36. stdout=subprocess.PIPE,
  37. stderr=subprocess.STDOUT,
  38. universal_newlines=True)
  39. print(f"Converting {fullpath} to PDF")
  40. subprocess.run([
  41. inkscape,
  42. "-T",
  43. "--export-background-opacity=1",
  44. "--export-type=pdf",
  45. "--export-dpi=400",
  46. f"--export-filename={fullpath.with_suffix('.pdf')}",
  47. fullpath,
  48. ],
  49. stdin=subprocess.PIPE,
  50. stdout=subprocess.PIPE,
  51. stderr=subprocess.STDOUT,
  52. universal_newlines=True)
  53. else:
  54. print(f"Path {f} does not exist")
  55. print("Complete")