2

I am trying to organize my python project that currently only adds a menu to a libreoffice calc menu bar:

# this file is called AddMenu.py which is located in 'C:\Program Files\LibreOffice\share\Scripts\python'

def create_menu(args=None):
   o_doc = CreateScriptService("Document")
   o_menu = o_doc.CreateMenu("Test Menu")
   o_menu.AddItem("About", command="About")
   o_menu.AddItem("Testing",
                 script="vnd.sun.star.script:AddMenu.py$item_b_listener?language=Python&location=user")

def item_b_listener(args):
    bas = CreateScriptService("Basic")
    s_args = args.split(",")
    msg = f"Menu name: {s_args[0]}\n"
    msg += f"Menu item: {s_args[1]}\n"
    msg += f"Item ID: {s_args[2]}\n"
    msg += f"Item status: {s_args[3]}"
    bas.MsgBox(msg)

The menu and buttons are added as expected, and About works fine. However, when I click on the "Test Menu" I get an error:

Library :   ScriptForge
Service :   Session
Method :    ExecutePythonScript
Arguments: [Scope], Script, arg0[, arg1] ...


A serious error has been detected in your code on argument : « Script ».

The requested Python script could not be located in the given libraries and modules.
« Scope » = user
« Script » = AddMenu.py$item_b_listener


THE EXECUTION IS CANCELLED.

Do you want to receive more information about the 'ExecutePythonScript' method ?

Any suggestions?

Follow up question: how do I run a python script when calc starts? Since the menu doesn't persist on restarting calc, I need to run the macro to reinstall it

2
  • "Follow-up question" — That's correct, it should be posted as a separate question. You could fix this by removing the second part and posting it as a different question, along with a description of what you have researched or tried so far. Commented Feb 3, 2023 at 13:53
  • I've created another thread for this followup stackoverflow.com/questions/75346270/… Commented Feb 4, 2023 at 15:46

3 Answers 3

2

&location=user

That indicates the LibreOffice user profile directory, for example C:\Users\(your username)\AppData\Roaming\LibreOffice\4\user\Scripts\python.

So either change that part to &location=application or move the script to the user profile directory.

https://help.libreoffice.org/latest/sq/text/sbasic/python/python_locations.html

Sign up to request clarification or add additional context in comments.

1 Comment

I changed the code to be &location=application, but that didn't work. I moved the coded to the user profile directory you suggested and it works fine. Thank you for your help!
1

It looks like the script is unable to locate the item_b_listener function. The script that is trying to call the function item_b_listener is located in a different location than the definition of the function. To resolve this issue, you need to make sure that the script is able to locate the function.

One way to do this is to create a separate Python module (file) containing the item_b_listener function, and then import that module into AddMenu.py.

Regarding your follow-up question, you can run a python script when LibreOffice Calc starts by adding it to the Calc startup macro. To do this, follow these steps:

Go to the Tools menu and select Macros > Organize Macros > Python. In the Python macro dialog, select your script and click on the Edit button. In the Python macro editor, add the following code to the global section of the script:

def Main():
# your script code here

Save and close the macro editor. In the Python macro dialog, click on the Run button to test your script. To make sure your script runs every time Calc starts, go to Tools > Options > LibreOffice > Advanced and check the box for "Run macro from an autoexec section." Save the options and restart Calc. Now, your script should run every time Calc starts.

3 Comments

Thanks for the responses. item_b_listener is in the same file as create_menu (AddMenu.py). It should be being registered as a callback for clicking on the menu item "Testing" and called when I click on it.
I've moved this discussion to stackoverflow.com/questions/75346270/…
"In the Python macro dialog, select your script and click on the Edit button" — What version and platform are you using? In all the versions I have used, including the new LibreOffice 7.5 on Windows, the Edit button is disabled. APSO offers a menu at Tools > Macros > Organize python scripts, but that is different from what is written in your answer. Also, under Tools > Options > LibreOffice > Advanced, there is no "Run macro from an autoexec section," nor do I see any mention of this from searching online.
0

sudo apt install python3-scriptforge

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.