如何与交互式子进程通信

How to communicate with interactive subprocess

我有一个包含 56 个对象的 SVG 文件,我想将其导出为单独的 PNG 文件。我可以用 subprocess 模块做到这一点,但它涉及执行 Inkscape 56 次,我正在寻找更好的方法。 Inkscape 有一个带有 shell 模式的命令行界面,我已经能够使用 shell 导出项目,但是当我尝试使用 subprocess 模块执行此操作时,我发出的任何命令都太长了:

import subprocess as sp

proc =sp.Popen('inkscape -z --shell'.split(), stdin=sp.PIPE, stdout=sp.PIPE, stderr=sp.PIPE, universal_newlines=True)
x=proc.communicate('--help')
print(x)

("Inkscape 0.91 r13725 interactive shell mode. Type 'quit' to quit.\n>ERROR: Command line too long\n", '')

我做错了什么?

inkscape 错误具有误导性。问题是缺少 \n.

proc.communicate('--help\n')