Version 6.4.0.3, tag libreoffice-6.4.0.3
[LibreOffice.git] / pyuno / demo / swritercomp.py
blob3bdd252e4e44986167681c87c21c139a99491e31
1 # -*- tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4 -*-
3 # This file is part of the LibreOffice project.
5 # This Source Code Form is subject to the terms of the Mozilla Public
6 # License, v. 2.0. If a copy of the MPL was not distributed with this
7 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 # This file incorporates work covered by the following license notice:
11 # Licensed to the Apache Software Foundation (ASF) under one or more
12 # contributor license agreements. See the NOTICE file distributed
13 # with this work for additional information regarding copyright
14 # ownership. The ASF licenses this file to you under the Apache
15 # License, Version 2.0 (the "License"); you may not use this file
16 # except in compliance with the License. You may obtain a copy of
17 # the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 # just a simple copy of the swriter.py demo, but implemented as a component. The advantage is,
21 # that the component may run within the office process which may give a performance improvement.
23 import unohelper
24 import uno
26 # a UNO struct later needed to create a document
27 from com.sun.star.text.ControlCharacter import PARAGRAPH_BREAK
28 from com.sun.star.text.TextContentAnchorType import AS_CHARACTER
29 from com.sun.star.awt import Size
31 from com.sun.star.lang import XMain
33 def insertTextIntoCell( table, cellName, text, color ):
34 tableText = table.getCellByName( cellName )
35 cursor = tableText.createTextCursor()
36 cursor.setPropertyValue( "CharColor", color )
37 tableText.setString( text )
39 # the UNO component
40 # implementing the interface com.sun.star.lang.XMain
41 # unohelper.Base implements the XTypeProvider interface
42 class SWriterComp(XMain,unohelper.Base):
43 def __init__( self, ctx ):
44 self.ctx = ctx
46 # implementation for XMain.run( [in] sequence< any > )
47 def run( self,args ):
48 ctx = self.ctx
49 smgr = ctx.ServiceManager
50 desktop = smgr.createInstanceWithContext( "com.sun.star.frame.Desktop",ctx)
52 # open a writer document
53 doc = desktop.loadComponentFromURL( "private:factory/swriter","_blank", 0, () )
55 text = doc.Text
56 cursor = text.createTextCursor()
57 text.insertString( cursor, "The first line in the newly created text document.\n", 0 )
58 text.insertString( cursor, "Now we are in the second line\n" , 0 )
60 # create a text table
61 table = doc.createInstance( "com.sun.star.text.TextTable" )
63 # with 4 rows and 4 columns
64 table.initialize( 4,4)
66 text.insertTextContent( cursor, table, 0 )
67 rows = table.Rows
69 table.setPropertyValue( "BackTransparent", uno.Bool(0) )
70 table.setPropertyValue( "BackColor", 13421823 )
71 row = rows.getByIndex(0)
72 row.setPropertyValue( "BackTransparent", uno.Bool(0) )
73 row.setPropertyValue( "BackColor", 6710932 )
75 textColor = 16777215
77 insertTextIntoCell( table, "A1", "FirstColumn", textColor )
78 insertTextIntoCell( table, "B1", "SecondColumn", textColor )
79 insertTextIntoCell( table, "C1", "ThirdColumn", textColor )
80 insertTextIntoCell( table, "D1", "SUM", textColor )
82 table.getCellByName("A2").setValue(22.5)
83 table.getCellByName("B2").setValue(5615.3)
84 table.getCellByName("C2").setValue(-2315.7)
85 table.getCellByName("D2").setFormula("sum <A2:C2>")
87 table.getCellByName("A3").setValue(21.5)
88 table.getCellByName("B3").setValue(615.3)
89 table.getCellByName("C3").setValue(-315.7)
90 table.getCellByName("D3").setFormula("sum <A3:C3>")
92 table.getCellByName("A4").setValue(121.5)
93 table.getCellByName("B4").setValue(-615.3)
94 table.getCellByName("C4").setValue(415.7)
95 table.getCellByName("D4").setFormula("sum <A4:C4>")
98 cursor.setPropertyValue( "CharColor", 255 )
99 cursor.setPropertyValue( "CharShadowed", uno.Bool(1) )
101 text.insertControlCharacter( cursor, PARAGRAPH_BREAK, 0 )
102 text.insertString( cursor, " This is a colored Text - blue with shadow\n" , 0 )
103 text.insertControlCharacter( cursor, PARAGRAPH_BREAK, 0 )
105 textFrame = doc.createInstance( "com.sun.star.text.TextFrame" )
106 textFrame.setSize( Size(15000,400))
107 textFrame.setPropertyValue( "AnchorType" , AS_CHARACTER )
109 text.insertTextContent( cursor, textFrame, 0 )
111 textInTextFrame = textFrame.getText()
112 cursorInTextFrame = textInTextFrame.createTextCursor()
113 textInTextFrame.insertString( cursorInTextFrame, "The first line in the newly created text frame.", 0 )
114 textInTextFrame.insertString( cursorInTextFrame, "\nWith this second line the height of the rame raises.",0)
115 text.insertControlCharacter( cursor, PARAGRAPH_BREAK, 0 )
117 cursor.setPropertyValue( "CharColor", 65536 )
118 cursor.setPropertyValue( "CharShadowed", uno.Bool(0) )
120 text.insertString( cursor, " That's all for now!" , 0 )
121 return 0
123 # pythonloader looks for a static g_ImplementationHelper variable
124 g_ImplementationHelper = unohelper.ImplementationHelper()
125 g_ImplementationHelper.addImplementation( \
126 SWriterComp,"org.openoffice.comp.pyuno.swriter",("org.openoffice.demo.SWriter",),)
128 # vim: set shiftwidth=4 softtabstop=4 expandtab: