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