问题描述
我正在尝试使用Python自动执行LibreOffice电子表格.我得到一个桌面,然后使用
I am trying to automat a LibreOffice spreadsheet using Python. I get a desktop and open the spreadsheet with
file_url = uno.systemPathToFileUrl(os.path.abspath("/path/to/file/estimation.xlsm"))
doc = desktop.loadComponentFromURL(file_url, "_blank", 0, oo_properties(MacroExecutionMode=4))
以下代码将打印基本脚本
The following code will print the basic script
the_basic_libs = doc.BasicLibraries
the_vba = the_basic_libs.getByName("VBAProject")
the_takerate = the_vba.getByName("TakeRate")
print(the_takerate)
打印的模块的第一行是:
The first lines of the module printed are:
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是否存在问题?我不知道为什么我可以打印脚本,但是脚本提供者找不到它.
Is there a problem with the script URI? I don't know why I can print the script but the script provider cannot find it.
推荐答案
以下内容对我有用:
scriptx = sp.getScript(
'vnd.sun.star.script:Standard.Module1.TakeRateScenarioAnalysis?'
'language=Basic&location=application')
但是,正如您所描述的那样,这并没有:
However, as described in your question, this did not:
scriptx = sp.getScript(
"vnd.sun.star.script:VBAProject.TakeRate.TakeRateScenarioAnalysis?"
"language=Basic&location=document")
将您的VBAProject
库移动到My Macros
中而不是在文档内部是否可以接受?然后一切都会按预期进行.
Would it be acceptable to move your VBAProject
library into My Macros
instead of inside the document? Then everything should work as expected.
一些可能会提供更多想法的相关链接:
A couple of related links that may give more ideas:
- How to call an existing LibreOffice python macro from a python script
- Running Libreoffice BASIC macro from python
编辑:
有一种方法可以调用存储在文档中的宏.从文档中获取脚本提供者 ,而不是主脚本提供者.
There is a way to call macros stored in a document. Get the script provider from the document, not the master script provider.
oScriptProvider = doc.getScriptProvider()
oScript = oScriptProvider.getScript(
"vnd.sun.star.script:Standard.Module1.SayHello?"
"language=Basic&location=document")
oScript.invoke((), (), ())
这篇关于从python执行LibreOffice Calc Basic宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!