我似乎找不到这个问题的简单答案。我已经在Libreoffice Basic中成功工作了:

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

RefCells = NamedRange.getReferredCells()

Set MainRange = RefCells.getDataArray()


然后,我遍历MainRange并拉出我感兴趣的行。

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

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

任何帮助将非常感激。

谢谢。

最佳答案

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

开始:


  通过Uno进行通信的库Python-Uno已经在LibreOffice Python的路径中。要初始化上下文,请在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()


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

关于python - 如何将2d libreoffice calc命名范围分配给python变量。可以在Libreoffice Basic中做到,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55108669/

10-14 19:20
查看更多