我似乎无法找到问题的简单答案。我已成功地在Libreoffice Basic中工作:
NamedRange = ThisComponent.NamedRanges.getByName("transactions_detail")
RefCells = NamedRange.getReferredCells()
Set MainRange = RefCells.getDataArray()
然后我遍历MainRange并拉出我感兴趣的行。
我可以在python宏中做类似的事情吗?我可以为python变量分配2d命名范围,还是必须迭代范围以分配单个单元格?
我是python的新手,但希望将我的迭代密集型宏函数转换为python,以期让它更快。
任何帮助将非常感激。
谢谢。
可以使用库pyuno
从Python操纵LibreOffice。不幸的是,pyuno
的文档不完整,但通过this tutorial可能有所帮助。
开始:
Python-Uno是通过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()
但是我不确定这会导致明显的性能提升。