Esempio n. 1
0
def add_ObjectN(N, metainf_manifest_xml_obj):
    """Add Object manifest:file-entry object to manifest.xml in META-INF of ods file"""

    NS = metainf_manifest_xml_obj.NS

    M = metainf_manifest_xml_obj.getroot()  # just to reduce line lengths

    # put subdirectory object into manifest
    obj_name = 'Object %i' % N
    attribD = {
        NS('manifest:full-path'): obj_name + '/',
        NS('manifest:media-type'): "application/vnd.oasis.opendocument.chart"
    }

    mfe = ET.Element(NS('manifest:file-entry'), attrib=attribD)
    M.insert(N, mfe)  # place in order

    # put xml files into subdirectory
    attribD = {
        NS('manifest:full-path'): obj_name + '/content.xml',
        NS('manifest:media-type'): "text/xml"
    }
    mfe = ET.Element(NS('manifest:file-entry'), attrib=attribD)
    M.insert(N * 3, mfe)  # place in order

    attribD = {
        NS('manifest:full-path'): obj_name + '/styles.xml',
        NS('manifest:media-type'): "text/xml"
    }
    mfe = ET.Element(NS('manifest:file-entry'), attrib=attribD)
    M.insert(N * 3, mfe)  # place in order
Esempio n. 2
0
    def new_elem(self, name, attribOD=None):
        """
        Create a new Element object.
        name format can be 'table:table' OR '{urn:oasis:names:tc:opendocument:xmlns:table:1.0}table'
        (i.e. can be path format OR tab format)
        attribOD is an OrderedDict in order to preserve attribute order.
        """

        tag = self.NS(name)
        #print( 'tag =',tag )

        if attribOD:
            OD = self.NS_attrib(attribOD)
            my_new_elem = ET.Element(tag, attrib=OD)
        else:
            my_new_elem = ET.Element(tag)

        sL = my_new_elem.tag.split('}')
        if len(sL) == 2:
            name = sL[1]
            uri = sL[0][1:]
            self.qnameOD[my_new_elem.tag] = '%s:%s' % (self.nsOD[uri], name)

        for qname, v in list(my_new_elem.attrib.items()):
            sL = qname.split('}')
            if len(sL) == 2:
                name = sL[1]
                uri = sL[0][1:]
                self.qnameOD[qname] = '%s:%s' % (self.nsOD[uri], name)

        return my_new_elem
Esempio n. 3
0
    def __init__(self,
                 plot_sheetname,
                 num_chart,
                 parent_obj,
                 excel_colors=True):
        """Inits SpreadSheet with filename and blank content.

        NS: cleans up the namespace callouts of the xml Element

        Attributes::

            plot_sheetname: name of data sheet

        """
        self.i_color = 0  # index into color chart for next curve
        self.i_prim_color = 0  # index into colorL
        self.i_sec_color = 0  # index into color2L

        self.i_symbol_type = 0  # index into SYMBOL_TYPE_LIST

        if excel_colors:
            self.color_list = EXCEL_COLOR_LIST
        else:
            self.color_list = BIG_COLOR_HEXSTR_LIST

        NS = parent_obj.NS

        # Start building new xml Element to be new Sheet in spreadsheet
        attribD = {NS('table:name'): plot_sheetname}
        newsheet = ET.Element(NS('table:table'), attrib=attribD)

        table_shapes = ET.Element(NS('table:shapes'))

        #print( 'table_shapes.text =',table_shapes.text )

        def NS_attrib(attD):
            D = {}
            for key, val in list(attD.items()):
                D[NS(key)] = val
            return D

        attribD = NS_attrib({
            'draw:z-index': "1",
            'draw:id': "id0",
            'draw:style-name': "a0",
            'draw:name': "Chart 1",
            'svg:x': "0in",
            'svg:y': "0in",
            'svg:width': "9.47551in",
            'svg:height': "6.88048in",
            'style:rel-width': "scale",
            'style:rel-height': "scale"
        })
        draw_frame = ET.Element(NS('draw:frame'), attrib=attribD)

        attribD = NS_attrib({
            'xlink:href': "Object %i/" % num_chart,
            'xlink:type': "simple",
            'xlink:show': "embed",
            'xlink:actuate': "onLoad"
        })
        draw_obj = ET.Element(NS('draw:object'), attrib=attribD)

        svg_title = ET.Element(NS('svg:title'))
        svg_desc = ET.Element(NS('svg:desc'))

        draw_frame.append(draw_obj)
        draw_frame.append(svg_title)
        draw_frame.append(svg_desc)
        table_shapes.append(draw_frame)

        tab_col = ET.Element(
            NS('table:table-column'),
            attrib={NS('table:number-columns-repeated'): "16384"})
        tab_row = ET.Element(
            NS('table:table-row'),
            attrib={NS('table:number-rows-repeated'): "1048576"})
        tab_cell = ET.Element(
            NS('table:table-cell'),
            attrib={NS('table:number-columns-repeated'): "16384"})
        tab_row.append(tab_cell)

        newsheet.append(table_shapes)
        newsheet.append(tab_col)
        newsheet.append(tab_row)

        # make sure any added Element objects are in nsOD, rev_nsOD and qnameOD of parent_obj
        def add_tag(tag):
            sL = tag.split('}')
            uri = sL[0][1:]
            name = sL[1]
            parent_obj.qnameOD[tag] = parent_obj.nsOD[uri] + ':' + name

        def add_tags(obj):
            if hasattr(obj, 'tag'):
                add_tag(obj.tag)
            if hasattr(obj, 'attrib'):
                for q, v in list(obj.attrib.items()):
                    add_tag(q)

        for parent in newsheet.iter():
            add_tags(parent)
            for child in parent.getchildren():
                add_tags(child)

        self.xmlSheetObj = newsheet

        self.plot_sheetname = ''
        self.data_sheetname = ''
        self.title = ''
        self.xlabel = ''
        self.ylabel = ''
        self.y2label = ''
        self.ycolL = None
        self.ycol2L = None

        self.logx = False
        self.logy = False
        self.log2y = False

        self.xcolL = []
        self.ycolDataSheetNameL = []

        self.xcol2L = []
        self.ycol2_DataSheetNameL = []

        self.showMarkerL = None
        self.showMarker2L = None
        self.markerTypeL = None
        self.markerType2L = None

        self.showLineL = None
        self.showLine2L = None

        self.lineThkL = None
        self.lineThk2L = None

        self.lineStyleL = None
        self.lineStyle2L = None
        self.set_of_line_styles = set()

        self.markerHtWdL = None
        self.markerHtWd2L = None

        self.showUnits = True

        self.colorL = None
        self.color2L = None

        self.labelL = None
        self.label2L = None
