首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何将命名为range的2d libreoffice计算赋给python变量。可以在Libreoffice Basic中完成

如何将命名为range的2d libreoffice计算赋给python变量。可以在Libreoffice Basic中完成
EN

Stack Overflow用户
提问于 2019-03-12 03:03:06
回答 1查看 150关注 0票数 1

我似乎找不到这个问题的简单答案。我在Libreoffice Basic中成功地实现了这一点:

代码语言:javascript
复制
NamedRange = ThisComponent.NamedRanges.getByName("transactions_detail")

RefCells = NamedRange.getReferredCells()

Set MainRange = RefCells.getDataArray()

然后,我迭代MainRange并提取出我感兴趣的行。

我可以在python宏中做类似的事情吗?我可以给python变量分配一个2d命名范围吗?或者我必须遍历范围才能分配单个单元格吗?

我是python的新手,但我希望将我的迭代密集型宏函数转换为python,希望能让它更快。

任何帮助都将不胜感激。

谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-03-12 04:12:59

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

要开始:

通过Uno进行通信的库

Python-Uno已经在LibreOffice Python的路径中。要初始化上下文,请在python shell中键入以下行:

代码语言:javascript
复制
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()

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

代码语言:javascript
复制
NamedRange = model.NamedRanges.getByName(“Test Name”)
MainRange = NamedRange.getDataArray()

然而,我不确定这是否会带来显着的性能提升。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55108669

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档