[Solved] Execute a Python Program From Base

Keyboard macros or custom scripts

[Solved] Execute a Python Program From Base

Postby Steve R. » Sun Oct 12, 2014 8:04 pm

It appears that I have two issues, one with the Shell command. The shell command reports that the file does not exist.
I inserted "FileExists" function as a second means of verifying the existence of the file and it correctly identifies that the file exists.
However, the shell command says that the file does not exist.

The second issue, is the shell command the correct way to execute a python script from Base?

Code: Select all   Expand viewCollapse view
sub RunPythonScript
   rem On Error Goto HandleError92
   rem shell("/home/steve/MP3_Copy_Project/USBFreeSpace.sh")   
   If FileExists("/usr/lib/libreoffice/share/Scripts/python/TestPythonScript.py") Then
           MsgBox "file exists."
        else
           MsgBox "file does NOT exist."
      End If
   [color=#BF0080]shell("/usr/lib/libreoffice/share/Scripts/python/TestPythonScript.py")[/color]   
   exit sub
HandleError92:
   Print "Handle Error #92"
   resume next
end sub
Last edited by Steve R. on Mon Oct 20, 2014 2:43 pm, edited 1 time in total.
Ubuntu 16.04 and Windows 10
User avatar
Steve R.
 
Posts: 162
Joined: Mon Sep 21, 2009 12:06 am
Location: Morehead City, North Carolina

Re: Execute a Python Program From Base

Postby Villeroy » Sun Oct 12, 2014 9:05 pm

The Basic code is unrelated to the office suite; and I guess so is your Python program. Why not use a desktop link to start your program like you would start any other program?
The reason why the file does not exist is:
/usr/lib/libreoffice/share/Scripts/python/TestPythonScript.py may be executable on a Linux system with a shebang in the first line and with the executable flag on the script file. Windows can not execute any scripts this way. Check out on the command line if your program is callable.
ls -l /usr/path/name.py
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: 29483
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Execute a Python Program From Base

Postby Steve R. » Sun Oct 12, 2014 10:33 pm

A added "#!/usr/bin/env python -c" and the file not found error message went away. However, the string "Hello World" does not pop-up.
Code: Select all   Expand viewCollapse view
#!/usr/bin/env python -c
localContext = uno.getComponentContext()
resolver = localContext.ServiceManager.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver", localContext )
ctx = resolver.resolve( "uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext" )
smgr = ctx.ServiceManager
desktop = smgr.createInstanceWithContext( "com.sun.star.frame.Desktop",ctx)
model = desktop.getCurrentComponent()
text = model.Text
cursor = text.createTextCursor()
text.insertString( cursor, "Hello World", 0 )
ctx.ServiceManager

I am very confused as to where this code would go.
This code was copied from PyUNO bridge which does not use the line: "#!/usr/bin/env python -c"
The PyUNO bridge webpage notes: "Inside the OpenOffice.org process within the scripting framework (OOo 2.0 and later only !!)

Currently the file is located here->"/usr/lib/libreoffice/share/Scripts/python/TestPythonScript.py"
It is executed by:
Code: Select all   Expand viewCollapse view
sub RunPythonScript
   rem On Error Goto HandleError92   
   shell("/usr/lib/libreoffice/share/Scripts/python/TestPythonScript.py")
   exit sub
HandleError92:
   Print "Handle Error #92"
   resume next
end sub


By any chance should the subroutine (in Base) be:
Code: Select all   Expand viewCollapse view
sub RunPythonScript
      rem On Error Goto HandleError92   
      localContext = uno.getComponentContext()
      resolver = localContext.ServiceManager.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver", localContext )
      ctx = resolver.resolve( "uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext" )
      smgr = ctx.ServiceManager
      desktop = smgr.createInstanceWithContext( "com.sun.star.frame.Desktop",ctx)
      model = desktop.getCurrentComponent()
      text = model.Text
      cursor = text.createTextCursor()
      text.insertString( cursor, "Hello World", 0 )
      ctx.ServiceManager
      exit sub
HandleError92:
   Print "Handle Error #92"
   resume next
end sub

I tried that once, but received a lot of error messages about things not being set.
Ubuntu 16.04 and Windows 10
User avatar
Steve R.
 
Posts: 162
Joined: Mon Sep 21, 2009 12:06 am
Location: Morehead City, North Carolina

Re: Execute a Python Program From Base

Postby karolus » Mon Oct 13, 2014 7:57 am

Hallo

Steve R. wrote:The PyUNO bridge webpage notes: "Inside the OpenOffice.org process within the scripting framework (OOo 2.0 and later only !!)

you are wrong! the statement for this example follows in the next line:
PyUNO bridge wrote:Inside the Python executable (and outside the OOo process)


This Script is designed to connect to a running office-process from outside and to write "Hello World" into the Current Document which has to be a Writer-doc

It make absolutly no Sense to execute it via Shell-command from inside the running office process addidionally from a component which is not Writer.
AOO4, Libreoffice 6.1 on Rasbian OS (on ARM)
User avatar
karolus
Volunteer
 
Posts: 856
Joined: Sat Jul 02, 2011 9:47 am

Re: Execute a Python Program From Base

Postby Steve R. » Mon Oct 13, 2014 1:14 pm

Thanks for that clarification. So how does one get Python and Base to work together?
Ubuntu 16.04 and Windows 10
User avatar
Steve R.
 
Posts: 162
Joined: Mon Sep 21, 2009 12:06 am
Location: Morehead City, North Carolina

Re: Execute a Python Program From Base

Postby karolus » Mon Oct 13, 2014 1:31 pm

Steve R. wrote:... So how does one get Python and Base to work together?


In general the same way as other office-components, - so you have to ask for the real task not for some `hello World` example.
AOO4, Libreoffice 6.1 on Rasbian OS (on ARM)
User avatar
karolus
Volunteer
 
Posts: 856
Joined: Sat Jul 02, 2011 9:47 am

Re: Execute a Python Program From Base

Postby Steve R. » Mon Oct 13, 2014 2:51 pm

Currently I am just experimenting with making a "connection" as a learning experience. A very simple program, such as "Hello" can be a valuable example from which to build future applications.

As previously stated, I am not familiar with Python so there is quite a bit of stuff I have to think about. Based on the responses so far, it would appear that Python can't be run directly from within Base, but that Python should be executed as an external program which connects to Base. If that is the case, I can stop exploring my current approach right now. If so that answers my question.
Ubuntu 16.04 and Windows 10
User avatar
Steve R.
 
Posts: 162
Joined: Mon Sep 21, 2009 12:06 am
Location: Morehead City, North Carolina

Re: Execute a Python Program From Base

Postby Villeroy » Mon Oct 13, 2014 5:03 pm

How to write a macro:
Code: Select all   Expand viewCollapse view
# HelloWorld python script for the scripting framework

def HelloWriter(*args):
    """Prints the string 'Hello World(in Python)' into the current document"""
#get the doc from the scripting context which is made available to all scripts
    model = XSCRIPTCONTEXT.getDocument()
#get the XText interface
    text = model.Text
    print text.getString()
#create an XTextRange at the end of the document
    tRange = text.End
#and set the string
    tRange.String = "Hello Writer (in Python) with args: "+ repr(args)
    return None

def HelloShell():
    print "Hello Shell"

Save this under ~/.openoffice/4/user/Script/python/HelloScript.py
Open a Writer document.
Get the "Form Controls" toolbar, turn on design mode (button #2) and draw a push button.
Assign the button's "Execute Action" event to MyMacros>HelloScript>HelloWriter, turn off design mode and push the button.
The macro appends this to the end of the document text:
Hello Writer (in Python) with args: ((com.sun.star.awt.ActionEvent){ (com.sun.star.lang.EventObject){ Source = (com.sun.star.uno.XInterface)0x7f2d46e5a7a0{implementationName=com.sun.star.comp.forms.OButtonControl, supportedServices={com.sun.star.awt.UnoControl,com.sun.star.awt.UnoControlButton,com.sun.star.form.control.SubmitButton,com.sun.star.form.control.CommandButton}, supportedInterfaces={com.sun.star.accessibility.XAccessible,com.sun.star.awt.XActionListener,com.sun.star.awt.XButton,com.sun.star.awt.XControl,com.sun.star.awt.XItemListener,com.sun.star.awt.XLayoutConstrains,com.sun.star.awt.XStyleSettingsSupplier,com.sun.star.awt.XToggleButton,com.sun.star.awt.XUnitConversion,com.sun.star.awt.XView,com.sun.star.awt.XWindow2,com.sun.star.beans.XPropertiesChangeListener,com.sun.star.beans.XPropertyChangeListener,com.sun.star.form.XApproveActionBroadcaster,com.sun.star.form.submission.XSubmission,com.sun.star.frame.XDispatchProviderInterception,com.sun.star.frame.XStatusListener,com.sun.star.lang.XComponent,com.sun.star.lang.XEventListener,com.sun.star.lang.XServiceInfo,com.sun.star.lang.XTypeProvider,com.sun.star.uno.XAggregation,com.sun.star.uno.XWeak,com.sun.star.util.XModeChangeBroadcaster}} }, ActionCommand = (string)"" },)

It prints Hello Writer (in Python) with args: followed by a textual representation of the calling object http://www.openoffice.org/api/docs/comm ... tener.html

Call the same macro without form control via Tools>Macros>Organize>Python... HelloScript>HelloWriter
It prints Hello Writer (in Python) with args: () () stands for an empty tuple.

Now call the same macro without form control via Tools>Macros>Organize>Python... HelloScript>HelloShell which prints nothing into the Writer document because Python's print command prints to a shell.

What can be seen as "the shell" when running a macro?
------------------------------------------------------------------------
Shutdown OpenOffice and restart it from a terminal (command openoffice or /path_to_openoffice/program/soffice)
Call from the welcome screen or from any document window Tools>Macros>Python...HelloScript>HelloShell
The output goes to the terminal from which you started the office suite. As I understand the matter, the office suite can be seen as an environment for macro code. Every macro is called from Tools>Macros..., from customized office menus, keyboard shortcuts, toolbar buttons, from office events, from form controls, various other objects supporting script events or even from a text hyperlink (vnd.sun.star.script:..., I forgot the syntax). The office passes any script output such as Python's print command to its parent shell. If this is a terminal, you see the output on that terminal. If the shell is a desktop environment, you see nothing. For most simple macro debugging you call the office from a terminal, start the macro from the office and use print commands to inspect things.
Last edited by Villeroy on Mon Oct 13, 2014 7:53 pm, edited 1 time in total.
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: 29483
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Execute a Python Program From Base

Postby Steve R. » Mon Oct 13, 2014 6:17 pm

Thanks for the follow-up. It will be a while before I can follow-up. While this is a learning experience, I may simply be "barking-up the wrong tree" on how to use Python.
Ubuntu 16.04 and Windows 10
User avatar
Steve R.
 
Posts: 162
Joined: Mon Sep 21, 2009 12:06 am
Location: Morehead City, North Carolina

Re: Execute a Python Program From Base

Postby Steve R. » Mon Oct 20, 2014 2:20 am

It appears that I may have a configuration issue of some sort. I am using LibreOffice 4.3.2.2. I have created two directories in the hope of this working. I placed your sample script in both locations.

One directory: /usr/lib/libreoffice/share/Scripts/python
The other: /home/steve/.config/libreoffice/4/user/Scripts/python

When I go to "Tools > Macros > Organize Macros > LibreOfficeBasic", I do not see any macros listed under either "LibreOffice Macros" or "MyMacros" for either Java or Python.
(LibreOffice provides some sample macros for both Java and Python, so they should be visible.)

Villeroy wrote:Now call the same macro without form control via Tools>Macros>Organize>Python...

With my current set-up, I don't see "Python" as an option.
I must be missing something??? Always an unexpected impediment.
Ubuntu 16.04 and Windows 10
User avatar
Steve R.
 
Posts: 162
Joined: Mon Sep 21, 2009 12:06 am
Location: Morehead City, North Carolina

Re: Execute a Python Program From Base

Postby karolus » Mon Oct 20, 2014 8:02 am

Hallo

You are on Ubuntu and use LibreOffice-packages from your Distribution?
In this case open the Packagemanager and search for `libreoffice-Script-provider-python` and install it.

Karolus
AOO4, Libreoffice 6.1 on Rasbian OS (on ARM)
User avatar
karolus
Volunteer
 
Posts: 856
Joined: Sat Jul 02, 2011 9:47 am

Re: Execute a Python Program From Base

Postby Steve R. » Mon Oct 20, 2014 2:19 pm

karolus wrote:You are on Ubuntu and use LibreOffice-packages from your Distribution?
In this case open the Packagemanager and search for `libreoffice-Script-provider-python` and install it.

Yes, your suggestion worked. I am using the Ubuntu release of LibreOffice. I used the Synaptic Program Manager to add the three available scripts (Python, Java, and Bean shell).
I am currently experimenting with the Python application. In some cases it works, in others it does not.
Thank-you.
Ubuntu 16.04 and Windows 10
User avatar
Steve R.
 
Posts: 162
Joined: Mon Sep 21, 2009 12:06 am
Location: Morehead City, North Carolina


Return to OpenOffice Basic, Python, BeanShell, JavaScript

Who is online

Users browsing this forum: No registered users and 2 guests