1bd8ef897SAndrew Rist# ************************************************************* 2bd8ef897SAndrew Rist# 3bd8ef897SAndrew Rist# Licensed to the Apache Software Foundation (ASF) under one 4bd8ef897SAndrew Rist# or more contributor license agreements. See the NOTICE file 5bd8ef897SAndrew Rist# distributed with this work for additional information 6bd8ef897SAndrew Rist# regarding copyright ownership. The ASF licenses this file 7bd8ef897SAndrew Rist# to you under the Apache License, Version 2.0 (the 8bd8ef897SAndrew Rist# "License"); you may not use this file except in compliance 9bd8ef897SAndrew Rist# with the License. You may obtain a copy of the License at 10bd8ef897SAndrew Rist# 11bd8ef897SAndrew Rist# http://www.apache.org/licenses/LICENSE-2.0 12bd8ef897SAndrew Rist# 13bd8ef897SAndrew Rist# Unless required by applicable law or agreed to in writing, 14bd8ef897SAndrew Rist# software distributed under the License is distributed on an 15bd8ef897SAndrew Rist# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16bd8ef897SAndrew Rist# KIND, either express or implied. See the License for the 17bd8ef897SAndrew Rist# specific language governing permissions and limitations 18bd8ef897SAndrew Rist# under the License. 19bd8ef897SAndrew Rist# 20bd8ef897SAndrew Rist# ************************************************************* 21bd8ef897SAndrew Rist 22cdf0e10cSrcweir# just a simple copy of the swriter.py demo, but implemented as a component. The advantage is, 23cdf0e10cSrcweir# that the component may run within the office process which may give a performance improvement. 24cdf0e10cSrcweir 25cdf0e10cSrcweirimport unohelper 26cdf0e10cSrcweirimport uno 27cdf0e10cSrcweir 28cdf0e10cSrcweir# a UNO struct later needed to create a document 29cdf0e10cSrcweirfrom com.sun.star.text.ControlCharacter import PARAGRAPH_BREAK 30cdf0e10cSrcweirfrom com.sun.star.text.TextContentAnchorType import AS_CHARACTER 31cdf0e10cSrcweirfrom com.sun.star.awt import Size 32cdf0e10cSrcweir 33cdf0e10cSrcweirfrom com.sun.star.lang import XMain 34cdf0e10cSrcweir 35cdf0e10cSrcweirdef insertTextIntoCell( table, cellName, text, color ): 36cdf0e10cSrcweir tableText = table.getCellByName( cellName ) 37cdf0e10cSrcweir cursor = tableText.createTextCursor() 38cdf0e10cSrcweir cursor.setPropertyValue( "CharColor", color ) 39cdf0e10cSrcweir tableText.setString( text ) 40cdf0e10cSrcweir 41cdf0e10cSrcweir# the UNO component 42cdf0e10cSrcweir# implementing the interface com.sun.star.lang.XMain 43cdf0e10cSrcweir# unohelper.Base implements the XTypeProvider interface 44cdf0e10cSrcweirclass SWriterComp(XMain,unohelper.Base): 45*d912c6c5SPedro Giffuni def __init__( self, ctx ): 46*d912c6c5SPedro Giffuni self.ctx = ctx 47cdf0e10cSrcweir 48*d912c6c5SPedro Giffuni # implementation for XMain.run( [in] sequence< any > ) 49*d912c6c5SPedro Giffuni def run( self,args ): 50cdf0e10cSrcweir 51*d912c6c5SPedro Giffuni ctx = self.ctx 52*d912c6c5SPedro Giffuni smgr = ctx.ServiceManager 53*d912c6c5SPedro Giffuni desktop = smgr.createInstanceWithContext( "com.sun.star.frame.Desktop",ctx) 54cdf0e10cSrcweir 55*d912c6c5SPedro Giffuni # open a writer document 56*d912c6c5SPedro Giffuni doc = desktop.loadComponentFromURL( "private:factory/swriter","_blank", 0, () ) 57cdf0e10cSrcweir 58*d912c6c5SPedro Giffuni text = doc.Text 59*d912c6c5SPedro Giffuni cursor = text.createTextCursor() 60*d912c6c5SPedro Giffuni text.insertString( cursor, "The first line in the newly created text document.\n", 0 ) 61*d912c6c5SPedro Giffuni text.insertString( cursor, "Now we are in the second line\n" , 0 ) 62cdf0e10cSrcweir 63*d912c6c5SPedro Giffuni # create a text table 64*d912c6c5SPedro Giffuni table = doc.createInstance( "com.sun.star.text.TextTable" ) 65cdf0e10cSrcweir 66*d912c6c5SPedro Giffuni # with 4 rows and 4 columns 67*d912c6c5SPedro Giffuni table.initialize( 4,4) 68cdf0e10cSrcweir 69*d912c6c5SPedro Giffuni text.insertTextContent( cursor, table, 0 ) 70*d912c6c5SPedro Giffuni rows = table.Rows 71cdf0e10cSrcweir 72*d912c6c5SPedro Giffuni table.setPropertyValue( "BackTransparent", uno.Bool(0) ) 73*d912c6c5SPedro Giffuni table.setPropertyValue( "BackColor", 13421823 ) 74*d912c6c5SPedro Giffuni row = rows.getByIndex(0) 75*d912c6c5SPedro Giffuni row.setPropertyValue( "BackTransparent", uno.Bool(0) ) 76*d912c6c5SPedro Giffuni row.setPropertyValue( "BackColor", 6710932 ) 77cdf0e10cSrcweir 78*d912c6c5SPedro Giffuni textColor = 16777215 79cdf0e10cSrcweir 80*d912c6c5SPedro Giffuni insertTextIntoCell( table, "A1", "FirstColumn", textColor ) 81*d912c6c5SPedro Giffuni insertTextIntoCell( table, "B1", "SecondColumn", textColor ) 82*d912c6c5SPedro Giffuni insertTextIntoCell( table, "C1", "ThirdColumn", textColor ) 83*d912c6c5SPedro Giffuni insertTextIntoCell( table, "D1", "SUM", textColor ) 84cdf0e10cSrcweir 85*d912c6c5SPedro Giffuni values = ( (22.5,21.5,121.5), 86*d912c6c5SPedro Giffuni (5615.3,615.3,-615.3), 87*d912c6c5SPedro Giffuni (-2315.7,315.7,415.7) ) 88*d912c6c5SPedro Giffuni table.getCellByName("A2").setValue(22.5) 89*d912c6c5SPedro Giffuni table.getCellByName("B2").setValue(5615.3) 90*d912c6c5SPedro Giffuni table.getCellByName("C2").setValue(-2315.7) 91*d912c6c5SPedro Giffuni table.getCellByName("D2").setFormula("sum <A2:C2>") 92cdf0e10cSrcweir 93*d912c6c5SPedro Giffuni table.getCellByName("A3").setValue(21.5) 94*d912c6c5SPedro Giffuni table.getCellByName("B3").setValue(615.3) 95*d912c6c5SPedro Giffuni table.getCellByName("C3").setValue(-315.7) 96*d912c6c5SPedro Giffuni table.getCellByName("D3").setFormula("sum <A3:C3>") 97cdf0e10cSrcweir 98*d912c6c5SPedro Giffuni table.getCellByName("A4").setValue(121.5) 99*d912c6c5SPedro Giffuni table.getCellByName("B4").setValue(-615.3) 100*d912c6c5SPedro Giffuni table.getCellByName("C4").setValue(415.7) 101*d912c6c5SPedro Giffuni table.getCellByName("D4").setFormula("sum <A4:C4>") 102cdf0e10cSrcweir 103cdf0e10cSrcweir 104*d912c6c5SPedro Giffuni cursor.setPropertyValue( "CharColor", 255 ) 105*d912c6c5SPedro Giffuni cursor.setPropertyValue( "CharShadowed", uno.Bool(1) ) 106cdf0e10cSrcweir 107*d912c6c5SPedro Giffuni text.insertControlCharacter( cursor, PARAGRAPH_BREAK, 0 ) 108*d912c6c5SPedro Giffuni text.insertString( cursor, " This is a colored Text - blue with shadow\n" , 0 ) 109*d912c6c5SPedro Giffuni text.insertControlCharacter( cursor, PARAGRAPH_BREAK, 0 ) 110cdf0e10cSrcweir 111*d912c6c5SPedro Giffuni textFrame = doc.createInstance( "com.sun.star.text.TextFrame" ) 112*d912c6c5SPedro Giffuni textFrame.setSize( Size(15000,400)) 113*d912c6c5SPedro Giffuni textFrame.setPropertyValue( "AnchorType" , AS_CHARACTER ) 114cdf0e10cSrcweir 115*d912c6c5SPedro Giffuni text.insertTextContent( cursor, textFrame, 0 ) 116cdf0e10cSrcweir 117*d912c6c5SPedro Giffuni textInTextFrame = textFrame.getText() 118*d912c6c5SPedro Giffuni cursorInTextFrame = textInTextFrame.createTextCursor() 119*d912c6c5SPedro Giffuni textInTextFrame.insertString( cursorInTextFrame, "The first line in the newly created text frame.", 0 ) 120*d912c6c5SPedro Giffuni textInTextFrame.insertString( cursorInTextFrame, "\nWith this second line the height of the rame raises.",0) 121*d912c6c5SPedro Giffuni text.insertControlCharacter( cursor, PARAGRAPH_BREAK, 0 ) 122cdf0e10cSrcweir 123*d912c6c5SPedro Giffuni cursor.setPropertyValue( "CharColor", 65536 ) 124*d912c6c5SPedro Giffuni cursor.setPropertyValue( "CharShadowed", uno.Bool(0) ) 125cdf0e10cSrcweir 126*d912c6c5SPedro Giffuni text.insertString( cursor, " That's all for now !!" , 0 ) 127*d912c6c5SPedro Giffuni return 0 128cdf0e10cSrcweir 129cdf0e10cSrcweir 130cdf0e10cSrcweir# pythonloader looks for a static g_ImplementationHelper variable 131cdf0e10cSrcweirg_ImplementationHelper = unohelper.ImplementationHelper() 132cdf0e10cSrcweirg_ImplementationHelper.addImplementation( \ 133*d912c6c5SPedro Giffuni SWriterComp,"org.openoffice.comp.pyuno.swriter",("org.openoffice.demo.SWriter",),) 134