从python执行LibreOffice Calc基本宏

问题描述 投票:1回答:1

我正在尝试使用Python自动化LibreOffice电子表格。我得到一个桌面,然后使用

打开电子表格
file_url = uno.systemPathToFileUrl(os.path.abspath("/path/to/file/estimation.xlsm"))
doc = desktop.loadComponentFromURL(file_url, "_blank", 0, oo_properties(MacroExecutionMode=4))

以下代码将打印基本脚本

the_basic_libs = doc.BasicLibraries
the_vba = the_basic_libs.getByName("VBAProject")
the_takerate = the_vba.getByName("TakeRate")
print(the_takerate)

打印的模块的第一行是:

Rem Attribute VBA_ModuleType=VBAModule
Option VBASupport 1

Public Sub TakeRateScenarioAnalysis()
Dim StartCell As Range

我将脚本带到

oor = OORunner()
msp = oor.get_context().getValueByName("/singletons/com.sun.star.script.provider.theMasterScriptProviderFactory")
sp = msp.createScriptProvider("")
scriptx = sp.getScript("vnd.sun.star.script:VBAProject.TakeRate.TakeRateScenarioAnalysis?language=Basic&location=document")

返回以下错误

Traceback (most recent call last):
  File "./runProjectEstimate.py", line 198, in <module>
    scriptx = sp.getScript("vnd.sun.star.script:VBAProject.TakeRate.TakeRateScenarioAnalysis?language=Basic&location=document")
__main__.ScriptFrameworkErrorException: The following Basic script could not be found:
library: 'VBAProject'
module: 'TakeRate'
method: 'TakeRateScenarioAnalysis'
location: 'document'

脚本URI是否有问题?我不知道为什么可以打印脚本,但是脚本提供者找不到它。

python libreoffice uno
1个回答
0
投票
scriptx = sp.getScript( 'vnd.sun.star.script:Standard.Module1.TakeRateScenarioAnalysis?' 'language=Basic&location=application')

但是,正如您的问题所述,这没有:

scriptx = sp.getScript(
    "vnd.sun.star.script:VBAProject.TakeRate.TakeRateScenarioAnalysis?"
    "language=Basic&location=document")

根据这些结果,当宏存储在文档中时,似乎不可能以这种方式调用宏。这很可能是权限问题。从可能是由其他人创建的文档中调用宏是传播病毒的一种好方法,因此LO试图防止这种情况。

VBAProject库移到My Macros而不是文档内部是否可以?然后一切都会按预期进行。

几个相关的链接可能会给出更多的想法:

How to call an existing LibreOffice python macro from a python script

© www.soinside.com 2019 - 2024. All rights reserved.