SunQuest
 
       Python
  Home arrow Python arrow Page 2 - Python and OpenOffice.org
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
VeriSign Whitepapers 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PYTHON

Python and OpenOffice.org
By: Peyton McCullough
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 22
    2006-03-21

    Table of Contents:
  • Python and OpenOffice.org
  • Connecting to OpenOffice.org
  • Getting to the Desktop Service
  • Tabular Talk

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Python and OpenOffice.org - Connecting to OpenOffice.org


    (Page 2 of 4 )

    We'll be connecting with OpenOffice.org through a separate process for now, since it's an easy place to begin. Before we do this, though, OpenOffice.org has to be listening for us. This involves passing a string to it when starting it up. Make sure that OpenOffice.org is completely closed (this includes closing OpenOffice.org from the system tray in Windows), and then open a console and change to the directory where OpenOffice.org is installed. Once there, change to the program directory. Now we'll open the OpenOffice.org Writer, passing a parameter that will allow OpenOffice.org to listen for a connection from us:

    $ ./swriter -accept="socket,host=localhost,port=2002;urp;"

    OpenOffice.org should open without any visible differences. The string we passed creates an UNO acceptor, which will listen for us to connect via Python-UNO and access the OpenOffice.org API. Open  the program directory in a new window, then find OpenOffice.org's version of Python and execute it to get into the interactive interpreter. Alternatively, if you know that your Linux distribution makes Python-UNO available in its default Python installation, simply open Python as usual.

    The obvious first step here is to import the Python-UNO module, named uno:

    >>> import uno

    The first thing we must do is import a local component context. If you want an in-depth technical explanation of exactly what a component context is and what it does, you can find several out there. However, we'll shorten things up a bit in explaining how things work. A context manager consists of a service manager and associated objects. Think of it as the context in which a service manager exists, as the name suggests.

    Now we're left with defining what a service manager is. A service manager manages services. Again, its name fits a simplified definition of its function. Finally, a service is simply an object that is assigned a specific set of tasks. For example, one particular service is responsible for loading and managing documents. Another can be used to gain access to OpenOffice.org's configuration. Going backward here, a service is managed by a service manager, which exists in a component context. Of course, there is more to it, but the extra knowledge isn't necessary right now.

    With component contexts, service managers and services explained briefly, let's go ahead and create a local component context, which we can later use to access OpenOffice.org's component context:

    >>> local = uno.getComponentContext()

    Now we have a local component context. Next, we have to access the UnoUrlResolver service. This will be used to connect to OpenOffice.org. To do this, we have to make use of the createInstanceWithContext method of the service manager, which may be found at local.ServiceManager. We will pass the name of the service we want, as well as the context, as arguments:

    >>> resolver = local.ServiceManager.createInstanceWithContext
    ("com.sun.star.bridge.UnoUrlResolver", local)

    We are now able to connect to OpenOffice.org. This is quite simple, and it only involves calling a method and passing a modified version of the string we passed to the OpenOffice.org Writer when we started it. After we do this, we'll have access to OpenOffice.org's component context, and we'll be able to manipulate OpenOffice.org through its API:

    >>> context = resolver.resolve
    ("uno:socket,host=localhost,port=2002;urp;StarOffice.
    ComponentContext")

    The string we pass as an argument is only slightly different from the one used when starting up OpenOffice.org. All we do is append "StarOffice.ComponentContext" to it to request OpenOffice.org's component context, which we store in context. We also add the protocol to the front of the string. We're now connected to OpenOffice.org.

    More Python Articles
    More By Peyton McCullough


       · This is a great article to help someone become an OpenOffice.org power user. I found...
       · Chris,Thanks for pointing that out to me. It looks as if the backslashes were...
       · Peyton,Thanks for your helpful article. I am exploring the Python/OOo interface...
       · Well.. I have been diving in the web for 2 hours and i have realized the...
     

       

    PYTHON ARTICLES

    - SSH with Twisted
    - Mobile Programming in Python using PyS60: UI...
    - Python: Count on It
    - Python Strings: Spinning Yarns
    - Python: More Fun with Strings
    - Python: Stringing You Along
    - Python Operators
    - Bluetooth Programming in Python: Network Pro...
    - Python Sets
    - Python Conditionals, Lists, Dictionaries, an...
    - Python: Input and Variables
    - Introduction to Python Programming
    - Mobile Programming in Python using PyS60: Ge...
    - Bluetooth Programming using Python
    - Finishing the PyMailGUI Client: User Help To...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway