ods-currency.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Copyright (C) 2009 Brad Ralph, Sydney, Australia
  4. #
  5. # This is free software. You may redistribute it under the terms
  6. # of the Apache license and the GNU General Public License Version
  7. # 2 or at your option any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public
  15. # License along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. #
  18. # Contributor(s):
  19. # Søren Roug
  20. # This example shows how to do a conditional currency style. We want negative
  21. # numbers to show as red and as Australian dollars.
  22. from odf.opendocument import OpenDocumentSpreadsheet
  23. from odf.style import Style, TextProperties, TableColumnProperties, Map
  24. from odf.number import NumberStyle, CurrencyStyle, CurrencySymbol, Number, Text
  25. from odf.text import P
  26. from odf.table import Table, TableColumn, TableRow, TableCell
  27. textdoc = OpenDocumentSpreadsheet()
  28. # Create a style for the table content. One we can modify
  29. # later in the spreadsheet.
  30. tablecontents = Style(name="Large number", family="table-cell")
  31. tablecontents.addElement(TextProperties(fontfamily="Arial", fontsize="15pt"))
  32. textdoc.styles.addElement(tablecontents)
  33. # Create automatic styles for the column widths.
  34. widewidth = Style(name="co1", family="table-column")
  35. widewidth.addElement(TableColumnProperties(columnwidth="2.8cm", breakbefore="auto"))
  36. textdoc.automaticstyles.addElement(widewidth)
  37. # Create the styles for $AUD format currency values
  38. ns1 = CurrencyStyle(name="positive-AUD", volatile="true")
  39. ns1.addElement(CurrencySymbol(language="en", country="AU", text=u"$"))
  40. ns1.addElement(Number(decimalplaces="2", minintegerdigits="1", grouping="true"))
  41. textdoc.styles.addElement(ns1)
  42. # Create the main style.
  43. ns2 = CurrencyStyle(name="main-AUD")
  44. ns2.addElement(TextProperties(color="#ff0000"))
  45. ns2.addElement(Text(text=u"-"))
  46. ns2.addElement(CurrencySymbol(language="en", country="AU", text=u"$"))
  47. ns2.addElement(Number(decimalplaces="2", minintegerdigits="1", grouping="true"))
  48. ns2.addElement(Map(condition="value()>=0", applystylename="positive-AUD"))
  49. textdoc.styles.addElement(ns2)
  50. # Create automatic style for the price cells.
  51. moneycontents = Style(name="ce1", family="table-cell", parentstylename=tablecontents, datastylename="main-AUD")
  52. textdoc.automaticstyles.addElement(moneycontents)
  53. # Start the table, and describe the columns
  54. table = Table(name="Currency colours")
  55. # Create a column (same as <col> in HTML) Make all cells in column default to currency
  56. table.addElement(TableColumn(stylename=widewidth, defaultcellstylename="ce1"))
  57. # Create a row (same as <tr> in HTML)
  58. tr = TableRow()
  59. table.addElement(tr)
  60. # Create a cell with a negative value. It should show as red.
  61. cell = TableCell(valuetype="currency", currency="AUD", value="-125")
  62. cell.addElement(P(text=u"$-125.00")) # The current displayed value
  63. tr.addElement(cell)
  64. # Create a row (same as <tr> in HTML)
  65. tr = TableRow()
  66. table.addElement(tr)
  67. # Create another cell but with a positive value. It should show in black
  68. cell = TableCell(valuetype="currency", currency="AUD", value="123")
  69. cell.addElement(P(text=u"$123.00")) # The current displayed value
  70. tr.addElement(cell)
  71. textdoc.spreadsheet.addElement(table)
  72. textdoc.save("currency.ods")