Python
  Home arrow Python arrow Page 4 - 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 
Moblin 
JMSL Numerical Library 
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 / 23
    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


    Python and OpenOffice.org - Tabular Talk


    (Page 4 of 4 )

    Tables are fairly easy to work with, so let's take a brief look at using them through Python-UNO. Before we do that, though, let's open up a new document:

    >>> document = desktop.loadComponentFromURL
    ("private:factory/swriter", "_blank", 0, ())
    >>> cursor = document.Text.createTextCursor()

    Now that we have a blank document with which to work, let's start on our table. We'll make a table of five countries that gives the population for each country. So, we'll have six rows, including a header row, and two columns. Here's how to create a table:

    >>> population = document.createInstance
    ("com.sun.star.text.TextTable")

    Of course, we now have to pass the number of rows and columns our table will have. This is done by passing the two values as arguments of the initialize method:

    >>> population.initialize(6,2)

    If you're looking at your document, you still won't see the table. Before it is visible, we have to insert it, which is similar to inserting text:

    >>> document.Text.insertTextContent(cursor, population, 0)

    We're now ready to being inserting text into our table. This involves referencing each cell by its name and then setting text. The first row is named "A", and the first column is named "1". This naming pattern continues to the end, and the cells are named by combining the row's name and the column's name. Let's insert our headers:

    >>> population.getCellByName("A1").setString("Country")
    >>> population.getCellByName("B1").setString("Population")

    Next, we can stick the country information in:

    >>> population.getCellByName("A2").setString("China")
    >>> population.getCellByName("B2").setString("1,306,313,812")
    >>> population.getCellByName("A3").setString("India")
    >>> population.getCellByName("B3").setString("1,080,264,388")
    >>> population.getCellByName("A4").setString("United States")
    >>> population.getCellByName("B4").setString("295,734,134")
    >>> population.getCellByName("A5").setString("Indonesia")
    >>> population.getCellByName("B5").setString("241,973,879")
    >>> population.getCellByName("A6").setString("Brazil")
    >>> population.getCellByName("B6").setString("186,112,794")

    Conclusion

    OpenOffice.org is quite a powerful tool, especially since it contains an API that can be used by several different languages. Fortunately, that list of languages includes Python, something made possible by Python-UNO. Python developers can import the library and create a local context component, which can be used to access OpenOffice.org's context component. Once connected, a developer can begin modifying a document. Although this article only looked at inserting text and tables and changing simple properties, OpenOffice.org's API does not stop there. Text and tables can be modified in many more ways that we looked at here, and we only looked at OpenOffice.org Writer, not any of the other applications included with the OpenOffice.org suite.

    There are plenty of places you can go from here. Try creating scripts that generate documents automatically, and take a look through the API if you need access to a feature but can't quite figure out how to go about accessing it:

    http://api.openoffice.org/docs/common/ref/com/sun/star/module-ix.html


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · 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