[Python] Add a menu

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
Emaïks
Posts: 4
Joined: Wed Jan 03, 2018 1:06 pm

[Python] Add a menu

Post by Emaïks »

Hello everyone.
I'm trying to add a menu with a Python macro but I can't find anything (I must be bad at searching).
I want to add a menu next to the menu "Help" with a custom name, and them map submenus with python macros.
I found how to do it with Basic but I can't read nor stand this language.

Thanks you for your help !!
LibreOffice 5.4.3.2 (x64) on Windows 10
User avatar
RoryOF
Moderator
Posts: 34586
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: [PYTHON] Add a menu

Post by RoryOF »

This topic and the link from it may be of assistance
viewtopic.php?t=70633
Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
Emaïks
Posts: 4
Joined: Wed Jan 03, 2018 1:06 pm

Re: [PYTHON] Add a menu

Post by Emaïks »

Thanks for your answer.
The link you provide me is about extension, but i'm currently working on macros.

Do you think I should give up what i'm doing and start making an extension then?

Thanks again for your help! :)
LibreOffice 5.4.3.2 (x64) on Windows 10
User avatar
Villeroy
Volunteer
Posts: 31269
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: [PYTHON] Add a menu

Post by Villeroy »

If your macro refers to one specific document or template, you can wrap everything, the menu and the macro in that document or template. Use Tools>Customize>Menu and embed the Python macro.

If your macro is a global one, create an extension with your Python macro and the menu definition.
https://wiki.openoffice.org/wiki/Extens ... n_Compiler can do most of the work.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
Emaïks
Posts: 4
Joined: Wed Jan 03, 2018 1:06 pm

Re: [PYTHON] Add a menu

Post by Emaïks »

Thanks for your answer.
What do you mean by wrapping ? Saving as an .ods or .ots file?

My macro refers something like a template.
As I will run my macros on specific documents generated by other persons.

Is it possible to load customizations made from Tool > Customize (Like new menus, ...) and macros into an existing ods file ?

Thanks again for your help
LibreOffice 5.4.3.2 (x64) on Windows 10
User avatar
Villeroy
Volunteer
Posts: 31269
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: [PYTHON] Add a menu

Post by Villeroy »

There are two kinds of macros in either language: the public ones and the ones that are embedded in documents and always refer to a specific document with its specificly named objects, bookmarks, settings etc. This may save hundreds of lines of code. Basic's ThisComponent or Python's XSCRIPTCONTEXT.getDocument() always refers to its own embedding document.
Because there is no integrated Python IDE like the one for Basic, you typically develop your Python macro as global code in the <user_profile>/Scripts/python/ directory. Finally you integrate Scripts/python/Your_Module.py in the zip archive. ODF documents are zipped XML and register the code and the paths in META.INF/manifest.xml as recently described here: viewtopic.php?f=20&t=91642&p=434434#p434305

APSO is an extension which makes it a little bit easier to maintain global and embedded Python macros.
https://extensions.libreoffice.org/exte ... thon/1-0.0
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
User avatar
Villeroy
Volunteer
Posts: 31269
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: [PYTHON] Add a menu

Post by Villeroy »

I forgot: The above linked Extension Compiler is an excellent example for a sophisticated Writer template with embedded Basic code. You store a new document from this template in your development directory, fill out some parts of the document and the Basic code does the right thing for this document in this directory.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
Post Reply