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?)

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Prof. Falken
  • 24,226
  • 19
  • 100
  • 173

1 Answers1

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

Community
  • 1
  • 1
jro
  • 9,300
  • 2
  • 32
  • 37
  • 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. – Prof. Falken Oct 16 '11 at 12:49
  • It's an optional installable component for OpenOffice. See the [Introduction to Python on OOo](http://wiki.services.openoffice.org/wiki/Using_Python_on_Windows) page in OpenOffice's Wiki for installation details. – jro Oct 16 '11 at 13:40
  • It runs now, but Python crashes: AppName: python.exe AppVer: 0.0.0.0 ModName: vclmi.dll ModVer: 3.4.302.500 Offset: 0000f13a – Prof. Falken Oct 16 '11 at 14:07
  • Are you using the python version that came with your OpenOffice installation? You can find it in "Program Files\OpenOffice3.x\program\python.exe". – jro Oct 16 '11 at 14:24
  • I do (didn't at first, but found that it is the common thing to do) – Prof. Falken Oct 16 '11 at 18:00
  • 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... – jro Oct 16 '11 at 18:07
  • 1
    desktop = service_manager.createInstanceWithContext("com.sun.star.frame.Desktop", ctx) <-- CRASHES ON THAT LINE – Prof. Falken Oct 16 '11 at 20:36
  • I will also try with OpenOffice instead of LibreOffice – Prof. Falken Oct 16 '11 at 21:00
  • Also crashes on the same line with OpenOffice – Prof. Falken Oct 16 '11 at 21:06
  • 1
    That's odd... could you try to run the example from the OpenOffice site itself? You can find it [here](http://udk.openoffice.org/python/python-bridge.html), 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). – jro Oct 17 '11 at 06:41
  • 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. – jro Oct 17 '11 at 07:18
  • 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! – Prof. Falken Oct 17 '11 at 07:22