在 LibreOffice / OpenOffice calc python 宏中使用 pyuno 时,我只想能够选择一系列单元格,并且在运行宏时,所有单元格数据(例如,作为一些可迭代对象)能够在 python 脚本中检索,以便可以对其进行操作。我几乎没有找到任何关于此的文档,并且会欢迎一些示例代码来演示如何执行此操作。
问问题
2512 次
2 回答
3
经过一段非常痛苦的试验和错误之后(感谢任何地方关于 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
目录中(至少在 Ubuntu 14.04 中),然后只要libreoffice-script-provider-python
安装了包,就可以在 LibreOffice Calc 中通过Tools->Macros->Run Macro 运行菜单选项。或者可以使用工具->自定义->键盘对话框将其绑定到键盘快捷键。
有关允许将数据从 LibreOffice Calc 加载到 Octave 以进行进一步分析的更完整示例,请参阅此 pyuno 脚本。
于 2015-02-18T21:27:03.383 回答
0
或者你可以尝试这样的事情,我认为这更容易理解。
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.EndColumn
于 2015-03-06T16:33:49.110 回答