Basic Noobie Python Problem - who took my script! :)

def Hello_there_2Python():
# get the doc from the scripting context which is made available to all scripts
desktop = XSCRIPTCONTEXT.getDesktop()
model = desktop.getCurrentComponent()

# access the active sheet
active_sheet = model.CurrentController.ActiveSheet

# write 'Hello World' in A1
cell = active_sheet.getCellRangeByName("A1")
cell.String = 'Hello World!'

Apologies I am totally new to Python. I use Ubuntu 22.04 as my OS. I have been using LO for many years but never tried running scripts or coding with it.

Py_not_showing_in_LO.pdf (77.6 KB)

Attached you can see that my script is in the file that contains all the Python scrips in LO. I have changed the permission in case that was the problem…but it still does not appear in the LO Calc Macro folder.

Please where am I going wrong?

    doc = XSCRIPTCONTEXT.getDocument()
    active_sheet = doc.CurrentController.ActiveSheet

    # write 'Hello World' in A1
    cell = active_sheet["A1"]
    cell.String = 'Hello World!'

https://wiki.documentfoundation.org/Macros/Python_Guide

Thanks for that I will give it a try…though I suspect it is not the only problem I have. When I go Tools>Macros>Organise Macros and click on the Python scripts, the box that appears will not allow any interaction with the scripts…I cannot run, edit etc. Why might that be?

read you the link?

Ooops! sorry, missed the link…was focusing too much on the code! Thank you!

Sorry, I am being very dense here…I do not see in the link any reference to my script not showing up in Tools > Macros > Organise Macros > Python > MyMacros…I seem to be missing a link in my understanding!!

You probably have a syntax error, show your code, exactly as you do.

Install you this file in your OS?

libreoffice-script-provider-python

image

QStandardPaths: XDG_RUNTIME_DIR not set, defaulting to '/tmp/runtime-root'
20-07-2023 14:01:34:  saved file: /usr/lib/libreoffice/share/Scripts/python/Hello_there_2.py (LF)
Stopped our command server.
Request: stopserver
Reply: Stopped the server
Stopped

So changed the file as you suggested and saved it…also ran

before I did all that. Closed and opened LO…still no sign of my script! I am totally baffled.

def Hello_there_2Python():
# get the doc from the scripting context which is made available to all scripts
desktop = XSCRIPTCONTEXT.getDesktop()
model = desktop.getCurrentComponent()

# access the active sheet
active_sheet = model.CurrentController.ActiveSheet

doc = XSCRIPTCONTEXT.getDocument()
active_sheet = doc.CurrentController.ActiveSheet

# write 'Hello World' in A1
cell = active_sheet.getCellRangeByName("A1")
cell.String = 'Hello World!'

What is HelloWorld in your screenshot?

That is a standard stock LO script…comes pre-installed.


The above is the source of my confusion…I was unaware that there were 2 different places to store the Python Macros. So, now I have stored the same Macro in both places…and still I cannot find them with Macros>Organise Macros>Python…surely things are not meant to be this difficult…or am I just being exceptionally dense??!

Your original question with pdf shows your scripts in /usr/lib/… and you told you are using Ubuntu.
.
As Ubuntu is using Snap now, the container may not be allowed to access /usr/lib (actually the container will then usually provide a replacement /usr/lib ). However it should “see” the files below $HOME, but one would have to check the restrictiond of the container.
.

You have 3 independent players here, TDF providing LibreOffice with integrated (reduced) python, Linux maintainers deciding to use pre-installed python to run LibreOffice-scripts and Ubuntu prefering to put some software in containers, wich may pose problems when you come to scripting).

This is no command you type to the shell, but a software package to be installed via apt or better snapstore. I guess apt can not place it inside a snap, if it is not available.

~$ libreoffice-script-provider-python
libreoffice-script-provider-python: command not found
$ apt-get libreoffice-script-provider-python
E: Invalid operation libreoffice-script-provider-python

I guess you are correct!

~$ set apt-get libreoffice-script-provider-python

That seemed to work as it got no response!

sudo apt-get install libreoffice-script-provider-python

Thanks but sudo did not work either. I have abandoned this project for the time being. Thank you for your help.