Ask Your Question

Range selection with a dialog box in a python Macro

asked 2018-06-08 12:22:08 +0200

tl67 gravatar image

updated 2018-06-11 07:57:21 +0200

Hello, 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

Here attached the file I work on.C:\fakepath\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


edit retag flag offensive close merge delete


Are you referring to posted by FJCC? Please edit the question to give the link.

Jim K gravatar imageJim K ( 2018-06-08 15:57:09 +0200 )edit

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

Jim K gravatar imageJim K ( 2018-06-08 16:03:42 +0200 )edit

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

cell2 = sheet.getCellRangeByName("F1")
cell2.Value = ods[2,2]
Jim K gravatar imageJim K ( 2018-06-08 16:07:12 +0200 )edit

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

tl67 gravatar imagetl67 ( 2018-06-08 16:42:12 +0200 )edit

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.

tl67 gravatar imagetl67 ( 2018-06-09 13:40:41 +0200 )edit

2 Answers

Sort by » oldest newest most voted

answered 2018-06-12 01:31:41 +0200

Jim K gravatar image

updated 2018-06-12 03:28:22 +0200

Here is a range selection example based on 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 import PropertyValue
from 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
edit flag offensive delete link more


@Jim K 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:

Ratslinger gravatar imageRatslinger ( 2018-06-12 03:51:52 +0200 )edit

answered 2018-06-09 06:28:18 +0200

Xoristzatziki gravatar image

updated 2018-06-09 06:32:42 +0200

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.)

edit flag offensive delete link more



I obtain a specific representation (dendrogram after 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...

tl67 gravatar imagetl67 ( 2018-06-09 09:36:19 +0200 )edit

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

tl67 gravatar imagetl67 ( 2018-06-10 09:46:30 +0200 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2018-06-08 12:22:08 +0200

Seen: 278 times

Last updated: Jun 12 '18