[Solved] Retrieve embedded Python script display in Writer

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
irs
Posts: 11
Joined: Fri Jul 26, 2013 1:37 pm

[Solved] Retrieve embedded Python script display in Writer

Post by irs »

With BASIC macro script embedded in a Writer document I am able to click on a button and retrieve all the script of a module and display it in the Writer document with this code...

Code: Select all

REM  *****  BASIC  *****

function Main()as string
	' Get the BASIC code
	if BasicLibraries.getByName("Standard").hasByName("Module1") then
		Main = BasicLibraries.getByName("Standard").getByName("Module1")
	end if
end function

sub message_button(button)
	doc = ThisComponent
	doc.getText().setString("Hello World! ~ Using BASIC" + _
	        chr(13) + chr(13) + Main())
end sub

sub clear_button(button)
	doc = ThisComponent
	doc.getText().setString("")
end sub
All good so far. Now, assuming I have python script embedded in a Writer document to handle the call-backs from a "message" and a "clear" button. How do I write the python code, such that when the "message" button is pressed it will get the Python script from Module.py and display it in the Writer document?

For example, in the following code, how would the Main() function be written to retrieve the python script...

Code: Select all

# coding: utf-8
from __future__ import unicode_literals

import uno
import sys 

def Main():
    """Get the Python code """
    # ...huh? how to do this?
    s = "The Python script should be returned here... \n\n"
    s += "FYI: Execute action for Message and Clear push buttons: \n"    
    s += "Module.py$message_button (document, Python) \n" 
    s += "Module.py$clear_button (document, Python) \n"   
    return s

def message_button(button):
    doc = XSCRIPTCONTEXT.getDocument()
    doc.getText().setString("Hello World! ~ Using Python: " +  sys.version.split(" ")[0] + 
            "\n\n" + Main())
    
def clear_button(button):
    doc = XSCRIPTCONTEXT.getDocument()
    doc.getText().setString("") 

g_exportedScripts = message_button, clear_button,
The above code is included in the Writer example documents: writer_basic_example.odt and writer_python_example.odt
Thanks, Ian.
Attachments
writer_python_example.odt
(11.38 KiB) Downloaded 181 times
writer_basic_example.odt
(13.5 KiB) Downloaded 195 times
Last edited by robleyd on Sat Jul 24, 2021 1:57 pm, edited 2 times in total.
Reason: Add green tick
OpenOffice 4.0 on ubuntu and winxp
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Retrieve embedded Python script and display in Writer do

Post by Villeroy »

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
irs
Posts: 11
Joined: Fri Jul 26, 2013 1:37 pm

Re: Retrieve embedded Python script and display in Writer do

Post by irs »

Villeroy, Thanks for recommending the Alternative Python Script Organizer (APSO). I'd describe it as essential for writing Python that is to be embedded into ODF files.

After a lot of experimenting I've found a way of being able to click on a push-button in a Writer document and the Python script being used is retrieved and displayed into the document. My Python method is not as elegant as the BASIC method, but it will be suffice to demo at an up-coming Python User Group meeting.

The following is the Python script that I embedded into my Writer document...

Code: Select all

#!/usr/bin/env python3
import uno, sys, os, zipimport

def get_python_script(button):
    url = button.value.Source.Model.Parent.Parent.Parent.URL
    url = url.split("//")[1] + "/Scripts/python/"
    importer = zipimport.zipimporter(url)
    return importer.get_source('Module')

def message_button(button):
    doc = XSCRIPTCONTEXT.getDocument()
    s = "Hello World! ~ Using Python: " +  sys.version.split(" ")[0]
    doc.getText().setString(s + "\n\n" + get_python_script(button))

def clear_button(button):
    doc = XSCRIPTCONTEXT.getDocument()
    doc.getText().setString("")

g_exportedScripts = message_button, clear_button,
This python code is in the attached Writer document: writer_python_example_v2.odt
cheers, Ian.
Attachments
writer_python_example_v2.odt
(13.72 KiB) Downloaded 186 times
OpenOffice 4.0 on ubuntu and winxp
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: [Solved] Retrieve embedded Python script display in Writ

Post by Villeroy »

You may be interested in my Python code installer which can also store any text or xml to the user profile. It saves the text document's body text in plain text format at a location that is specified by a custom field. The code is in plain sight, opened read-only and will be installed with a single click on a button.
How to prepare:
1) Open the template. It opens read-only. Turn on edit mode. Ignore any macro warning for now.
2) Double-click the custom field in the frame and specify the path relative to the user profile analog to the preset Scripts/python/MY_MODULE.py
3) Replace the place holder field with your code.
4) Save the new document to a trusted directory if you want to test the installer.

How to use
1) If the file was opened with a macro warning, save it to a trusted directory and call menu:File>Reload to enable macro execution in the trusted directory.
2) Push the install button and confirm the message box. If the file exists, there will be a warning.
Attachments
Script_Installer.ott
Writer template with script installer (embedded Basic)
(20.72 KiB) Downloaded 198 times
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