Range selection with a dialog box in a python Macro

I try to build some additional statistics capabilities (Menu: data->statistics). if the statistics part works in my python macro, i don’t know how to create the same type of dialog box as in “data->statistics” to select the cells range. I found in forum a solution but only in “Starbasic” (the link [Solved~] Obtaining a Cell Range Selection from a Dialog (View topic) • Apache OpenOffice Community Forum)

Here attached the file I work on.1st Python Macro.ods
In this macro python, the actual selection is clustered (Hierarchical ascendant classification).
Select the whole table (A1:D31) and launch the macro “macro_WP”

Thank you for your help


Are you referring to [Solved~] Obtaining a Cell Range Selection from a Dialog (View topic) • Apache OpenOffice Community Forum posted by FJCC? Please edit the question to give the link.

What operating system? On Windows, python for LibreOffice does not come with numpy, so macro_WP() raises an exception while loading.

Regarding question 2, it seems your code already does this:

cell2 = sheet.getCellRangeByName("F1")
cell2.Value = ods[2,2]

question 2) Yes It’s much more the graphical output I don’t know how to display in the worksheet.

I work on Linux. On Linux, both installations are independent. My question is about establishing easily a bridge between the two. Integrating the power of python in libreoffice would be a major asset over other spreadsheets.

Here is a range selection example based on Range Selection - Apache OpenOffice Wiki. It can be run in a new spreadsheet.

As is often the case with Python-UNO, the code starts a separate thread so that the spreadsheet interface doesn’t lock up. Separate threads do not seem to be necessary for Basic or Java macros, presumably because those interpreters do not block the main LO graphics thread, whereas the Python interpreter does for some reason.

import threading
import time

import uno
import unohelper
from com.sun.star.beans import PropertyValue
from com.sun.star.sheet import XRangeSelectionListener

def getRange(dummy_context=None):
    controller = XSCRIPTCONTEXT.getDocument().getCurrentController()
    xRngSel = controller
    aListener = ExampleRangeListener()
    aArguments = (
        createProp("Title", "Please select a range"),
        createProp("CloseOnMouseRelease", False)
    t1 = WaiterThread(xRngSel, aListener)

class ExampleRangeListener(XRangeSelectionListener, unohelper.Base):
    def __init__(self):
        self.aResult = "not yet"
    def done(self, aEvent):
        self.aResult = aEvent.RangeDescriptor
    def aborted(self, dummy_aEvent):
        self.aResult = "nothing"
    def disposing(self, dummy_aEvent):

class WaiterThread(threading.Thread):
    def __init__(self, xRngSel, aListener):
        self.xRngSel = xRngSel
        self.aListener = aListener

    def run(self):
        for dummy in range(120):  # don't wait more than 60 seconds
            if self.aListener.aResult != "not yet":
        doc = XSCRIPTCONTEXT.getDocument()
        sheet = doc.getSheets().getByIndex(0)
        cell = sheet.getCellByPosition(0,0)

def createProp(name, value):
    """Creates an UNO property."""
    prop = PropertyValue()
    prop.Name = name
    prop.Value = value
    return prop

@jimk Looks good. This works nicely on Linux. When I did something similar, I needed non-contiguous selection. For that I simply created a non modal dialog (in python) and the selected cells are in getCurrentSelection() such as:


transfer back in libreoffice's worksheet some mathematical results is obtained by:

cell2 = sheet.getCellRangeByName("F1")
cell2.Value = XXX
cell2 = sheet.getCellRangeByName("F2")
cell2.Value = YYY

etc. etc. and you already know how to do this in your python script. How these returned data are used by the spreadsheet to present some graphical output (by the spreadsheet) is unrelated.

(If you do not know how to create an image, to put in the spreadsheet, from all these, unrelated to LO imports such as numpy, scipy, matplotlib etc., then better ask any of the related python forums.)


I obtain a specific representation (dendrogram after plt.show) I would like to insert it in libreoffice’s worksheet just like a picture…If someone has a solution for a picture file, it’s probably the bigger part of the solution…

Your answer is not relevant. Can you delete his response status and integrate it as a comment. Thank you