简体   繁体   English

使用python启动openoffice进程以与pyuno一起使用

[英]start openoffice process with python to use with pyuno using subprocess

I use this command to start openoffice: 我使用以下命令启动openoffice:

soffice --accept="socket,host=localhost,port=8100;urp;StarOffice.Service" --headless --nofirststartwizard

The following command will ensure that openoffice is accepting connections on port 8100: 以下命令将确保openoffice在端口8100上接受连接:

netstat -nap | grep office

output: 输出:

tcp        0      0 127.0.0.1:8100          0.0.0.0:* LISTEN     2467/soffice.bin 

Python script to start openoffice process: 使用Python脚本启动openoffice流程:

command = [
    'soffice',
    '--accept=socket,host=localhost,port=8100;urp;StarOffice.Service',
    '--headless',
    '--nofirststartwizard'
]
subprocess.Popen(command, shell=True)

For some reason, the netstat command outputs nothing when i try to start openoffice with this python script. 由于某种原因,当我尝试使用此python脚本启动openoffice时,netstat命令不会输出任何内容。 the process is there, but it does not accept connections. 该过程已存在,但不接受连接。 What am i doing wrong ? 我究竟做错了什么 ?

From the documentation : 文档中

On Unix with shell=True, the shell defaults to /bin/sh. 在shell = True的Unix上,shell默认为/ bin / sh。 If args is a string, the string specifies the command to execute through the shell. 如果args是字符串,则该字符串指定要通过外壳执行的命令。

If args is a sequence, the first item specifies the command string, and any additional items will be treated as additional arguments to the shell itself. 如果args是序列,则第一项指定命令字符串,任何其他项都将被视为shell本身的其他参数。

Here, you should just remove shell=True to pass the arguments to soffice instead of passing the arguments to the shell: 在这里,您只需要删除shell=True即可将参数传递给soffice而不是将参数传递给shell:

subprocess.Popen(command)

To use shell=True , you need to build all arguments into a single command (arguments would need to be escaped of course): 要使用shell=True ,您需要将所有参数构建到一个命令中(当然,参数需要转义):

subprocess.Popen(command.join(' '), shell=True)

The following executes but UNO connections to the pipe don't work: 执行以下操作,但与管道的UNO连接不起作用:

soffice = subprocess.Popen([ '/usr/bin/soffice', '--accept="pipe,name=hello;urp;"', '--norestore', '--nologo', '--nodefault', '--headless', ])

If I execute this from the terminal pipe connections work just fine: 如果我从终端管道连接执行此操作,则工作正常:

/usr/bin/soffice --accept="pipe,name=hello;urp;" --norestore --nologo --nodefault --headless

I can see from my debugger that subprocess.Popen is created successfully and that the args look correct and that it has a pid. 从调试器中可以看到subprocess.Popen已成功创建,并且args看起来正确并且具有pid。 I am not sure why this happens. 我不知道为什么会这样。 Can anybody explain this? 有人可以解释吗?

I eventually got it to work as follows: 我最终使它按如下方式工作:

soffice = subprocess.Popen(' '.join([ '/usr/bin/soffice', '--accept="pipe,name=hello;urp;"', '--norestore', '--nologo', '--nodefault', '--headless', ]), shell=True)

Do note however, that closing the pid with soffice.kill() leaves behind some processes. 但是请注意,使用soffice.kill()关闭pid会留下一些进程。

See the question here regarding this problem: OpenOffice Forum Question 29873 请参阅此处有关此问题的问题: OpenOffice论坛问题29873

I ran into a nearly identical issue, and it drove me nuts until I figured it out. 我遇到了几乎相同的问题,这让我发疯,直到我弄清楚了。 Fortunately, the fix is simple. 幸运的是,修复很简单。

There are two ways to fix the call to Popen in the original question: 有两种方法可以解决原始问题中对Popen的调用:

  1. either take out , shell=True 要么取出, shell=True
  2. or add quotes to the second item in command like this: 或在command的第二项中添加引号,如下所示:

    '--accept="socket,host=localhost,port=8100;urp;StarOffice.Service"'

The problem is that the shell doesn't parse the args correctly without those quotes, so either don't use the shell (better way), or quote the one argument as above. 问题在于,如果没有这些引号,外壳程序将无法正确解析args,因此,请不要使用外壳程序(更好的方式),或者如上所述引用一个参数。 I say that not using the shell is the better way because then it's simple to shut down soffice using the .terminate() method of the object returned by Popen . 我说,不使用外壳是更好的办法,因为那是简单的关闭soffice使用.terminate()返回的对象的方法Popen Otherwise you need to use a library like psutil to find all your child processes and kill them off yourself, because as Scott P. pointed out, terminating the shell doesn't stop soffice. 否则,您需要使用psutil之类的库来查找所有子进程并将其杀死,因为正如Scott P.所指出的那样,终止shell并不能终止soffice。

Scott P: The reason your first call to Popen doesn't work, is because you have the quotes in the second item, but aren't using the shell. 斯科特警:你先打电话的原因Popen行不通,是因为在第二个项目的报价,但没有使用的外壳。 The shell would remove the quotes when it parsed the command line, but because you're not using it they remain, and then soffice doesn't interpret the argument the way you're expecting. Shell会在解析命令行时删除引号,但是由于您没有使用引号,因此保留了它,然后soffice不会按照您期望的方式解释该参数。 Likewise, that's why your second call to Popen does work. 同样的,这就是为什么你到第二个呼叫Popen 确实工作。

Another illustration of this issue is here: Error calling LibreOffice from Python 此问题的另一个例证在这里: 从Python调用LibreOffice时出错

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM