Remote control or script Open Office to edit a Word document from Python

I want (preferably on Windows) to run Open Office on a specific document, find a fixed line and replace it with another line selected by my program.

How to do this, from an external Python program? Ole sometime? Native Python scripting solution?

(The document is in Word 97-2003 format, but it probably doesn't matter?)

+2
source share
1 answer

I would 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) 

For more information on search, see XSearchable docs . Also, make sure OpenOffice is started with the following command line: swriter "-accept=socket,host=localhost,port=2002;urp;" .

+3
source

Source: https://habr.com/ru/post/919937/


All Articles