crashtesting: assert on reimport of docx export of ooo102874-2.doc
[LibreOffice.git] / pyuno / demo / swriter.py
blob70af4f98d003ddddd166c2f71869d64a8a1f4a61
1 # -*- tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4 -*-
3 # This file is part of the LibreOffice project.
5 # This Source Code Form is subject to the terms of the Mozilla Public
6 # License, v. 2.0. If a copy of the MPL was not distributed with this
7 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 # This file incorporates work covered by the following license notice:
11 # Licensed to the Apache Software Foundation (ASF) under one or more
12 # contributor license agreements. See the NOTICE file distributed
13 # with this work for additional information regarding copyright
14 # ownership. The ASF licenses this file to you under the Apache
15 # License, Version 2.0 (the "License"); you may not use this file
16 # except in compliance with the License. You may obtain a copy of
17 # the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 # bootstrap uno component context
21 import uno
23 # a UNO struct later needed to create a document
24 from com.sun.star.text.ControlCharacter import PARAGRAPH_BREAK
25 from com.sun.star.text.TextContentAnchorType import AS_CHARACTER
26 from com.sun.star.awt import Size
28 def insertTextIntoCell( table, cellName, text, color ):
29 tableText = table.getCellByName( cellName )
30 cursor = tableText.createTextCursor()
31 cursor.setPropertyValue( "CharColor", color )
32 tableText.setString( text )
34 localContext = uno.getComponentContext()
36 resolver = localContext.ServiceManager.createInstanceWithContext(
37 "com.sun.star.bridge.UnoUrlResolver", localContext )
39 smgr = resolver.resolve( "uno:socket,host=localhost,port=2002;urp;StarOffice.ServiceManager" )
40 remoteContext = smgr.getPropertyValue( "DefaultContext" )
42 #remoteContext = resolver.resolve( "uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext" )
43 #smgr = remoteContext.ServiceManager
45 desktop = smgr.createInstanceWithContext( "com.sun.star.frame.Desktop",remoteContext)
47 # open a writer document
48 doc = desktop.loadComponentFromURL( "private:factory/swriter","_blank", 0, () )
50 text = doc.Text
51 cursor = text.createTextCursor()
52 text.insertString( cursor, "The first line in the newly created text document.\n", 0 )
53 text.insertString( cursor, "Now we are in the second line\n" , 0 )
55 # create a text table
56 table = doc.createInstance( "com.sun.star.text.TextTable" )
58 # with 4 rows and 4 columns
59 table.initialize(4, 4)
61 text.insertTextContent( cursor, table, 0 )
62 rows = table.Rows
64 table.setPropertyValue( "BackTransparent", False )
65 table.setPropertyValue( "BackColor", 13421823 )
66 row = rows[0]
67 row.setPropertyValue( "BackTransparent", False )
68 row.setPropertyValue( "BackColor", 6710932 )
70 textColor = 16777215
72 insertTextIntoCell( table, "A1", "FirstColumn", textColor )
73 insertTextIntoCell( table, "B1", "SecondColumn", textColor )
74 insertTextIntoCell( table, "C1", "ThirdColumn", textColor )
75 insertTextIntoCell( table, "D1", "SUM", textColor )
77 table.getCellByName("A2").setValue(22.5)
78 table.getCellByName("B2").setValue(5615.3)
79 table.getCellByName("C2").setValue(-2315.7)
80 table.getCellByName("D2").setFormula("sum <A2:C2>")
82 table.getCellByName("A3").setValue(21.5)
83 table.getCellByName("B3").setValue(615.3)
84 table.getCellByName("C3").setValue(-315.7)
85 table.getCellByName("D3").setFormula("sum <A3:C3>")
87 table.getCellByName("A4").setValue(121.5)
88 table.getCellByName("B4").setValue(-615.3)
89 table.getCellByName("C4").setValue(415.7)
90 table.getCellByName("D4").setFormula("sum <A4:C4>")
93 cursor.setPropertyValue( "CharColor", 255 )
94 cursor.setPropertyValue( "CharShadowed", True )
96 text.insertControlCharacter( cursor, PARAGRAPH_BREAK, 0 )
97 text.insertString( cursor, " This is a colored Text - blue with shadow\n" , 0 )
98 text.insertControlCharacter( cursor, PARAGRAPH_BREAK, 0 )
100 textFrame = doc.createInstance( "com.sun.star.text.TextFrame" )
101 textFrame.setSize( Size(15000,400))
102 textFrame.setPropertyValue( "AnchorType" , AS_CHARACTER )
105 text.insertTextContent( cursor, textFrame, 0 )
107 textInTextFrame = textFrame.getText()
108 cursorInTextFrame = textInTextFrame.createTextCursor()
109 textInTextFrame.insertString( cursorInTextFrame, "The first line in the newly created text frame.", 0 )
110 textInTextFrame.insertString( cursorInTextFrame, "\nWith this second line the height of the rame raises.",0)
111 text.insertControlCharacter( cursor, PARAGRAPH_BREAK, 0 )
113 cursor.setPropertyValue( "CharColor", 65536 )
114 cursor.setPropertyValue( "CharShadowed", False )
116 text.insertString( cursor, " That's all for now!" , 0 )
118 # vim: set shiftwidth=4 softtabstop=4 expandtab: