Difference between revisions of "PrinttoWriter.py"

From Apache OpenOffice Wiki
Jump to: navigation, search
(Explanation)
(How it Works)
Line 130: Line 130:
 
== How it Works ==
 
== How it Works ==
 
PrintToWriter.py code is really just a way to:
 
PrintToWriter.py code is really just a way to:
 +
* create the enviroment
 
* create a document
 
* create a document
 
* generate the content
 
* generate the content
Line 135: Line 136:
  
 
So first we need to create the '''enviroment''' so that we don't generate a verbose code, we need to insert it into variable.
 
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 [http://api.openoffice.org/docs/common/ref/com/sun/star/text/ControlCharacter.html ControlCharacter] component which includes all the format we need.
 +
 +
* Paragraph Break
 +
* Line Break
 +
* Hard Hyphen
 +
* Soft Hyphen
 +
* Hard Space
 +
* Append Paragraph

Revision as of 21:29, 17 January 2007

Danny.OOo.PrintToWriter.py

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.

[python]

  1. Danny.OOo.PrintToWriter.py
  2. This makes it easy and convenient to print a bunch of text into
  3. a Writer document.
  4. Copyright (c) 2003-2004 Danny Brewer
  5. d29583@groovegarden.com
  6. This library is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU Lesser General Public
  8. License as published by the Free Software Foundation; either
  9. version 2.1 of the License, or (at your option) any later version.
  10. This library is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. Lesser General Public License for more details.
  14. You should have received a copy of the GNU Lesser General Public
  15. License along with this library; if not, write to the Free Software
  16. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. See: http://www.gnu.org/licenses/lgpl.html
  18. If you make changes, please append to the change log below.
  19. Change Log
  20. 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.PARAGRAPH_BREAK" ) com_sun_star_text_ControlCharacter_LINE_BREAK = uno.getConstantByName( "com.sun.star.text.ControlCharacter.LINE_BREAK" ) com_sun_star_text_ControlCharacter_HARD_HYPHEN = uno.getConstantByName( "com.sun.star.text.ControlCharacter.HARD_HYPHEN" ) com_sun_star_text_ControlCharacter_SOFT_HYPHEN = uno.getConstantByName( "com.sun.star.text.ControlCharacter.SOFT_HYPHEN" ) com_sun_star_text_ControlCharacter_HARD_SPACE = uno.getConstantByName( "com.sun.star.text.ControlCharacter.HARD_SPACE" ) com_sun_star_text_ControlCharacter_APPEND_PARAGRAPH = uno.getConstantByName( "com.sun.star.text.ControlCharacter.APPEND_PARAGRAPH" )


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 ) 
       self.writeParagraphBreak() 
   def write( self, arg1, *argsRest ): 
       self.writeOne( arg1 ) 
       for arg in argsRest: 
           self.writeTab() 
           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() 
   oOutput.writeLn() 
   oOutput.writeLn( "Tab delimited values..." ) 
   
   oOutput.write( 123 ) 
   oOutput.writeTab() 
   oOutput.write( 456 ) 
   oOutput.writeTab() 
   oOutput.write( 789 ) 
   oOutput.writeLn() 
   
   oOutput.write( 465 ) 
   oOutput.writeTab() 
   oOutput.write( 522 ) 
   oOutput.writeTab() 
   oOutput.write( 835 ) 
   oOutput.writeLn() 
   
   oOutput.write( 886 ) 
   oOutput.writeTab() 
   oOutput.write( 164 ) 
   oOutput.writeTab() 
   oOutput.write( 741 ) 
   oOutput.writeLn()

How it Works

PrintToWriter.py 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
  • Line Break
  • Hard Hyphen
  • Soft Hyphen
  • Hard Space
  • Append Paragraph
Personal tools