Esempio n. 1
0
    def load(self, filename):
        """ load a table in ods format """

        # read the file
        doc = load(filename)

        # get table style
        self.table.direction = "ltr"
        try:
            stylename = doc.getElementsByType(Table)[0].getAttribute("stylename")
            style = doc.getStyleByName(stylename)
            tps = style.getElementsByType(TableProperties)
            for tp in tps:
                if tp.getAttribute("writingmode") == "rl-tb":
                    self.table.direction = "rtl"
        except:
            pass

        # loop on all rows and columns
        i = 0
        for row in doc.getElementsByType(TableRow):
            j = 1
            i += 1
            for cell in row.getElementsByType(TableCell):

                # creat a new cell
                c = SodsCell()

                try:
                    numbercolumnsrepeated = int(cell.getAttribute("numbercolumnsrepeated"))
                except:
                    numbercolumnsrepeated = 1

                c.text = self.getCellText(cell)

                c.value_type = cell.getAttribute("valuetype")

                # FIXME: no percentage support, convert to float
                if c.value_type == "percentage":
                    c.value_type = "float"

                c.formula = cell.getAttribute("formula")
                if c.formula:
                    c.formula = self.cleanFormula(c.formula[3:])
                c.date_value = cell.getAttribute("datevalue")
                c.value = cell.getAttribute("value")

                stylename = cell.getAttribute("stylename")
                if stylename:
                    style = doc.getStyleByName(stylename)

                    # parse data style
                    # this only work on patch'd version of odfpy
                    # file:opendocument.py, function:build_caches
                    # -if element.qname == (STYLENS, u'style')):
                    # +if element.qname in ((STYLENS, u'style'), (NUMBERNS,u'number-style')):
                    datastylename = style.getAttribute("datastylename")
                    if datastylename:
                        datastyle = doc.getStyleByName(datastylename)
                        if datastyle:
                            p = datastyle.getElementsByType(Number)[0]
                            if p:
                                decimalplaces = p.getAttribute("decimalplaces")
                                grouping = p.getAttribute("grouping")

                                if decimalplaces == "2" and grouping == "true":
                                    c.format = "#,##0.00"
                                else:
                                    c.format = ""

                    for p in style.getElementsByType(TextProperties):
                        c.font_family = p.getAttribute("fontfamily")
                        if not c.font_family:
                            c.font_family = "Arial"
                        c.font_size = self.translateToPt(p.getAttribute("fontsize"))
                        c.color = p.getAttribute("color")
                        if not c.color:
                            c.color = "#000000"

                    for p in style.getElementsByType(TableCellProperties):

                        c.background_color = p.getAttribute("backgroundcolor")
                        if not c.background_color or c.background_color == "transparent":
                            c.background_color = "default"
                        border = self.translateBorderToPt(p.getAttribute("border"))
                        if border == "none":
                            c.border_top = self.translateBorderToPt(p.getAttribute("bordertop"))
                            c.border_bottom = self.translateBorderToPt(p.getAttribute("borderbottom"))
                            c.border_left = self.translateBorderToPt(p.getAttribute("borderleft"))
                            c.border_right = self.translateBorderToPt(p.getAttribute("borderright"))
                        else:
                            c.border_top = border
                            c.border_bottom = border
                            c.border_left = border
                            c.border_right = border

                    for p in style.getElementsByType(Map):
                        c.condition = p.getAttribute("condition")
                        if c.condition:
                            c.condition = self.cleanFormula(c.condition)

                        applystylename = p.getAttribute("applystylename")
                        applystyle = doc.getStyleByName(applystylename)

                        for cp in applystyle.getElementsByType(TextProperties):
                            c.condition_color = cp.getAttribute("color")

                        for cp in applystyle.getElementsByType(TableCellProperties):
                            c.condition_background_color = cp.getAttribute("backgroundcolor")

                # check for sodsOds formulas (starting with !)
                if len(c.text) > 0 and c.text[0] == "!":
                    c.formula = c.text
                    c.value_type = "float"

                # insert cell to table
                while numbercolumnsrepeated > 0:
                    self.table.setCellAt(i, j, c)
                    j += 1
                    numbercolumnsrepeated -= 1
	def importXml(self, xml_text):
		''' load cells from text in xml format '''
		
		# get the cells elements from our xml file
		xml_table = ElementTree.XML(xml_text)
		
		# loop on all the cells in xml file
		for xml_cell in xml_table:
			# FIXME: we assume that all the cell element are in the right
			# order
			
			# get i, j
			i, j = int(xml_cell[0].text), int(xml_cell[1].text)
			
			# get cell
			c = SodsCell()
			
			c.color = xml_cell[2].text
			c.font_family = xml_cell[3].text
			c.font_size = xml_cell[4].text
			
			c.background_color = xml_cell[5].text
			c.border_top = xml_cell[6].text
			c.border_bottom = xml_cell[7].text
			c.border_left = xml_cell[8].text
			c.border_right = xml_cell[9].text
	
			if xml_cell[10].text:
				c.text = unescape(xml_cell[10].text)
			c.value_type = xml_cell[11].text
			c.value = [None, xml_cell[12].text][xml_cell[12].text != 'None']
			c.formula = [None, unescape(xml_cell[13].text)][xml_cell[13].text != 'None']
			c.date_value = [None, xml_cell[14].text][xml_cell[14].text != 'None']
	
			c.condition = [None, unescape(xml_cell[15].text)][xml_cell[15].text != 'None']
			c.condition_state = eval(xml_cell[16].text)
			c.condition_color = xml_cell[17].text
			c.condition_background_color = xml_cell[18].text
			
			text_align = xml_cell[19].text
			c.column_width = xml_cell[20].text
			c.format = [None, unescape(xml_cell[21].text)][xml_cell[21].text != 'None']
			
			# insert cell to table
			self.table.setCellAt(i, j, c)