Python Subprocess.Run没有运行Inkscape pdf到svg

时间:2018-06-18 18:30:49

标签: python subprocess inkscape

我正在使用Inkscape获取输入单页pdf文件并输出svg文件。以下是命令行

的工作原理

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"

其中-z--without-gui的缩写,-f是输入文件的缩写,-l--export-plain-svg的缩写。这可以从命令行工作。

我无法从Python中获得相同的功能,无论是将命令行作为一个长字符串传递还是作为单独的参数传递。 stderrstdout都没有出错,因为它们都会打印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))

为了测试操作系统管道,我写了一些VBA代码(我的普通语言),这个工作

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

所以我显然在Python代码中做了些蠢事。我确信经验丰富的Python程序员可以轻松解决。

1 个答案:

答案 0 :(得分:2)

交换你的斜杠:

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知道在Windows操作系统上交换正斜杠用于反斜杠,而你的反斜杠当前作为转义前缀。