start openoffice process with python to use with pyuno using subprocess

1.6k views Asked by At

I use this command to start 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:

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:

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. the process is there, but it does not accept connections. What am i doing wrong ?

3

There are 3 answers

1
Nicolas Cortot On BEST ANSWER

From the documentation:

On Unix with shell=True, the shell defaults to /bin/sh. If args is a string, the string specifies the command to execute through the shell.

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.

Here, you should just remove shell=True to pass the arguments to soffice instead of passing the arguments to the 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):

subprocess.Popen(command.join(' '), shell=True)
0
Scott P. On

The following executes but UNO connections to the pipe don't work:

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. 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.

See the question here regarding this problem: OpenOffice Forum Question 29873

0
NotoriousDBA On

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:

  1. either take out , shell=True
  2. or add quotes to the second item in command like this:

    '--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. 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. 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.

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. 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. Likewise, that's why your second call to Popen does work.

Another illustration of this issue is here: Error calling LibreOffice from Python