Forum at OOoForum.orgThe Forum
 [Home]   [FAQ]   [Search]   [Memberlist]   [Usergroups]   [Register
 [Profile]   [Log in to check your private messages]   [Log in

OOo python scripting !

Post new topic   Reply to topic Forum Index -> Macros and API
View previous topic :: View next topic  
Author Message
General User
General User

Joined: 13 Jun 2003
Posts: 7
Location: Texas

PostPosted: Fri Jun 13, 2003 6:10 am    Post subject: OOo python scripting ! Reply with quote


I actually got this little script working with jython 2.1 with OOo 1.1 beta 2.! For those who don't know jython is a special version of python that allows access to java libraries. It allows you to use the java api from a python script.. To get this to work you need to add the appropriate jars to the classpath (jjust like in the java examples).

Here is something similar to the first example in the developers guide. Some interesting notes when comparing this to the java equivilant is you will notice all the type declarations are gone (python does dynamic typing) . For some I think this (jython-OOo) might be a good alternative to OOo automation.In particular its good for smaller projects where you dont want to deal with the added complexities of java or c++ (dynamic typing, non-compiled, clean python syntax).

Any thoughts????


import java.lang
import jarray
import com
from import XUnoUrlResolver
from import UnoRuntime
from import XComponentContext
from import XMultiComponentFactory
from import XPropertySet

from import PropertyValue
from import XComponent
from import XSpreadsheetDocument
from import XSpreadsheets
from import XSpreadsheet
from import XSpreadsheetView
from import XCell
from import XModel
from import XController
from import XComponentLoader

xlocalcontext =
xlocalservicemanager = xlocalcontext.getServiceManager()

urlresolver = xlocalservicemanager.createInstanceWithContext("",xlocalcontext)

xunourlresolver = UnoRuntime.queryInterface(XUnoUrlResolver,urlresolver)
initialobject = xunourlresolver.resolve('uno:socket,host=localhost,port=8081;urp;StarOffice.SeerviceManager')
xpropertyset = UnoRuntime.queryInterface(XPropertySet,initialobject)
context = xpropertyset.getPropertyValue('DefaultContext')
xremotecontext = UnoRuntime.queryInterface(XComponentContext,context)
xrsm = xremotecontext.getServiceManager()


desktop = xrsm.createInstanceWithContext("",xremotecontext)
pbag = xrsm.createInstaceWithContext("",xremotecontext)

xcomponentloader = UnoRuntime.queryInterface(XComponentLoader,desktop)
loadprops = []

xspreadsheetcomponent = xcomponentloader.loadComponentFromURL("private:factory/scalc","_blank",0,loadprops)
xspreadsheetdocument = UnoRuntime.queryInterface(XSpreadsheetDocument,xspreadsheetcomponent)
xspreadsheets = xspreadsheetdocument.getSheets()
sheet = xspreadsheets.getByName("mysheet")
xsheet = UnoRuntime.queryInterface(XSpreadsheet,sheet)

xcell = xsheet.getCellByPosition(0,0)

# do some more stuff.....
Back to top
View user's profile Send private message Visit poster's website AIM Address

Joined: 02 Apr 2003
Posts: 3991
Location: Lawrence, Kansas, USA

PostPosted: Fri Jun 13, 2003 8:19 am    Post subject: Reply with quote

> Any thoughts?

Yeah. First of all: very cold! This inspires me a lot!

Now on to, um, I mean brainstorming...

Suppose that a Java component could be written that included Jython. This component would allow Jython scripts to be run. Since the Jython component would be a component, it would be integrated into OOo.

As a component, I imagine that much of your initial setup code would disappear.

The Jython environment could probably be augmented over the standard Jython to include a number of additional extra global functions, kind of like StarBasic does. Things such as "ThisComponent", "CreateUnoService()", "StarDesktop", etc.

Now it would be trivially easy for Python programmers to script OOo. It would be much more like StarBasic than like Java. You wouldn't get a remote service manager. You could just start at, say the global StarDesktop variable. Or the global CreateUnoService() function.

I guess I've really got to learn how to get an OOo component written, installed, and registered. Why oh why is this so difficult? Maybe second project is to write one universal component that can run arbitrary other Java jar files, to make it easier for other Java programmers to get stuff integrated inside of OOo with far fewer hoops to jump through. Make such a universal java component create a new folder under OOo/share/JavaComponents, such that ANY jar files placed there automatically are queried and their menus added to the OOo menubar, toolbar, etc. upon startup. Simply removing a jar file from the OOo/share/JavaComponents folder would remove them from the installation. Simple. No registering/unregistering once the universal java component itself is registered.

Now I'm really dreaming, but just imagine if such a hypothetical component (my earlier hypotheteical Jython component) could be included in the standard distribution of OOo, and if python scripts could be attached to documents.

All I want for Christmas 2003 is for OOo to be able to embed SOME kind of real language directly into documents. And those documents be usable on any standard OOo without having to get users to jump through all kinds of hoops to install some scripting framework, or register a component or other nonsense.

And no, StarBasic is not a real language. At least, not as is. No disrespect to Basic in general. But my god, StarBasic doesn't even have recursion! I can't even easily draw fractals in Draw. Let alone offering some trivial syntax to define new classes, or create data structures.

Here is my proposed bastardized Basic syntax for creating classes (borrowed from Visual FoxPro).....


Define Class Foobar As Custom
    propertyX = "zigzag"
    Sub SomethingOrOther( a As Long, b As Boolean )
        This.propertyX = "zugzog"
    End Sub
End Define

oFoo = CreateObject( "Foobar" )
oFoo.SomethingOrOther( 4, TRUE )

But I should stop complaining about StarBasic, and be glad I have that much.
After all, I'm really into OOo, and am greatful to have it even in its current form.

Back to your success. I have dabbled in Python. I'll really have to try Jython and your technique.
Back to top
View user's profile Send private message
General User
General User

Joined: 07 Jun 2003
Posts: 26
Location: Germany

PostPosted: Fri Jun 13, 2003 12:13 pm    Post subject: Reply with quote


Any thoughts????

This sounds great! There also exists a native python uno bridge, pyuno. It's available at


Suppose that a Java component could be written that included Jython. This component would allow Jython scripts to be run. Since the Jython component would be a component, it would be integrated into OOo.

Hm, that'd be something for the new scripting engine of the framework project. Then, integrating Jython scripts into the office would be possible executing/using java classes. Does jython run with suns 1.4.1_02 jre/jdk?

Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic Forum Index -> Macros and API All times are GMT - 8 Hours
Page 1 of 1

Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

Powered by phpBB © 2001, 2005 phpBB Group