1*b1cdbd2cSJim Jagielski# ************************************************************* 2*b1cdbd2cSJim Jagielski# 3*b1cdbd2cSJim Jagielski# Licensed to the Apache Software Foundation (ASF) under one 4*b1cdbd2cSJim Jagielski# or more contributor license agreements. See the NOTICE file 5*b1cdbd2cSJim Jagielski# distributed with this work for additional information 6*b1cdbd2cSJim Jagielski# regarding copyright ownership. The ASF licenses this file 7*b1cdbd2cSJim Jagielski# to you under the Apache License, Version 2.0 (the 8*b1cdbd2cSJim Jagielski# "License"); you may not use this file except in compliance 9*b1cdbd2cSJim Jagielski# with the License. You may obtain a copy of the License at 10*b1cdbd2cSJim Jagielski# 11*b1cdbd2cSJim Jagielski# http://www.apache.org/licenses/LICENSE-2.0 12*b1cdbd2cSJim Jagielski# 13*b1cdbd2cSJim Jagielski# Unless required by applicable law or agreed to in writing, 14*b1cdbd2cSJim Jagielski# software distributed under the License is distributed on an 15*b1cdbd2cSJim Jagielski# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*b1cdbd2cSJim Jagielski# KIND, either express or implied. See the License for the 17*b1cdbd2cSJim Jagielski# specific language governing permissions and limitations 18*b1cdbd2cSJim Jagielski# under the License. 19*b1cdbd2cSJim Jagielski# 20*b1cdbd2cSJim Jagielski# ************************************************************* 21*b1cdbd2cSJim Jagielski 22*b1cdbd2cSJim Jagielskiimport uno 23*b1cdbd2cSJim Jagielski 24*b1cdbd2cSJim Jagielski# a UNO struct later needed to create a document 25*b1cdbd2cSJim Jagielskifrom com.sun.star.text.ControlCharacter import PARAGRAPH_BREAK 26*b1cdbd2cSJim Jagielskifrom com.sun.star.text.TextContentAnchorType import AS_CHARACTER 27*b1cdbd2cSJim Jagielskifrom com.sun.star.awt import Size 28*b1cdbd2cSJim Jagielski 29*b1cdbd2cSJim Jagielskifrom com.sun.star.lang import XMain 30*b1cdbd2cSJim Jagielski 31*b1cdbd2cSJim Jagielskidef insertTextIntoCell( table, cellName, text, color ): 32*b1cdbd2cSJim Jagielski tableText = table.getCellByName( cellName ) 33*b1cdbd2cSJim Jagielski cursor = tableText.createTextCursor() 34*b1cdbd2cSJim Jagielski cursor.setPropertyValue( "CharColor", color ) 35*b1cdbd2cSJim Jagielski tableText.setString( text ) 36*b1cdbd2cSJim Jagielski 37*b1cdbd2cSJim Jagielski 38*b1cdbd2cSJim Jagielskidef createTable(): 39*b1cdbd2cSJim Jagielski """creates a new writer document and inserts a table with some data (also known as the SWriter sample)""" 40*b1cdbd2cSJim Jagielski ctx = uno.getComponentContext() 41*b1cdbd2cSJim Jagielski smgr = ctx.ServiceManager 42*b1cdbd2cSJim Jagielski desktop = smgr.createInstanceWithContext( "com.sun.star.frame.Desktop",ctx) 43*b1cdbd2cSJim Jagielski 44*b1cdbd2cSJim Jagielski # open a writer document 45*b1cdbd2cSJim Jagielski doc = desktop.loadComponentFromURL( "private:factory/swriter","_blank", 0, () ) 46*b1cdbd2cSJim Jagielski 47*b1cdbd2cSJim Jagielski text = doc.Text 48*b1cdbd2cSJim Jagielski cursor = text.createTextCursor() 49*b1cdbd2cSJim Jagielski text.insertString( cursor, "The first line in the newly created text document.\n", 0 ) 50*b1cdbd2cSJim Jagielski text.insertString( cursor, "Now we are in the second line.\n" , 0 ) 51*b1cdbd2cSJim Jagielski 52*b1cdbd2cSJim Jagielski # create a text table 53*b1cdbd2cSJim Jagielski table = doc.createInstance( "com.sun.star.text.TextTable" ) 54*b1cdbd2cSJim Jagielski 55*b1cdbd2cSJim Jagielski # with 4 rows and 4 columns 56*b1cdbd2cSJim Jagielski table.initialize( 4,4) 57*b1cdbd2cSJim Jagielski 58*b1cdbd2cSJim Jagielski text.insertTextContent( cursor, table, 0 ) 59*b1cdbd2cSJim Jagielski rows = table.Rows 60*b1cdbd2cSJim Jagielski 61*b1cdbd2cSJim Jagielski table.setPropertyValue( "BackTransparent", uno.Bool(0) ) 62*b1cdbd2cSJim Jagielski table.setPropertyValue( "BackColor", 13421823 ) 63*b1cdbd2cSJim Jagielski row = rows.getByIndex(0) 64*b1cdbd2cSJim Jagielski row.setPropertyValue( "BackTransparent", uno.Bool(0) ) 65*b1cdbd2cSJim Jagielski row.setPropertyValue( "BackColor", 6710932 ) 66*b1cdbd2cSJim Jagielski 67*b1cdbd2cSJim Jagielski textColor = 16777215 68*b1cdbd2cSJim Jagielski 69*b1cdbd2cSJim Jagielski insertTextIntoCell( table, "A1", "FirstColumn", textColor ) 70*b1cdbd2cSJim Jagielski insertTextIntoCell( table, "B1", "SecondColumn", textColor ) 71*b1cdbd2cSJim Jagielski insertTextIntoCell( table, "C1", "ThirdColumn", textColor ) 72*b1cdbd2cSJim Jagielski insertTextIntoCell( table, "D1", "SUM", textColor ) 73*b1cdbd2cSJim Jagielski 74*b1cdbd2cSJim Jagielski values = ( (22.5,21.5,121.5), 75*b1cdbd2cSJim Jagielski (5615.3,615.3,-615.3), 76*b1cdbd2cSJim Jagielski (-2315.7,315.7,415.7) ) 77*b1cdbd2cSJim Jagielski table.getCellByName("A2").setValue(22.5) 78*b1cdbd2cSJim Jagielski table.getCellByName("B2").setValue(5615.3) 79*b1cdbd2cSJim Jagielski table.getCellByName("C2").setValue(-2315.7) 80*b1cdbd2cSJim Jagielski table.getCellByName("D2").setFormula("sum <A2:C2>") 81*b1cdbd2cSJim Jagielski 82*b1cdbd2cSJim Jagielski table.getCellByName("A3").setValue(21.5) 83*b1cdbd2cSJim Jagielski table.getCellByName("B3").setValue(615.3) 84*b1cdbd2cSJim Jagielski table.getCellByName("C3").setValue(-315.7) 85*b1cdbd2cSJim Jagielski table.getCellByName("D3").setFormula("sum <A3:C3>") 86*b1cdbd2cSJim Jagielski 87*b1cdbd2cSJim Jagielski table.getCellByName("A4").setValue(121.5) 88*b1cdbd2cSJim Jagielski table.getCellByName("B4").setValue(-615.3) 89*b1cdbd2cSJim Jagielski table.getCellByName("C4").setValue(415.7) 90*b1cdbd2cSJim Jagielski table.getCellByName("D4").setFormula("sum <A4:C4>") 91*b1cdbd2cSJim Jagielski 92*b1cdbd2cSJim Jagielski 93*b1cdbd2cSJim Jagielski cursor.setPropertyValue( "CharColor", 255 ) 94*b1cdbd2cSJim Jagielski cursor.setPropertyValue( "CharShadowed", uno.Bool(1) ) 95*b1cdbd2cSJim Jagielski 96*b1cdbd2cSJim Jagielski text.insertControlCharacter( cursor, PARAGRAPH_BREAK, 0 ) 97*b1cdbd2cSJim Jagielski text.insertString( cursor, " This is a colored text - blue with shadow\n" , 0 ) 98*b1cdbd2cSJim Jagielski text.insertControlCharacter( cursor, PARAGRAPH_BREAK, 0 ) 99*b1cdbd2cSJim Jagielski 100*b1cdbd2cSJim Jagielski textFrame = doc.createInstance( "com.sun.star.text.TextFrame" ) 101*b1cdbd2cSJim Jagielski textFrame.setSize( Size(15000,400)) 102*b1cdbd2cSJim Jagielski textFrame.setPropertyValue( "AnchorType" , AS_CHARACTER ) 103*b1cdbd2cSJim Jagielski 104*b1cdbd2cSJim Jagielski text.insertTextContent( cursor, textFrame, 0 ) 105*b1cdbd2cSJim Jagielski 106*b1cdbd2cSJim Jagielski textInTextFrame = textFrame.getText() 107*b1cdbd2cSJim Jagielski cursorInTextFrame = textInTextFrame.createTextCursor() 108*b1cdbd2cSJim Jagielski textInTextFrame.insertString( cursorInTextFrame, "The first line in the newly created text frame.", 0 ) 109*b1cdbd2cSJim Jagielski textInTextFrame.insertString( cursorInTextFrame, "\nWith this second line the height of the frame raises.",0) 110*b1cdbd2cSJim Jagielski text.insertControlCharacter( cursor, PARAGRAPH_BREAK, 0 ) 111*b1cdbd2cSJim Jagielski 112*b1cdbd2cSJim Jagielski cursor.setPropertyValue( "CharColor", 65536 ) 113*b1cdbd2cSJim Jagielski cursor.setPropertyValue( "CharShadowed", uno.Bool(0) ) 114*b1cdbd2cSJim Jagielski 115*b1cdbd2cSJim Jagielski text.insertString( cursor, " That's all for now!" , 0 ) 116*b1cdbd2cSJim Jagielski 117*b1cdbd2cSJim Jagielskig_exportedScripts = createTable, 118