1 2# bootstrap uno component context 3import uno 4import unohelper 5 6 7# a UNO struct later needed to create a document 8from com.sun.star.text.ControlCharacter import PARAGRAPH_BREAK 9from com.sun.star.text.TextContentAnchorType import AS_CHARACTER 10from com.sun.star.awt import Size 11 12 13def insertTextIntoCell( table, cellName, text, color ): 14 tableText = table.getCellByName( cellName ) 15 cursor = tableText.createTextCursor() 16 cursor.setPropertyValue( "CharColor", color ) 17 tableText.setString( text ) 18 19localContext = uno.getComponentContext() 20 21resolver = localContext.ServiceManager.createInstanceWithContext( 22 "com.sun.star.bridge.UnoUrlResolver", localContext ) 23 24smgr = resolver.resolve( "uno:socket,host=localhost,port=2002;urp;StarOffice.ServiceManager" ) 25remoteContext = smgr.getPropertyValue( "DefaultContext" ) 26 27#remoteContext = resolver.resolve( "uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext" ) 28#smgr = remoteContext.ServiceManager 29 30desktop = smgr.createInstanceWithContext( "com.sun.star.frame.Desktop",remoteContext) 31 32# open a writer document 33doc = desktop.loadComponentFromURL( "private:factory/swriter","_blank", 0, () ) 34 35text = doc.Text 36cursor = text.createTextCursor() 37text.insertString( cursor, "The first line in the newly created text document.\n", 0 ) 38text.insertString( cursor, "Now we are in the second line\n" , 0 ) 39 40# create a text table 41table = doc.createInstance( "com.sun.star.text.TextTable" ) 42 43# with 4 rows and 4 columns 44table.initialize( 4,4) 45 46text.insertTextContent( cursor, table, 0 ) 47rows = table.Rows 48 49table.setPropertyValue( "BackTransparent", uno.Bool(0) ) 50table.setPropertyValue( "BackColor", 13421823 ) 51row = rows.getByIndex(0) 52row.setPropertyValue( "BackTransparent", uno.Bool(0) ) 53row.setPropertyValue( "BackColor", 6710932 ) 54 55textColor = 16777215 56 57insertTextIntoCell( table, "A1", "FirstColumn", textColor ) 58insertTextIntoCell( table, "B1", "SecondColumn", textColor ) 59insertTextIntoCell( table, "C1", "ThirdColumn", textColor ) 60insertTextIntoCell( table, "D1", "SUM", textColor ) 61 62values = ( (22.5,21.5,121.5), 63 (5615.3,615.3,-615.3), 64 (-2315.7,315.7,415.7) ) 65table.getCellByName("A2").setValue(22.5) 66table.getCellByName("B2").setValue(5615.3) 67table.getCellByName("C2").setValue(-2315.7) 68table.getCellByName("D2").setFormula("sum <A2:C2>") 69 70table.getCellByName("A3").setValue(21.5) 71table.getCellByName("B3").setValue(615.3) 72table.getCellByName("C3").setValue(-315.7) 73table.getCellByName("D3").setFormula("sum <A3:C3>") 74 75table.getCellByName("A4").setValue(121.5) 76table.getCellByName("B4").setValue(-615.3) 77table.getCellByName("C4").setValue(415.7) 78table.getCellByName("D4").setFormula("sum <A4:C4>") 79 80 81cursor.setPropertyValue( "CharColor", 255 ) 82cursor.setPropertyValue( "CharShadowed", uno.Bool(1) ) 83 84text.insertControlCharacter( cursor, PARAGRAPH_BREAK, 0 ) 85text.insertString( cursor, " This is a colored Text - blue with shadow\n" , 0 ) 86text.insertControlCharacter( cursor, PARAGRAPH_BREAK, 0 ) 87 88textFrame = doc.createInstance( "com.sun.star.text.TextFrame" ) 89textFrame.setSize( Size(15000,400)) 90textFrame.setPropertyValue( "AnchorType" , AS_CHARACTER ) 91 92 93text.insertTextContent( cursor, textFrame, 0 ) 94 95textInTextFrame = textFrame.getText() 96cursorInTextFrame = textInTextFrame.createTextCursor() 97textInTextFrame.insertString( cursorInTextFrame, "The first line in the newly created text frame.", 0 ) 98textInTextFrame.insertString( cursorInTextFrame, "\nWith this second line the height of the rame raises.",0) 99text.insertControlCharacter( cursor, PARAGRAPH_BREAK, 0 ) 100 101cursor.setPropertyValue( "CharColor", 65536 ) 102cursor.setPropertyValue( "CharShadowed", uno.Bool(0) ) 103 104text.insertString( cursor, " That's all for now !!" , 0 ) 105 106