user2905179
user2905179

Reputation: 21

Execute a script inside another script in python

I want to make a python script that will run OpenOffice and create an odt file with Py-Uno.

That's what I have tried:

import os
import uno

os.system("soffice '--accept=socket,host=localhost,port=2002;urp;'")

local = uno.getComponentContext()
resolver = local.ServiceManager.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver", local)

context = resolver.resolve("uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext")

desktop = context.ServiceManager.createInstanceWithContext("com.sun.star.frame.Desktop", context)

#Start new document
document = desktop.loadComponentFromURL("private:factory/swriter", "_blank", 0, ())
cursor = document.Text.createTextCursor()

#Insert text
document.Text.insertString(cursor, "This text is being added to openoffice using python and uno package.", 0)

document.Text.insertString(cursor, "\n\nThis is a new paragraph.", 0)

When I run this script, OpenOffice opens, but it doesn't create a new file. It seems that while os.system is running, the rest of the script isn't executed. What could I do to make it work? Thanks for the help!

Upvotes: 0

Views: 108

Answers (1)

smeso
smeso

Reputation: 4285

You should use subprocess module:

import subprocess
subprocess.Popen(["soffice","'--accept=socket,host=localhost,port=2002;urp;'"])

os.system will wait for the new process to terminate. Using Popen without redirecting process' stdout it will be executed in background.

Upvotes: 2

Related Questions