Search code examples
pythonms-wordopenoffice.orglibreofficepyuno

How to save a document edited from PyUNO?


I have now succeeded in opening a Word 97-2003 (.doc) document and edited it from Python. But how do I save it?

I always get:

Traceback (most recent call last):
  File "office.py", line 55, in <module>
    model.storeToUrl('file:///c:/temp/out.doc', ())
AttributeError: storeToUrl

(Related question.)

What should the attributes be?

And how do I then close the document?


Solution

  • The other answer is all over the net, and is very confusing. In some examples, model is the TEXT object, storeToURL() and dispose() are methods of the document object, here is another implementation.

    from com.sun.star.beans import PropertyValue
    from unohelper import systemPathToFileURL
    
    # open a writer document object
    doc = desktop.loadComponentFromURL("private:factory/swriter", "_blank", 0, ())
    
    .....
    
    url = systemPathToFileUrl('c:/out.doc')
    
    # NOTE THAT ARGS IS A TUPLE OF PROPERTY VALUES
    args = (PropertyValue('FilterName', 0, 'MS Word 97', 0),)
    
    doc.storeToURL(url, args)
    
    # close the document
    doc.dispose()