如何将 2d libreoffice calc 命名范围分配给 python 变量。可以在 Libreoffice Basic 中完成

How to assign a 2d libreoffice calc named range to a python variable. Can do it in Libreoffice Basic

我似乎找不到问题的简单答案。我在 Libreoffice Basic 中成功运行:

NamedRange = ThisComponent.NamedRanges.getByName("transactions_detail")

RefCells = NamedRange.getReferredCells()

Set MainRange = RefCells.getDataArray()

然后我遍历 MainRange 并提取我感兴趣的行。

我可以在 python 宏中做类似的事情吗?我可以将 2d 命名范围分配给 python 变量,还是必须遍历该范围以分配单个单元格?

我是 python 的新手,但希望将我的迭代密集型宏函数转换为 python 以期使其更快。

如有任何帮助,我们将不胜感激。

谢谢。

可以从 Python 使用库 pyuno 操作 LibreOffice。不幸的是,pyuno 的文档不完整,但阅读 this tutorial 可能会有所帮助。

开始:

Python-Uno, the library to communicate via Uno, is already in the LibreOffice Python’s path. To initialize your context, type the following lines in your python shell :

import socket  # only needed on win32-OOo3.0.0
import uno

# get the uno component context from the PyUNO runtime
localContext = uno.getComponentContext()

# create the UnoUrlResolver
resolver = localContext.ServiceManager.createInstanceWithContext(
                "com.sun.star.bridge.UnoUrlResolver", localContext )

# connect to the running office
ctx = resolver.resolve( "uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext" )
smgr = ctx.ServiceManager

# get the central desktop object
desktop = smgr.createInstanceWithContext( "com.sun.star.frame.Desktop",ctx)

# access the current writer document
model = desktop.getCurrentComponent()

然后要获取命名范围并将数据作为数组访问,可以使用以下方法:

NamedRange = model.NamedRanges.getByName(“Test Name”)
MainRange = NamedRange.getDataArray()

但是我不确定这是否会带来明显的性能提升。