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