Example #1
0
    def getStyle(self, c, cell, datastylename, style_id, odfdoc):
        """ get a style_name by style_id """

        if not style_id in self.styles.keys():
            # create new style
            cs = Style(name=cell, family="table-cell", datastylename=datastylename)
            cs.addElement(TextProperties(color=c.color, fontsize=c.font_size, fontfamily=c.font_family))

            # set backgound and borders
            if c.background_color != "default" and c.background_color != "transparent":
                cs.addElement(TableCellProperties(backgroundcolor=c.background_color))
            if c.border_top != "none":
                cs.addElement(TableCellProperties(bordertop=c.border_top))
            if c.border_bottom != "none":
                cs.addElement(TableCellProperties(borderbottom=c.border_bottom))
            if c.border_left != "none":
                cs.addElement(TableCellProperties(borderleft=c.border_left))
            if c.border_right != "none":
                cs.addElement(TableCellProperties(borderright=c.border_right))

            # set ods conditional style
            if c.condition:
                cns = Style(name="cns" + cell, family="table-cell")
                cns.addElement(TextProperties(color=c.condition_color))
                cns.addElement(TableCellProperties(backgroundcolor=c.condition_background_color))
                odfdoc.styles.addElement(cns)

                cs.addElement(Map(condition=c.condition, applystylename="cns" + cell))

            odfdoc.automaticstyles.addElement(cs)

            self.styles[style_id] = cell

        return self.styles[style_id]
Example #2
0
    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=u"-"))
            dcs.addElement(Month(style="long"))
            dcs.addElement(Text(text=u"-"))
            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=unicode(escape(c.text), "utf-8")))

                tr.addElement(tc)

        odfdoc.spreadsheet.addElement(table)
        odfdoc.save(filename)