我正在使用 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-五月-2018-page0.svg"
其中 -z
是 --without-gui
的缩写,-f
是输入文件的缩写,-l
是 --export-plain-svg
的缩写。这可以从命令行运行。
我无法从 Python 中获得等效的工作,无论是将命令行作为一个长字符串还是作为单独的参数传递。 stderr
和 stdout
没有给出错误,因为它们都打印 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 程序员可以轻松解决。
最佳答案
交换斜杠:
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 操作系统上将正斜杠交换为反斜杠,并且您的反斜杠当前充当转义前缀。
关于Python Subprocess.Run 不运行 Inkscape pdf 到 svg,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50915483/