Search code examples
pythonsubprocessinkscape

Python Subprocess.Run not running Inkscape pdf to svg


I am using Inkscape to take an input single page pdf file and to output an svg file. The following works from the command line

c:\progra~1\Inkscape\inkscape -z -f "N:\pdf_skunkworks\inflation-report-may-2018-page0.pdf" -l "N:\pdf_skunkworks\inflation-report-may-2018-page0.svg"

where -z is short for --without-gui, -f is short for input file, -l is short for --export-plain-svg. And that works from command line.

I could not get the equivalent to work from Python, either passing the command line as one long string or as separate arguments. stderr and stdout give no error as they both print None

import subprocess #import call,subprocess
#completed = subprocess.run(["c:\Progra~1\Inkscape\Inkscape.exe",r"-z -f \"N:\pdf_skunkworks\inflation-report-may-2018-page0.pdf\" -l \"N:\pdf_skunkworks\inflation-report-may-2018-page0.svg\""])
completed = subprocess.run(["c:\Progra~1\Inkscape\Inkscape.exe","-z", r"-f \"N:\pdf_skunkworks\inflation-report-may-2018-page0.pdf\"" , r"-l \"N:\pdf_skunkworks\inflation-report-may-2018-page0.svg\""])
print ("stderr:" + str(completed.stderr))
print ("stdout:" + str(completed.stdout))

Just to test OS plumbing I wrote some VBA code (my normal language) and this works

Sub TestShellToInkscape()
    '* Tools->References->Windows Script Host Object Model (IWshRuntimeLibrary)
    Dim sCmd As String
    sCmd = "c:\progra~1\Inkscape\inkscape -z -f ""N:\pdf_skunkworks\inflation-report-may-2018-page0.pdf"" -l ""N:\pdf_skunkworks\inflation-report-may-2018-page0.svg"""
    Debug.Print sCmd

    Dim oWshShell As IWshRuntimeLibrary.WshShell
    Set oWshShell = New IWshRuntimeLibrary.WshShell

    Dim lProc As Long
    lProc = oWshShell.Run(sCmd, 0, True)

End Sub

So I'm obviously doing something silly in the Python code. I'm sure experienced Python programmer could solve easily.


Solution

  • Swap your slashes:

    import subprocess #import call,subprocess
    completed = subprocess.run(['c:/Progra~1/Inkscape/Inkscape.exe',
          '-z', 
          '-f', r'N:/pdf_skunkworks/inflation-report-may-2018-page0.pdf' , 
          '-l', r'N:/pdf_skunkworks/inflation-report-may-2018-page0.svg'])
    print ("stderr:" + str(completed.stderr))
    print ("stdout:" + str(completed.stdout))
    

    Python knows to swap forward slashes for back slashes on windows OS, and your back slashes are currently acting as escape prefixes.