def test_percentage(self): """ Test that an automatic style can refer to a PercentageStyle as a datastylename """ doc = OpenDocumentSpreadsheet() nonze = PercentageStyle(name='N11') nonze.addElement(Number(decimalplaces='2', minintegerdigits='1')) nonze.addElement(Text(text='%')) doc.automaticstyles.addElement(nonze) pourcent = Style(name='pourcent', family='table-cell', datastylename='N11') pourcent.addElement(ParagraphProperties(textalign='center')) pourcent.addElement( TextProperties(attributes={ 'fontsize': "10pt", 'fontweight': "bold", 'color': "#000000" })) doc.automaticstyles.addElement(pourcent) table = Table(name='sheet1') tr = TableRow() tc = TableCell(formula='=AVERAGE(C4:CB62)/2', stylename='pourcent', valuetype='percentage') tr.addElement(tc) table.addElement(tr) doc.spreadsheet.addElement(table) doc.save(u"TEST.ods") self.saved = True d = load(u"TEST.ods") result = d.contentxml() # contentxml is supposed to yeld a bytes self.assertNotEqual(-1, result.find(b'''<number:percentage-style''')) self.assertNotEqual(-1, result.find(b'''style:data-style-name="N11"''')) self.assertNotEqual(-1, result.find(b'''style:name="pourcent"'''))
widewidth = Style(name="co1", family="table-column") widewidth.addElement( TableColumnProperties(columnwidth="2.8cm", breakbefore="auto")) textdoc.automaticstyles.addElement(widewidth) # Create the styles for $AUD format currency values ns1 = CurrencyStyle(name="positive-AUD", volatile="true") ns1.addElement(CurrencySymbol(language="en", country="AU", text=u"$")) ns1.addElement(Number(decimalplaces="2", minintegerdigits="1", grouping="true")) textdoc.styles.addElement(ns1) # Create the main style. ns2 = CurrencyStyle(name="main-AUD") ns2.addElement(TextProperties(color="#ff0000")) ns2.addElement(Text(text=u"-")) ns2.addElement(CurrencySymbol(language="en", country="AU", text=u"$")) ns2.addElement(Number(decimalplaces="2", minintegerdigits="1", grouping="true")) ns2.addElement(Map(condition="value()>=0", applystylename="positive-AUD")) textdoc.styles.addElement(ns2) # Create automatic style for the price cells. moneycontents = Style(name="ce1", family="table-cell", parentstylename=tablecontents, datastylename="main-AUD") textdoc.automaticstyles.addElement(moneycontents) # Start the table, and describe the columns table = Table(name="Currency colours")
def save(self, filename, i_max = None, j_max = None): ''' save table in ods format ''' if not i_max: i_max = self.table.i_max if not j_max: j_max = self.table.j_max # update cells text self.table.updateTable(i_max, j_max) # create new odf spreadsheet odfdoc = OpenDocumentSpreadsheet() # set direction style rtl = Style(name = "dir", family = "table") if self.table.direction == 'rtl': rtl.addElement(TableProperties(writingmode="rl-tb")) odfdoc.automaticstyles.addElement(rtl) # create the table table = Table(name = "sheet 1", stylename = 'dir') # default style ts = Style(name = "ts", family = "table-cell") ts.addElement(TextProperties(fontfamily = SodsCell().font_family, fontsize = SodsCell().font_size)) odfdoc.styles.addElement(ts) # create columns for j in range(1, j_max): colname = "col" + str(j) c = self.table.getCellAt(0, j) width = c.column_width cs = Style(name = colname, family = "table-column") cs.addElement(TableColumnProperties(columnwidth = width, breakbefore = "auto")) odfdoc.automaticstyles.addElement(cs) table.addElement(TableColumn(stylename = colname, defaultcellstylename = "ts")) # make sure values are up to date # loop and update the cells value for i in range(1, i_max): # create new ods row tr = TableRow() table.addElement(tr) # create default data styles for dates and numbers ncs = NumberStyle(name="ncs") ncs.addElement(Number(decimalplaces="2", minintegerdigits="1", grouping="true")) odfdoc.styles.addElement(ncs) ncs2 = NumberStyle(name="ncs2") ncs2.addElement(Number(decimalplaces="0", minintegerdigits="1", grouping="false")) odfdoc.styles.addElement(ncs2) dcs = DateStyle(name="dcs") dcs.addElement(Year(style='long')) dcs.addElement(Text(text = '-')) dcs.addElement(Month(style='long')) dcs.addElement(Text(text = '-')) dcs.addElement(Day(style='long')) odfdoc.styles.addElement(dcs) for j in range(1, j_max): # update the cell text and condition cell = self.table.encodeColName(j) + str(i) c = self.table.getCellAt(i, j) # chose datastylename if c.value_type == 'date': datastylename = "dcs" else: if c.format == "": datastylename = "ncs2" if c.format == "#,##0.00": datastylename = "ncs" # get cell style id if (c.condition): style_id = (datastylename + c.color + c.font_size + c.font_family + c.background_color + c.border_top + c.border_bottom + c.border_left + c.border_right + c.condition_color + c.condition_background_color) else: style_id = (datastylename + c.color + c.font_size + c.font_family + c.background_color + c.border_top + c.border_bottom + c.border_left + c.border_right) # set ods style style_name = self.getStyle(c, cell, datastylename, style_id, odfdoc) # create new ods cell if (c.formula and c.formula[0] == '=' and c.formula[:4] != '=uni'): if self.table.isFloat(c.value): tc = TableCell(valuetype = c.value_type, formula = c.formula, value = float(c.value), stylename = style_name) else: tc = TableCell(valuetype = c.value_type, formula = c.formula, value = 0, stylename = style_name) elif (c.value_type == 'date'): tc = TableCell(valuetype = c.value_type, datevalue = c.date_value, stylename = style_name) elif (c.value_type == 'float') and self.table.isFloat(c.value): tc = TableCell(valuetype = c.value_type, value = float(c.value), stylename = style_name) else: tc = TableCell(valuetype = 'string', stylename = style_name) # set ods text tc.addElement(P(text = str(escape(c.text), 'utf-8'))) tr.addElement(tc) odfdoc.spreadsheet.addElement(table) odfdoc.save(filename)