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

后端 未结 1 1844
暖寄归人
暖寄归人 2021-01-07 01:28

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.

Ho

相关标签:
1条回答
  • 2021-01-07 01:41

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

    0 讨论(0)
提交回复
热议问题