Esempio n. 4
0
    def __init__(self, data_sheetname, list_of_rows, parent_obj):
        """Inits SpreadSheet with filename and blank content.

        NS: cleans up the namespace callouts of the xml Element

        Attributes::

            data_sheetname: name of data sheet
            nrows: number of rows including label and units rows
            ncols: max number of cols
            labelL: list of labels
            unitsL: list of units

        """

        NS = parent_obj.NS

        self.data_sheetname = data_sheetname
        self.list_of_rows = list_of_rows

        # calc number of rows and columns in data
        self.nrows = len(list_of_rows)
        self.ncols = 0
        for row in list_of_rows:
            self.ncols = max(self.ncols, len(row))

        # make sure that row1 and row2 contain labels and units
        self.labelL = []
        self.unitsL = []
        for n in range(self.ncols):
            try:
                s = str(list_of_rows[0][n]).strip()
            except:
                s = 'Column %i' % (n + 1, )
            self.labelL.append(s)

            try:
                s = str(list_of_rows[1][n]).strip()
            except:
                s = ''
            self.unitsL.append(s)

        # Start building new xml Element to be new Sheet in spreadsheet
        #print( nsmap )
        attribD = {
            NS('table:name'): data_sheetname,
            NS('table:style-name'): "ta1"
        }
        newsheet = ET.Element(NS('table:table'), attrib=attribD)

        colD = {
            NS('table:style-name'): "co1",
            NS('table:number-columns-repeated'): "16384",
            NS('table:default-cell-style-name'): "ce1"
        }
        col_elm = ET.Element(NS('table:table-column'), attrib=colD)
        newsheet.append(col_elm)

        str_cellD = {
            NS('office:value-type'): "string",
            NS('table:style-name'): "ce1"
        }
        float_cellD = {
            NS('office:value-type'): "float",
            NS('table:style-name'): "ce1"
        }

        row_rep = 1048576  # max number of rows

        for row in list_of_rows:
            row_obj = ET.Element(NS('table:table-row'),
                                 attrib={NS('table:style-name'): "ro1"})

            col_rep = 16384  # max number of columns

            for value in row:
                try:
                    fval = float(value)
                    float_cellD[NS('office:value')] = str(fval)
                    D = float_cellD
                    text_p = "%g" % fval
                except:
                    D = str_cellD
                    text_p = "%s" % value

                cell_obj = ET.Element(NS('table:table-cell'), attrib=D)
                text_obj = ET.Element(NS('text:p'))
                text_obj.text = text_p
                cell_obj.append(text_obj)
                row_obj.append(cell_obj)
                col_rep -= 1  # decrement max available columns remaining

            last_cell_obj = ET.Element(
                NS('table:table-cell'),
                attrib={NS('table:number-columns-repeated'): "%i" % col_rep})
            row_obj.append(last_cell_obj)
            newsheet.append(row_obj)
            row_rep -= 1  # decrement max remaining number of rows

        last_row_obj = ET.Element(NS('table:table-row'),
                                  attrib={
                                      NS('table:style-name'):
                                      "ro1",
                                      NS('table:number-rows-repeated'):
                                      "%i" % row_rep
                                  })
        last_row_obj.append(
            ET.Element(NS('table:table-cell'),
                       attrib={NS('table:number-columns-repeated'): "16384"}))

        newsheet.append(last_row_obj)

        # make sure any added Element objects are in nsOD, rev_nsOD and qnameOD of parent_obj

        # make sure any added Element objects are in nsOD, rev_nsOD and qnameOD of parent_obj
        def add_tag(tag):
            sL = tag.split('}')
            uri = sL[0][1:]
            name = sL[1]
            parent_obj.qnameOD[tag] = parent_obj.nsOD[uri] + ':' + name

        def add_tags(obj):
            if hasattr(obj, 'tag'):
                add_tag(obj.tag)
            if hasattr(obj, 'attrib'):
                for q, v in list(obj.attrib.items()):
                    add_tag(q)

        for parent in newsheet.iter():
            add_tags(parent)
            for child in parent.getchildren():
                add_tags(child)

        self.xmlSheetObj = newsheet