Python
  Home arrow Python arrow Page 3 - 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 - Getting to the Desktop Service
    ( Page 3 of 4 )

    Now that we have access to OpenOffice.org's API, it's time to manipulate things a bit. Earlier, I mentioned that there was a service responsible for the current document. This service can be access by the name "com.sun.star.frame.Desktop". Using it, let's play around with the OpenOffice.org Writer document that was created when we started OpenOffice.org. First, we'll have to get the service using the aforementioned createInstanceWithContext method and passing the name of the service and the context:

    >>> desktop = context.ServiceManager.createInstanceWithContext
    ("com.sun.star.frame.Desktop", context)

    Good, now we have access to the Desktop service, and the power to load documents and manage the documents that we have loaded. Let's start off by getting the current document, which we can modify. This is done by calling the getCurrentComponent method:

    >>> document = desktop.getCurrentComponent()

    Before we do anything interesting with our document, let's create a cursor with which to actually do work. This is quite simple:

    >>> cursor = document.Text.createTextCursor()

    Using our cursor, we are able to add some content to our document. Let's start off by adding a simple string of text:

    >>> document.Text.insertString(cursor, "Adding text is very
    simple.", 0)

    Upon execution of the above line, you should see a line of text appear in your document. To add a linebreak to the document, simply add in the linebreak character:

    >>> document.Text.insertString(cursor, "\n\nThis is a new
    paragraph.", 0)

    Simple indentation is also fairly easy. It only involves adding an indentation ("\t") character:

    >>> document.Text.insertString(cursor, "\n\n\tAnd this is another
    new paragraph.", 0)

    Of course, adding text over and over again isn't that helpful, which is why we are allowed to manipulate the cursor. For example, let's say we wanted a bigger font size. This can be easily done by changing a property of our cursor. UNO objects contain properties, which can be changed by a simple method call. Before we change the font size of our cursor, let's get the current font size:

    >>> cursor.getPropertyValue("CharHeight")
    12.0

    As you can see, the property for font size is called "CharHeight." Using this name, we can change the value of the property by calling another method. Let's change the font size to twenty:

    >>> cursor.setPropertyValue("CharHeight", 20)

    Our font size has now been changed, and we are free to add some text in the new size:

    >>> document.Text.insertString(cursor, "\n\nThis is in size 20.",
    0)

    We're not limited to changing the text size. We can change just about anything. Let's change the font face and the font weight:

    >>> cursor.setPropertyValue("CharFontName", "Arial")
    >>> cursor.setPropertyValue("CharWeight", 150)
    >>> document.Text.insertString(cursor, "\n\nThis is bold Arial.",
    0)



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