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

    Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here!

    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


       · 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 5 hosted by Hostway