testdatastyles.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Copyright (C) 2009 Søren Roug, European Environment Agency
  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. #
  20. import unittest, os, os.path
  21. from odf.opendocument import OpenDocumentSpreadsheet, OpenDocumentChart, load
  22. from odf.style import Style, ParagraphProperties, TextProperties, GraphicProperties, \
  23. ChartProperties
  24. from odf.number import Text,PercentageStyle, Number
  25. from odf.table import Table,TableRow,TableCell
  26. from odf import text, chart
  27. class TestDatastyles(unittest.TestCase):
  28. saved = False
  29. def tearDown(self):
  30. if self.saved:
  31. os.unlink("TEST.ods")
  32. def test_percentage(self):
  33. """ Test that an automatic style can refer to a PercentageStyle as a datastylename """
  34. doc = OpenDocumentSpreadsheet()
  35. nonze = PercentageStyle(name='N11')
  36. nonze.addElement(Number(decimalplaces='2', minintegerdigits='1'))
  37. nonze.addElement(Text(text='%'))
  38. doc.automaticstyles.addElement(nonze)
  39. pourcent = Style(name='pourcent', family='table-cell', datastylename='N11')
  40. pourcent.addElement(ParagraphProperties(textalign='center'))
  41. pourcent.addElement(TextProperties(attributes={'fontsize':"10pt",'fontweight':"bold", 'color':"#000000" }))
  42. doc.automaticstyles.addElement(pourcent)
  43. table = Table(name='sheet1')
  44. tr = TableRow()
  45. tc = TableCell(formula='=AVERAGE(C4:CB62)/2',stylename='pourcent', valuetype='percentage')
  46. tr.addElement(tc)
  47. table.addElement(tr)
  48. doc.spreadsheet.addElement(table)
  49. doc.save(u"TEST.ods")
  50. self.saved = True
  51. d = load(u"TEST.ods")
  52. result = d.contentxml() # contentxml is supposed to yeld a bytes
  53. self.assertNotEqual(-1, result.find(b'''<number:percentage-style'''))
  54. self.assertNotEqual(-1, result.find(b'''style:data-style-name="N11"'''))
  55. self.assertNotEqual(-1, result.find(b'''style:name="pourcent"'''))
  56. def test_chart_style(self):
  57. """ Test that chart:style-name reference is seen in content.xml """
  58. doc = OpenDocumentChart()
  59. chartstyle = Style(name="chartstyle", family="chart")
  60. chartstyle.addElement( GraphicProperties(stroke="none", fillcolor="#ffffff"))
  61. doc.automaticstyles.addElement(chartstyle)
  62. mychart = chart.Chart( width="576pt", height="504pt", stylename=chartstyle, attributes={'class':'chart:bar'})
  63. doc.chart.addElement(mychart)
  64. # Add title
  65. titlestyle = Style(name="titlestyle", family="chart")
  66. titlestyle.addElement( GraphicProperties(stroke="none", fill="none"))
  67. titlestyle.addElement( TextProperties(fontfamily="'Nimbus Sans L'",
  68. fontfamilygeneric="swiss", fontpitch="variable", fontsize="13pt"))
  69. doc.automaticstyles.addElement(titlestyle)
  70. mytitle = chart.Title(x="385pt", y="27pt", stylename=titlestyle)
  71. mytitle.addElement( text.P(text="Title"))
  72. mychart.addElement(mytitle)
  73. # Add subtitle
  74. subtitlestyle = Style(name="subtitlestyle", family="chart")
  75. subtitlestyle.addElement( GraphicProperties(stroke="none", fill="none"))
  76. subtitlestyle.addElement( TextProperties(fontfamily="'Nimbus Sans L'",
  77. fontfamilygeneric="swiss", fontpitch="variable", fontsize="10pt"))
  78. doc.automaticstyles.addElement(subtitlestyle)
  79. subtitle = chart.Subtitle(x="0pt", y="123pt", stylename=subtitlestyle)
  80. subtitle.addElement( text.P(text="my subtitle"))
  81. mychart.addElement(subtitle)
  82. # Legend
  83. legendstyle = Style(name="legendstyle", family="chart")
  84. legendstyle.addElement( GraphicProperties(fill="none"))
  85. legendstyle.addElement( TextProperties(fontfamily="'Nimbus Sans L'",
  86. fontfamilygeneric="swiss", fontpitch="variable", fontsize="6pt"))
  87. doc.automaticstyles.addElement(legendstyle)
  88. mylegend = chart.Legend(legendposition="end", legendalign="center", stylename=legendstyle)
  89. mychart.addElement(mylegend)
  90. # Plot area
  91. plotstyle = Style(name="plotstyle", family="chart")
  92. plotstyle.addElement( ChartProperties(seriessource="columns",
  93. percentage="false", stacked="false",
  94. threedimensional="true"))
  95. doc.automaticstyles.addElement(plotstyle)
  96. plotarea = chart.PlotArea(datasourcehaslabels="both", stylename=plotstyle)
  97. mychart.addElement(plotarea)
  98. # Style for the X,Y axes
  99. axisstyle = Style(name="axisstyle", family="chart")
  100. axisstyle.addElement( ChartProperties(displaylabel="true"))
  101. doc.automaticstyles.addElement(axisstyle)
  102. # Title for the X axis
  103. xaxis = chart.Axis(dimension="x", name="primary-x", stylename=axisstyle)
  104. plotarea.addElement(xaxis)
  105. xt = chart.Title()
  106. xaxis.addElement(xt)
  107. xt.addElement(text.P(text="x_axis"))
  108. # Title for the Y axis
  109. yaxis = chart.Axis(dimension="y", name="primary-y", stylename=axisstyle)
  110. plotarea.addElement(yaxis)
  111. yt = chart.Title()
  112. yaxis.addElement(yt)
  113. yt.addElement(text.P(text="y_axis"))
  114. result = doc.contentxml() # contentxml() is supposed to yeld a bytes
  115. self.assertNotEqual(-1, result.find(b'''style:family="chart"'''))
  116. if __name__ == '__main__':
  117. unittest.main()