从python运行Libreoffice BASIC宏 [英] Running Libreoffice BASIC macro from python

查看:124
本文介绍了从python运行Libreoffice BASIC宏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在LibreOffice BASIC中有一个宏,我想从我的python程序中运行它.我发现一些线程在其中使用此代码:

I've a macro in LibreOffice BASIC and I want to run it from my python program. I've found some threads in which they use this code:

import os
import win32com.client

if os.path.exists("excelsheet.xlsm"):
    xl=win32com.client.Dispatch("Excel.Application")
    xl.Workbooks.Open(Filename="C:\Full Location\To\excelsheet.xlsm", ReadOnly=1)
    xl.Application.Run("excelsheet.xlsm!modulename.macroname")
##    xl.Application.Save() # if you want to save then uncomment this line and change delete the ", ReadOnly=1" part from the open function.
    xl.Application.Quit() # Comment this out if your excel script closes
    del xl

但这是用于Windows Excell程序的,而我要用于LibreOffice程序.可以这样做吗?

But this is for windows Excell program and I want for the LibreOffice program. Is it possible to do this?

谢谢:)

推荐答案

我的首选方法是将python脚本放入

My preferred way is to put the python script in the Scripts/python subfolder of your LibreOffice user directory. Then add this function at the bottom:

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,

现在转到 Tools->,从Writer运行python脚本.宏->运行宏.展开我的宏,然后选择脚本的名称.

Now run the python script from Writer by going to Tools -> Macros -> Run Macro. Expand My Macros and select the name of the script.

似乎更接近您的Excel示例的另一种方法是通过系统调用启动LibreOffice的侦听实例:

Another way which seems closer to your Excel example is to start a listening instance of LibreOffice with a system call:

start soffice -accept=socket,host=0,port=2002;urp;

我通常在shell脚本(Windows上的批处理文件)而不是python中执行此操作.然后在python中,从实例获取文档上下文:

I typically do that part in a shell script (batch file on Windows) rather than python. Then in python, get the document context from the instance:

import uno
localContext = uno.getComponentContext()

此后,代码将类似于上面的代码.请注意,在Windows上使用这种方法时,必须使用LibreOffice随附的 python.exe 才能加载 uno 模块.

After that, the code would look similar to what is above. Note that with this approach on Windows, the python.exe included with LibreOffice must be used in order to load the uno module.

第三种方法是简单地进行系统调用:

A third way is to simply do a system call:

soffice "macro:///Standard.Module1.Macro1()"

有关此第三种方法的更多信息,请参见 https://forum.openoffice.org/en/forum/viewtopic.php?f=20&t=8232 .

For more on this third approach, see https://forum.openoffice.org/en/forum/viewtopic.php?f=20&t=8232.

这篇关于从python运行Libreoffice BASIC宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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