远程控制或脚本Open Office从Python编辑Word文档 [英] Remote control or script Open Office to edit Word document from Python
问题描述
我想(最好在Windows上)在特定文档上启动Open Office,搜索固定的字符串,并将其替换为程序选择的另一个字符串.
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.
如何从外部Python程序执行此操作? OLE东西?原生Python脚本解决方案?
How do I do that, from an external Python program? OLE-something? The native Python scripting solution?
(文档为Word 97-2003格式,但这可能不相关吗?)
推荐答案
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)
请参见 XSearchable文档有关搜索的详细信息.另外,请确保使用以下命令行启动OpenOffice:swriter "-accept=socket,host=localhost,port=2002;urp;"
.
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;"
.
这篇关于远程控制或脚本Open Office从Python编辑Word文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!