2 # -*- coding: utf-8 -*-
3 #**********************************************************************
5 # PrintToWriter.py - sample script for openoffice-python
7 # This implements a simple class which makes it easy and convenient
8 # to print a bunch of text into a Writer document.
10 # http://openoffice-python.origo.ethz.ch/
12 #**********************************************************************
14 # Copyright (c) 2003-2004 Danny Brewer <d29583@groovegarden.com>
15 # Copyright (c) 2008 by Hartmut Goebel <h.goebel@goebel-consult.de>
17 # This library is free software; you can redistribute it and/or modify
18 # it under the terms of the GNU Lesser General Public License as
19 # published by the Free Software Foundation; either version 2.1 of the
20 # License, or (at your option) any later version.
22 # This library is distributed in the hope that it will be useful, but
23 # WITHOUT ANY WARRANTY; without even the implied warranty of
24 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 # Lesser General Public License for more details.
27 # You should have received a copy of the GNU Lesser General Public
28 # License along with this library; if not, write to the Free Software
29 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
32 # See: http://www.gnu.org/licenses/lgpl.html
34 #**********************************************************************
36 # If you make changes, please append to the change log below.
39 # Danny Brewer 2004-06-05 Revised
40 # Hartmut Goebel 2008-03-09 Major changes, adopted for
43 #**********************************************************************
46 import openoffice.interact
48 _ControlCharacter = "com.sun.star.text.ControlCharacter"
50 PARAGRAPH_BREAK = uno.getConstantByName(_ControlCharacter+ ".PARAGRAPH_BREAK")
51 LINE_BREAK = uno.getConstantByName(_ControlCharacter+ ".LINE_BREAK")
52 HARD_HYPHEN = uno.getConstantByName(_ControlCharacter+ ".HARD_HYPHEN")
53 SOFT_HYPHEN = uno.getConstantByName(_ControlCharacter+ ".SOFT_HYPHEN")
54 HARD_SPACE = uno.getConstantByName(_ControlCharacter+ ".HARD_SPACE")
55 APPEND_PARAGRAPH = uno.getConstantByName(_ControlCharacter+ ".APPEND_PARAGRAPH")
59 A class which allows conveniently printing stuff into a Writer
62 Very useful for debugging, general output purposes, or even for
66 def __init__(self, desktop=None):
68 desktop = openoffice.interact.Desktop() # todo: defaultDesktop?
69 self._doc = desktop.newDoc('swriter', )#target='_blank', hidden=1)
70 self._text = self._doc.getText()
71 self._cursor = self._text.createTextCursor()
73 def writeControlCharacter(self, char, absorb=False):
74 # todo: what does 'absort' mean?
75 self._text.insertControlCharacter(self._cursor, char, absorb)
77 def write(self, string, absorb=False):
78 self._text.insertString(self._cursor, str(string), absorb)
80 def writelines(self, lines, absorb=False):
82 self._text.insertString(self._cursor, line, absorb)
84 def writeTab(self, absorb=False):
85 self.write("\t", absorb)
87 def paragraphBreak(self, absorb=False):
88 self.writeControlCharacter(PARAGRAPH_BREAK, absorb)
90 def writeLn(self, *args):
94 def writeTabSeparated(self, *args):
104 if __name__ == '__main__':
106 An example of how to use the PrintToWriter class to trivially
107 create a new Writer document, and write out text into it.
109 doc = PrintToWriter()
110 doc.writeLn("Hello World")
111 doc.writeTabSeparated("String", 123, 456.23, True)
115 doc.writeLn("Tab delimited values ...")
117 doc.writeTabSeparated(123, 456, 789); doc.writeLn()
118 doc.writeTabSeparated(465, 522, 835); doc.writeLn()
119 doc.writeTabSeparated(886, 164, 741); doc.writeLn()