Can't run Python macro in LibreOffice
Question:
I’m trying to learn to write macros for LibreOffice in Python. I made simple macro, put in into ~/.config/libreoffice/4/user/Scripts/python/
and found it in Tools/Macros/Organize Macros/Python…/My Macros. So far all works as expected, macro is visible.
But when I click on it, the Run button stays grayed out. I cannot run it. Does anyone know what might be the cause?
Thanks
EDIT: File is executable, I set the rights to 777 just to be sure. Python code is valid.
EDIT2: I reinstalled LibreOffice and it works now. Probably was something wrong with my installation. Script file in ~/.config/libreoffice/4/user/Scripts/python/
set as executable works. And @ngulam examples worked even before reinstall.
Thanks for all the advice you’ve given me and have a good day.
Answers:
Macros for OpenOffice/LibreOffice in Python don’t have to be executable.
The location is right, though you might want to create a sub-directory (e.g. for CALC or WRITER), and put it in there – since otherwise it will be visible in all other components (where it might not work).
Did you restart LibreOffice after copying?
Note: For LO 5.2 you need to put your pythons here..
/opt/libreoffice5.2/share/Scripts/python
.. or better still place a link there into a folder where you can edit without sudo ie. issue a command like this..
cd /opt/libreoffice5.2/share/Scripts/python
sudo ln -s /home/rich/Sources/Pythons rje_pythons
where /home/rich/Sources/Pythons is your more convenient folder.
If you put your code inside a function named as the file, and returns something or None it should work.
Example: create inside Scripts folder, a file called MakeSomethingPython.py as the following, as you can see i’ve named the function MakeSomethingPython:
#import library or function you need
def MakeSomethingPython( ):
//code to make something
//
return
I had the same or similar problem. My script MyCalcHelloWorld.py with the function MyCalcHelloWorld resting in /home/tom/.config/libreoffice/4/user/Scripts/python/ created the library in the Run Macro dialog left pane, but not the script name to run in the right pane.
I have tried to copy one of the preinstalled system-wide python macro scripts from /opt/libreoffice7.5/share/Scripts/python/ to /home/tom/.config/libreoffice/4/user/Scripts/python/ and it showed fine.
In the end I realized, that my script had a syntax error in it (double quotes in a code copied from a web page were not simple double quotes, but UTF-8 characters for upper and lower double quotes) and that was the reason for the script not showing.
I’m trying to learn to write macros for LibreOffice in Python. I made simple macro, put in into ~/.config/libreoffice/4/user/Scripts/python/
and found it in Tools/Macros/Organize Macros/Python…/My Macros. So far all works as expected, macro is visible.
But when I click on it, the Run button stays grayed out. I cannot run it. Does anyone know what might be the cause?
Thanks
EDIT: File is executable, I set the rights to 777 just to be sure. Python code is valid.
EDIT2: I reinstalled LibreOffice and it works now. Probably was something wrong with my installation. Script file in ~/.config/libreoffice/4/user/Scripts/python/
set as executable works. And @ngulam examples worked even before reinstall.
Thanks for all the advice you’ve given me and have a good day.
Macros for OpenOffice/LibreOffice in Python don’t have to be executable.
The location is right, though you might want to create a sub-directory (e.g. for CALC or WRITER), and put it in there – since otherwise it will be visible in all other components (where it might not work).
Did you restart LibreOffice after copying?
Note: For LO 5.2 you need to put your pythons here..
/opt/libreoffice5.2/share/Scripts/python
.. or better still place a link there into a folder where you can edit without sudo ie. issue a command like this..
cd /opt/libreoffice5.2/share/Scripts/python
sudo ln -s /home/rich/Sources/Pythons rje_pythons
where /home/rich/Sources/Pythons is your more convenient folder.
If you put your code inside a function named as the file, and returns something or None it should work.
Example: create inside Scripts folder, a file called MakeSomethingPython.py as the following, as you can see i’ve named the function MakeSomethingPython:
#import library or function you need
def MakeSomethingPython( ):
//code to make something
//
return
I had the same or similar problem. My script MyCalcHelloWorld.py with the function MyCalcHelloWorld resting in /home/tom/.config/libreoffice/4/user/Scripts/python/ created the library in the Run Macro dialog left pane, but not the script name to run in the right pane.
I have tried to copy one of the preinstalled system-wide python macro scripts from /opt/libreoffice7.5/share/Scripts/python/ to /home/tom/.config/libreoffice/4/user/Scripts/python/ and it showed fine.
In the end I realized, that my script had a syntax error in it (double quotes in a code copied from a web page were not simple double quotes, but UTF-8 characters for upper and lower double quotes) and that was the reason for the script not showing.