Python Subprocess.Run 不运行 Inkscape pdf 到 svg

标签 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-五月-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 程序员可以轻松解决。

最佳答案

交换斜杠:

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/

相关文章:

javascript - 无法将带有 SnapSVG 的元素添加到我的页面

Python 正则表达式 : force greedy match using alternation

Python、OpenCV、Raspberry Pi-3 - 属性错误 - 'NoneType' 对象

python - 使用 BeautifulSoup 进行多处理来改进 Wikipedia 抓取

python - 如何从 python 运行可执行文件并向它传递请求的参数?

php - PHP/Apache 中的 Inkscape 不会将字体呈现为 PNG

python - 为什么 TensorFlow 示例在增加批量大小时会失败?

Python 子进程 readlines() 挂起

Python 脚本没有输出

svg - Inkscape 忽略 <use>