Remote control or script Open Office to edit Word document from Python
Asked Answered
M

1

2

I want to (preferably on Windows) start Open Office on a particular document, search for a fixed string and replace it with another string selected by my program.

How do I do that, from an external Python program? OLE-something? The native Python scripting solution?

(The document is in the Word 97-2003 format, but that is probably not relevant?)

Moquette answered 16/10, 2011 at 10:22 Comment(0)
D
3

I'd say using the Python-UNO bridge. Does this work for you?

import uno

ctx = uno.getComponentContext()
service_manager = ctx.getServiceManager() 
desktop = service_manager.createInstanceWithContext("com.sun.star.frame.Desktop", ctx)
document = desktop.loadComponentFromURL("file:///file.doc", "_blank", 0, ())

replace_desc = document.createReplaceDescriptor() 
replace_desc.setSearchString("text_to_replace") 

find_iter = document.findFirst(replace_desc)
while find_iter:
    find_iter.String = "replacement_text"
    find_iter = document.findNext(find_iter.End, replace_desc)

See the XSearchable docs for details on searching. Also, make sure to have OpenOffice started with the following command line: swriter "-accept=socket,host=localhost,port=2002;urp;".

Dedans answered 16/10, 2011 at 10:59 Comment(12)
Perhaps silly question, but when Python says, ImportError: no module uno, what am I doing wrong? I have the standard Windows 2.7.2 Python installed.Moquette
It's an optional installable component for OpenOffice. See the Introduction to Python on OOo page in OpenOffice's Wiki for installation details.Dedans
It runs now, but Python crashes: AppName: python.exe AppVer: 0.0.0.0 ModName: vclmi.dll ModVer: 3.4.302.500 Offset: 0000f13aMoquette
Are you using the python version that came with your OpenOffice installation? You can find it in "Program Files\OpenOffice3.x\program\python.exe".Dedans
I do (didn't at first, but found that it is the common thing to do)Moquette
I kind of doubt if the crash is related to this. Since there is no callstack, could you try to execute it line by line to see when it crashes? Seems to work fine here...Dedans
desktop = service_manager.createInstanceWithContext("com.sun.star.frame.Desktop", ctx) <-- CRASHES ON THAT LINEMoquette
I will also try with OpenOffice instead of LibreOfficeMoquette
Also crashes on the same line with OpenOfficeMoquette
That's odd... could you try to run the example from the OpenOffice site itself? You can find it here, try to run the one under the "Hello World" heading. I'd say that one should work without problems. Take care to use the topmost script (there are more "Hello world" examples on that page).Dedans
Ah, yes :). You have to have it running with soffice "-accept=socket,host=localhost,port=2002;urp;"... which I had by default. I added this to the answer.Dedans
Now I have the problem that whatever URL to load I use, it says the URL is invalid. I can change documents that were already opened though... ah, found it! You need three slashes in the file:/// url. Thanks!Moquette

© 2022 - 2024 — McMap. All rights reserved.