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  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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? 
Google.com  
PYTHON

Python and OpenOffice.org
By: Peyton McCullough
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 27
    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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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


    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
     

       

    PYTHON ARTICLES

    - Tuples and Other Python Object Types
    - The Dictionary Python Object Type
    - String and List Python Object Types
    - Introducing Python Object Types
    - Mobile Programming using PyS60: Advanced UI ...
    - Nested Functions in Python
    - Python Parameters, Functions and Arguments
    - Python Statements and Functions
    - Statements and Iterators in Python
    - Sequences and Sets in Python
    - Python Expressions and Operators
    - Dictionaries, Variables and Statements in Py...
    - Data Types in Python
    - The Python Language
    - SSH with Twisted





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek