当在LibreOffice / OpenOffice calc宏中使用pyuno时,我只希望能够选择一个单元格的范围,并且当宏运行时,所有的单元数据(例如一些可迭代的对象)都能够在python中检索,以便能够对其进行操作。我几乎没有找到关于这方面的任何文档,并且欢迎一些示例代码来演示如何做到这一点。
发布于 2015-02-18 21:27:03
在经历了一段非常痛苦的尝试和错误之后(多亏了很少的文档和任何地方使用pyuno的例子--如果我忽略了什么,请纠正我),我最终得到了以下代码,这些代码似乎是在做我想要做的事情:
import uno
doc = XSCRIPTCONTEXT.getDocument()
def mymodule():
ctrlr = doc.CurrentController
sel = ctrlr.getSelection()
x = sel.getDataArray()
# now the data is available as nested tuples in x, so do something with it
file('/tmp/out', 'w').write(repr(x))这可以放在python文件中,并存储在~/.config/libreoffice/4/user/Scripts/python目录中(至少在Ubuntu14.04中),然后只要安装了libreoffice-script-provider-python包,就可以通过Tools->Macros->Run选项在LibreOffice Calc中运行。也可以使用工具->Customize->键盘对话框将其绑定到键盘快捷方式。
有关允许将数据从LibreOffice Calc加载到Octave以进行进一步分析的更完整的示例,请参见这个pyuno脚本。
发布于 2015-03-06 16:33:49
或者你可以尝试这样的方法,我认为这更容易理解。
desktop = XSCRIPTCONTEXT.getDesktop()
model = desktop.getCurrentComponent()
try:
sheets = model.getSheets()
except AttributeError:
raise Exception("This script is for Calc Spreadsheets only")
#sheet = sheets.getByName('Sheet1')
sheet = model.CurrentController.getActiveSheet()
oSelection = model.getCurrentSelection()
oArea = oSelection.getRangeAddress()
first_row = oArea.StartRow
last_row = oArea.EndRow
first_col = oArea.StartColumn
last_col = oArea.EndColumnhttps://stackoverflow.com/questions/28593278
复制相似问题