TableSample.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #
  2. # This file is part of the LibreOffice project.
  3. #
  4. # This Source Code Form is subject to the terms of the Mozilla Public
  5. # License, v. 2.0. If a copy of the MPL was not distributed with this
  6. # file, You can obtain one at http://mozilla.org/MPL/2.0/.
  7. #
  8. # This file incorporates work covered by the following license notice:
  9. #
  10. # Licensed to the Apache Software Foundation (ASF) under one or more
  11. # contributor license agreements. See the NOTICE file distributed
  12. # with this work for additional information regarding copyright
  13. # ownership. The ASF licenses this file to you under the Apache
  14. # License, Version 2.0 (the "License"); you may not use this file
  15. # except in compliance with the License. You may obtain a copy of
  16. # the License at http://www.apache.org/licenses/LICENSE-2.0 .
  17. #
  18. import uno
  19. # a UNO struct later needed to create a document
  20. from com.sun.star.text.ControlCharacter import PARAGRAPH_BREAK
  21. from com.sun.star.text.TextContentAnchorType import AS_CHARACTER
  22. from com.sun.star.awt import Size
  23. def insertTextIntoCell(table, cellName, text, color):
  24. tableText = table.getCellByName(cellName)
  25. cursor = tableText.createTextCursor()
  26. cursor.setPropertyValue("CharColor", color)
  27. tableText.setString(text)
  28. def createTable():
  29. """Creates a new writer document and inserts a table with some data
  30. (also known as the SWriter sample).
  31. """
  32. ctx = uno.getComponentContext()
  33. smgr = ctx.ServiceManager
  34. desktop = smgr.createInstanceWithContext("com.sun.star.frame.Desktop", ctx)
  35. # open a writer document
  36. doc = desktop.loadComponentFromURL(
  37. "private:factory/swriter", "_blank", 0, ())
  38. text = doc.Text
  39. cursor = text.createTextCursor()
  40. text.insertString(
  41. cursor,
  42. "The first line in the newly created text document.\n",
  43. 0)
  44. text.insertString(
  45. cursor,
  46. "Now we are in the second line\n",
  47. 0)
  48. # create a text table
  49. table = doc.createInstance("com.sun.star.text.TextTable")
  50. # with 4 rows and 4 columns
  51. table.initialize(4, 4)
  52. text.insertTextContent(cursor, table, 0)
  53. rows = table.Rows
  54. table.setPropertyValue("BackTransparent", uno.Bool(0))
  55. table.setPropertyValue("BackColor", 13421823)
  56. row = rows.getByIndex(0)
  57. row.setPropertyValue("BackTransparent", uno.Bool(0))
  58. row.setPropertyValue("BackColor", 6710932)
  59. textColor = 16777215
  60. insertTextIntoCell(table, "A1", "FirstColumn", textColor)
  61. insertTextIntoCell(table, "B1", "SecondColumn", textColor)
  62. insertTextIntoCell(table, "C1", "ThirdColumn", textColor)
  63. insertTextIntoCell(table, "D1", "SUM", textColor)
  64. table.getCellByName("A2").setValue(22.5)
  65. table.getCellByName("B2").setValue(5615.3)
  66. table.getCellByName("C2").setValue(-2315.7)
  67. table.getCellByName("D2").setFormula("sum <A2:C2>")
  68. table.getCellByName("A3").setValue(21.5)
  69. table.getCellByName("B3").setValue(615.3)
  70. table.getCellByName("C3").setValue(-315.7)
  71. table.getCellByName("D3").setFormula("sum <A3:C3>")
  72. table.getCellByName("A4").setValue(121.5)
  73. table.getCellByName("B4").setValue(-615.3)
  74. table.getCellByName("C4").setValue(415.7)
  75. table.getCellByName("D4").setFormula("sum <A4:C4>")
  76. cursor.setPropertyValue("CharColor", 255)
  77. cursor.setPropertyValue("CharShadowed", uno.Bool(1))
  78. text.insertControlCharacter(cursor, PARAGRAPH_BREAK, 0)
  79. text.insertString(
  80. cursor,
  81. "This is a colored Text - blue with shadow\n",
  82. 0)
  83. text.insertControlCharacter(cursor, PARAGRAPH_BREAK, 0)
  84. textFrame = doc.createInstance("com.sun.star.text.TextFrame")
  85. textFrame.setSize(Size(15000, 400))
  86. textFrame.setPropertyValue("AnchorType", AS_CHARACTER)
  87. text.insertTextContent(cursor, textFrame, 0)
  88. textInTextFrame = textFrame.getText()
  89. cursorInTextFrame = textInTextFrame.createTextCursor()
  90. textInTextFrame.insertString(
  91. cursorInTextFrame,
  92. "The first line in the newly created text frame.",
  93. 0)
  94. textInTextFrame.insertString(
  95. cursorInTextFrame,
  96. "\nWith this second line the height of the rame raises.",
  97. 0)
  98. text.insertControlCharacter(cursor, PARAGRAPH_BREAK, 0)
  99. cursor.setPropertyValue("CharColor", 65536)
  100. cursor.setPropertyValue("CharShadowed", uno.Bool(0))
  101. text.insertString(cursor, "That's all for now !!", 0)
  102. g_exportedScripts = createTable,
  103. # vim: set shiftwidth=4 softtabstop=4 expandtab: