简体   繁体   English

如何通过pyuno提取LibreOffice calc中当前选择的单元格范围?

[英]How can you extract the currently-selected range of cells in LibreOffice calc via pyuno?

When using pyuno in a LibreOffice / OpenOffice calc python macro, I would like simply to be able to select a range of cells, and when the macro is run, for all of the cell data (eg as some iterable object) to be able to be retrieved within the python script, so that it can be manipulated. 在LibreOffice / OpenOffice calc python宏中使用pyuno时,我只希望能够选择一系列单元格,并且在运行宏时,对于所有单元格数据(例如,作为一些可迭代对象),可以在python脚本中检索,以便可以对其进行操作。 I have found hardly any documentation on this, and would welcome some example code that demonstrates how to do this. 我几乎没有关于此的任何文档,并且欢迎一些示例代码演示如何执行此操作。

After a very painful time of trial and error (thanks to the scant documentation and examples on pyuno usage anywhere -- please correct me if I've overlooked something), I ended up with the following code that seems to do what I'm after: 经过一段非常痛苦的尝试和错误(由于缺少有关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))

This can be put into a python file, and stored (at least with Ubuntu 14.04) in the ~/.config/libreoffice/4/user/Scripts/python directory, and then as long as the libreoffice-script-provider-python package is installed, it can be run from within LibreOffice Calc via the Tools->Macros->Run Macro menu option. 可以将其放入python文件,并存储(至少在Ubuntu 14.04中)到~/.config/libreoffice/4/user/Scripts/python目录中,然后再存储到libreoffice-script-provider-python包中已安装,可以通过“ 工具”->“宏”->“运行宏”菜单选项在LibreOffice Calc中运行 Or it can be bound to a keyboard shortcut using the Tools->Customize->Keyboard dialog. 也可以使用“ 工具”->“自定义”->“键盘”对话框将其绑定到键盘快捷键。

For a more complete example that allows one to load data from LibreOffice Calc into Octave for further analysis, see this pyuno script . 有关允许将LibreOffice Calc中的数据加载到Octave中进行进一步分析的更完整示例,请参阅此pyuno脚本

Or you could try something like this, which I think is more easily understood. 或者您可以尝试类似的方法,我认为这更容易理解。

    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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 LibreOffice 中安装或激活 PyUno? - How do you install or activate PyUno in LibreOffice? 如何在 LibreOffice Calc 中使用 PyUNO 更改单元格边框的 LineWidth? - How to change the LineWidth of a cell border using PyUNO in LibreOffice Calc? 如何将2d libreoffice calc命名范围分配给python变量。 可以在Libreoffice Basic中做到 - How to assign a 2d libreoffice calc named range to a python variable. Can do it in Libreoffice Basic 使用pyuno对calc文档中的单元格范围进行排序 - Sorting cell range in a calc document with pyuno python-uno 如何用 pipe 操作 libreoffice-calc? - How python-uno can manipulate libreoffice-calc with pipe? Excel (LibreOffice Calc) 显示空白单元格,但 openpyxl 显示值 - Excel (LibreOffice Calc) show blank cells but openpyxl shows values 将细胞从Libreoffice Calc复制到Spyder中的IPython控制台 - Copying cells from Libreoffice Calc to IPython console in Spyder 如何从命令行Python脚本中保存LibreOffice Calc电子表格中的所有工作表 - How can I save ALL sheets in a LibreOffice Calc Spreadsheet from a command-line Python script 为私有 Python 构建安装 pyuno (LibreOffice) - Installing pyuno (LibreOffice) for private Python build 如何将数据从 LibreOffice Calc 导入 SQL 数据库? - How to import data from LibreOffice Calc to a SQL database?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM