【问题标题】:Python Subprocess.Run not running Inkscape pdf to svgPython Subprocess.Run 没有运行 Inkscape pdf 到 svg
【发布时间】:2018-11-27 15:42:51
【问题描述】:

我正在使用 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 程序员可以轻松解决。

【问题讨论】:

  • 可能您的进程甚至没有运行,没有出现异常?你能试试Popencommunicate吗?喜欢here
  • @Srini:感谢您的反馈。多亏了你,我运行了 SysInternals Process Monitor 来检查“生成堆栈”。这帮助我诊断了命令行,并结合 JacobIRR 的建议帮助我解决了问题。

标签: python subprocess inkscape


【解决方案1】:

交换你的斜线:

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

【讨论】:

  • 您让我走上了正确的道路,我已将工作代码粘贴到此答案中。谢谢。
猜你喜欢
  • 1970-01-01
  • 2015-07-07
  • 1970-01-01
  • 2011-11-11
  • 1970-01-01
  • 2011-12-10
  • 1970-01-01
  • 2021-12-11
  • 2014-05-17
相关资源
最近更新 更多