我的首选方法是将python脚本放在LibreOffice user directory的Scripts/python子文件夹中。然后在底部添加此函数:def call_basic_macro():
document = XSCRIPTCONTEXT.getDocument()
frame = document.getCurrentController().getFrame()
ctx = XSCRIPTCONTEXT.getComponentContext()
dispatcher = ctx.ServiceManager.createInstanceWithContext(
'com.sun.star.frame.DispatchHelper', ctx)
url = document.getURL()
macro_call = ('macro:///Standard.Module1.Macro1("%s")' % url)
dispatcher.executeDispatch(frame, macro_call, "", 0, ())
g_exported_scripts=call_basic_macro,
现在从Writer运行python脚本,方法是转到Tools -> Macros -> Run Macro。展开My Macros并选择脚本的名称。在
另一种更接近Excel示例的方法是使用系统调用启动LibreOffice的侦听实例:
^{pr2}$
我通常用shell脚本(Windows上的批处理文件)而不是python来完成这一部分。然后在python中,从实例中获取文档上下文:import uno
localContext = uno.getComponentContext()
之后,代码看起来与上面的代码类似。注意,对于Windows上的这种方法,必须使用LibreOffice附带的python.exe来加载uno模块。在
第三种方法是简单地进行系统调用:soffice "macro:///Standard.Module1.Macro1()"