Detailed tutorial regarding LibreOffice to Python macro writing, especially for Calc

@mikekaganski

X-ray and AltSearch are macro libraries. We can distribute macro libraries by means of extension packages. Such packages install macro code without adding new UNO services.

Interesting, I wasn’t aware of this distinction. Let me see if I understand what you are saying. For example, my extension (primarily written in python) contains lines such as the following.

class InterlinSettingsJob(JobWrapper):
    def __init__(self, ctx):
        JobWrapper.__init__(self, ctx)

    def showDialog(self):
        from lingt.ui.comp.interlinsettings import showDlg
        showDlg(self.ctx)

g_ImplementationHelper.addImplementation(
    InterlinSettingsJob,
    "name.JimK.LinguisticTools.InterlinSettings",
    ("com.sun.star.task.Job",),)

This is referenced in my Addons.xcu for a menu entry:

<value>service:name.JimK.LinguisticTools.InterlinSettings?execute</value>

Is that the kind of service you were talking about, and are you saying that this wouldn’t work for Basic?

My extension does have some Basic code as well but it gets called differently. Looking back through the code, one way that Basic gets called is by using macro:/// syntax with the dispatcher. Another way is by creating buttons dynamically that are assigned to Basic macros. In both cases, the Basic code is stored in a library in the extension, not given an implementation or service name or anything.

Yes, exactly. And no, we can’t write such services in StarBasic. There are quite powerful StarBasic extensions, most prominently XRay. They consist of macro code that is visible in the macro organizer and some UI elements calling some entry routines. Your Python class will not appear in any macro library. It constitutes an UNO service which is callable as a “Job”.
Related topic: Create APSO Toolbar Icon - #8 by flywire

Makes sense. That’s why calling Xray looks different from MRI. Again from my extension:

mspf = unoObjs.smgr.createInstanceWithContext(
    "com.sun.star.script.provider.MasterScriptProviderFactory",
    unoObjs.ctx)
scriptPro = mspf.createScriptProvider("")
try:
    xScript = scriptPro.getScript(
        "vnd.sun.star.script:XrayTool._Main.Xray?" +
        "language=Basic&location=application")
except:
    raise RuntimeException(
        "\nBasic library Xray is not installed", unoObjs.ctx)
xScript.invoke((myObject,), (), ())

...

mri_obj = unoObjs.ctx.ServiceManager.createInstanceWithContext(
    "mytools.Mri", unoObjs.ctx)
mri_obj.inspect(target)

I have started the Python tutorial series for Macros. Here’s a simple and detailed guide on how to start your Python macro journey (both Linux and Windows):

You don’t need to install Python on Windows in or der to run or write macros. LO comes with its own Python runtime C:\Program Files\LibreOffice\program\python.exe which is installed by default unless you have chosen otherwise in a custom installation.

AFAIK, there’s no way to avoid Python installation on Windows, even using custom option…

but I think you refer to the install of the python LibreOffice includes in its own installation. @Villeroy refers to the sentence in the tutorial

Windows users need to install Python in their system and LibreOffice as well.

and installing a second “external” python-interpreter is not necessary to run macros in LibreOffice.
.
In a first steps guide it is simply wrong without explanation, as it will provoke the situation where one complains of modules not found in LibreOffice-pythons after installing them in the other version…

I am not convinced that I misinterpreted the phrase :wink: