远程控制或脚本打开 Office 以从 Python 编辑 Word 文档 [英] Remote control or script Open Office to edit Word document from Python

查看:39
本文介绍了远程控制或脚本打开 Office 以从 Python 编辑 Word 文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想(最好在 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 格式,但这可能不相关?)

推荐答案

我想说的是使用 Python-UNO 桥.这对你有用吗?

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

这篇关于远程控制或脚本打开 Office 以从 Python 编辑 Word 文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