我正在使用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))


只是为了测试OS管道,我写了一些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程序员可以轻松解决。

最佳答案

交换您的斜杠:

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 OS上将正斜杠交换为反斜杠,并且您的反斜杠当前充当转义前缀。

10-08 08:09
查看更多