How can you extract the currently-selected range of cells in LibreOffice calc via pyuno?
libreoffice-calc, openoffice-calc, python, pyuno
Solution
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:
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. 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.
Problem
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 (e.g. as some iterable object) to be able to be retrieved within the python script, so that it can be manipulated. I have found hardly any documentation on this, and would welcome some example code that demonstrates how to do this.