0

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!

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.