From Apache OpenOffice Wiki
Revision as of 04:28, 9 June 2007 by Jza (Talk | contribs)

Jump to: navigation, search

This makes it easy and convenient to print a bunch of text into a Writer document.

See the example routine in the module that shows how easy this module is to use, and how useful it can be.


  1. If you make changes, please append to the change log below.
  2. Change Log
  3. Danny Brewer Revised 2004-06-05-01
  1. OOo's libraries

import uno

  1. Danny's libraries

from Danny.OOo.OOoLib import StarDesktop

com_sun_star_text_ControlCharacter_PARAGRAPH_BREAK = uno.getConstantByName( "" ) com_sun_star_text_ControlCharacter_LINE_BREAK = uno.getConstantByName( "" ) com_sun_star_text_ControlCharacter_HARD_HYPHEN = uno.getConstantByName( "" ) com_sun_star_text_ControlCharacter_SOFT_HYPHEN = uno.getConstantByName( "" ) com_sun_star_text_ControlCharacter_HARD_SPACE = uno.getConstantByName( "" ) com_sun_star_text_ControlCharacter_APPEND_PARAGRAPH = uno.getConstantByName( "" )

class PrintToWriter:

   """A class which allows conveniently printing stuff into a Writer document. 
   Very useful for debugging, general output purposes, or even for creating reports.""" 
   def __init__( self ): 
       self.oWriterDoc = StarDesktop.loadComponentFromURL( "private:factory/swriter", "_blank", 0, () ) 
       self.oWriterText = self.oWriterDoc.getText() 
       self.oWriterCursor = self.oWriterText.createTextCursor() 
   def writeLn( self, *args ): 
       if len( args ) > 0: 
           apply( self.write, args ) 
   def write( self, arg1, *argsRest ): 
       self.writeOne( arg1 ) 
       for arg in argsRest: 
           self.writeOne( arg ) 
   def writeOne( self, arg, bAbsorb=False ): 
       self.writeString( str( arg ), bAbsorb ) 
   def writeTab( self, bAbsorb=False ): 
       self.writeString( "\t", bAbsorb ) 
   def writeParagraphBreak( self, bAbsorb=False ): 
       self.writeControlCharacter( com_sun_star_text_ControlCharacter_PARAGRAPH_BREAK, bAbsorb ) 
   def writeString( self, cString, bAbsorb=False ): 
       self.oWriterText.insertString( self.oWriterCursor, cString, bAbsorb ) 
   def writeControlCharacter( self, nCtrlChar, bAbsorb=False ): 
       self.oWriterText.insertControlCharacter( self.oWriterCursor, nCtrlChar, bAbsorb ) 

def example_PrintToWriter():

   """An example of how to use the PrintToWriter class to trivially create 
   a new Writer document, and write out text into it.""" 
   oOutput = PrintToWriter() 
   oOutput.writeLn( "Hello World" ) 
   oOutput.write( "String", 123, 456.23, True ) 
   oOutput.writeLn( "Tab delimited values..." ) 
   oOutput.write( 123 ) 
   oOutput.write( 456 ) 
   oOutput.write( 789 ) 
   oOutput.write( 465 ) 
   oOutput.write( 522 ) 
   oOutput.write( 835 ) 
   oOutput.write( 886 ) 
   oOutput.write( 164 ) 
   oOutput.write( 741 ) 

How it Works code is really just a way to:

  • create the enviroment
  • create a document
  • generate the content
  • insert it within a document

So first we need to create the enviroment so that we don't generate a verbose code, we need to insert it into variable.

To create the enviroment we need a couple of things, first we import a library, which already save us a bunch of code in order to reference to UNO. The next step is to choose the fomat element we will need to input. For tha we use the uno.getConstantByName and point to the ControlCharacter component which includes all the format we need.

  • Paragraph Break - Insert a paragraph break
  • Line Break - Inserts a line break inside of the paragraph
  • Hard Hyphen - A character that appears like a dash, but prevents hyphenation at its position
  • Soft Hyphen - Marks a preferred position for hyphenation
  • Hard Space - A character that appears like a space, but prevents hyphenation at this point
  • Append Paragraph - A new paragraph is appended

The next step is to generate the class for PrintToWriter, this is the class that this file is about. Then we need to load the writer component, on like 59. We use the getText() function so we can access the text area from the writer's component. Once we are there, we explicity activate the Cursor.

We go on using the following functions:

  • writeLn = Insert a break on end of Line
  • write =
  • writeOne = Write String
  • writeTab = Insert a \t value as a string
  • writeParagraphBreak = we call on the PARAGRAPH_BREAK component
  • writeString = We use cString after the Cursor
  • writeControlCharacter = nCtrlChar

The example_PrinterToWrite is basicaly we execute the funcitons we create under the PrintToWriter class. This will be enough for you to see how we can use the functions to insert content and how it works the different funmction.

Personal tools