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  
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 - 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



     
     
    >>> 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 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek