Beispiel #1
0
    def write(self, root):
        # Remove any existing comment shapes
        if not hasattr(root, "findall"):
            root = Element("xml")

        comments = root.findall("{%s}shape" % vmlns)
        for c in comments:
            if c.get("type") == '#_x0000_t202':
                root.remove(c)

        # check whether comments shape type already exists
        shape_types = root.findall("{%s}shapetype" % vmlns)
        comments_type = False
        for s in shape_types:
            if s.get("id") == '_x0000_t202':
                comments_type = True
                break

        if not comments_type:
            self.add_comment_shapetype(root)

        for idx, (coord, comment) in enumerate(self.comments, 1026):
            self.add_comment_shape(root, idx, coord, comment)

        return tostring(root)
Beispiel #2
0
    def write(self, root):
        # Remove any existing comment shapes
        if not hasattr(root, "findall"):
            root = Element("xml")

        comments = root.findall("{%s}shape" % vmlns)
        for c in comments:
            if c.get("type") == '#_x0000_t202':
                root.remove(c)

        # check whether comments shape type already exists
        shape_types = root.findall("{%s}shapetype" % vmlns)
        comments_type = False
        for s in shape_types:
            if s.get("id") == '_x0000_t202':
                comments_type = True
                break

        if not comments_type:
            self.add_comment_shapetype(root)

        for idx, (coord, comment) in enumerate(self.comments, 1026):
            self.add_comment_shape(root, idx, coord)

        return tostring(root)
Beispiel #3
0
    def write(self, root):

        if not hasattr(root, "findall"):
            root = Element("xml")
        # Remove any existing comment shapes
        comments = root.findall("{%s}shape[@type='#_x0000_t202']" % vmlns)
        for c in comments:
            root.remove(c)

        # check whether comments shape type already exists

        shape_types = root.find("{%s}shapetype[@id='_x0000_t202']" % vmlns)
        if not shape_types:
            self.add_comment_shapetype(root)

        for idx, (coord, comment) in enumerate(self.comments, 1026):
            self.add_comment_shape(root, idx, coord)

        return tostring(root)
Beispiel #4
0
    def write(self, root):

        if not hasattr(root, "findall"):
            root = Element("xml")

        # Remove any existing comment shapes
        comments = root.findall("{%s}shape[@type='#_x0000_t202']" % vmlns)
        for c in comments:
            root.remove(c)

        # check whether comments shape type already exists
        shape_types = root.find("{%s}shapetype[@id='_x0000_t202']" % vmlns)
        if not shape_types:
            self.add_comment_shapetype(root)

        for idx, (coord, comment) in enumerate(self.comments, 1026):
            self.add_comment_shape(root, idx, coord, comment.height, comment.width)

        return tostring(root)
Beispiel #5
0
def write_content_types(workbook, as_template=False):
    """Write the content-types xml."""

    seen = set()
    if workbook.vba_archive:
        root = fromstring(workbook.vba_archive.read(ARC_CONTENT_TYPES))
        for elem in root.findall('{%s}Override' % CONTYPES_NS):
            seen.add(elem.attrib['PartName'])
        for elem in root.findall('{%s}Default' % CONTYPES_NS):
            seen.add(elem.attrib['Extension'])
    else:
        if LXML:
            NSMAP = {None: CONTYPES_NS}
            root = Element('{%s}Types' % CONTYPES_NS, nsmap=NSMAP)
        else:
            root = Element('{%s}Types' % CONTYPES_NS)

    for setting_type, name, content_type in static_content_types_config:

        attrib = {'ContentType': content_type}
        if setting_type == 'Override':
            if '/' + name not in seen:
                tag = '{%s}Override' % CONTYPES_NS
                attrib['PartName'] = '/' + name
                SubElement(root, tag, attrib)
        else:
            if name not in seen:
                tag = '{%s}Default' % CONTYPES_NS
                attrib['Extension'] = name
                SubElement(root, tag, attrib)

    nodes = root.findall('{%s}Override' % CONTYPES_NS)
    for wb_elem in nodes:
        if wb_elem.get("PartName") == "/" + ARC_WORKBOOK:
            ct = as_template and XLTX or XLSX
            if workbook.vba_archive:
                ct = as_template and XLTM or XLSM
            wb_elem.set("ContentType", ct)

    drawing_id = 1
    chart_id = 1
    comments_id = 1

    for sheet_id, sheet in enumerate(workbook.worksheets):
        name = '/xl/worksheets/sheet%d.xml' % (sheet_id + 1)
        if name not in seen:
            SubElement(root, '{%s}Override' % CONTYPES_NS, {
                'PartName': name,
                'ContentType': WORKSHEET_TYPE
            })

        if sheet._charts or sheet._images:
            name = '/xl/drawings/drawing%d.xml' % drawing_id
            if name not in seen:
                SubElement(root, '{%s}Override' % CONTYPES_NS, {
                    'PartName': name,
                    'ContentType': DRAWING_TYPE
                })

            drawing_id += 1

            for chart in sheet._charts:
                name = '/xl/charts/chart%d.xml' % chart_id
                if name not in seen:
                    SubElement(root, '{%s}Override' % CONTYPES_NS, {
                        'PartName': name,
                        'ContentType': CHART_TYPE
                    })

                chart_id += 1

        if sheet._comment_count > 0:
            name = '/xl/comments%d.xml' % comments_id
            if name not in seen:
                SubElement(root, '{%s}Override' % CONTYPES_NS, {
                    'PartName': name,
                    'ContentType': COMMENTS_TYPE
                })
            comments_id += 1

    for idx, _ in enumerate(workbook._external_links, 1):
        el = Element(
            '{%s}Override' % CONTYPES_NS, {
                'PartName':
                '/xl/externalLinks/externalLink{0}.xml'.format(idx),
                'ContentType': EXTERNAL_LINK
            })
        root.append(el)

    return tostring(root)
Beispiel #6
0
def write_content_types(workbook, as_template=False):
    """Write the content-types xml."""

    seen = set()
    if workbook.vba_archive:
        root = fromstring(workbook.vba_archive.read(ARC_CONTENT_TYPES))
        for elem in root.findall('{%s}Override' % CONTYPES_NS):
            seen.add(elem.attrib['PartName'])
        for elem in root.findall('{%s}Default' % CONTYPES_NS):
            seen.add(elem.attrib['Extension'])
    else:
        if LXML:
            NSMAP = {None : CONTYPES_NS}
            root = Element('{%s}Types' % CONTYPES_NS, nsmap=NSMAP)
        else:
            root = Element('{%s}Types' % CONTYPES_NS)

    for setting_type, name, content_type in static_content_types_config:

        attrib = {'ContentType': content_type}
        if setting_type == 'Override':
            if '/' + name not in seen:
                tag = '{%s}Override' % CONTYPES_NS
                attrib['PartName'] = '/' + name
                SubElement(root, tag, attrib)
        else:
            if name not in seen:
                tag = '{%s}Default' % CONTYPES_NS
                attrib['Extension'] = name
                SubElement(root, tag, attrib)

    nodes = root.findall('{%s}Override' % CONTYPES_NS)
    for wb_elem in nodes:
        if wb_elem.get("PartName") == "/" + ARC_WORKBOOK:
            ct = as_template and XLTX or XLSX
            if workbook.vba_archive:
                ct = as_template and XLTM or XLSM
            wb_elem.set("ContentType", ct)

    drawing_id = 1
    chart_id = 1
    comments_id = 1

    for sheet_id, sheet in enumerate(workbook.worksheets):
        name = '/xl/worksheets/sheet%d.xml' % (sheet_id + 1)
        if name not in seen:
            SubElement(root, '{%s}Override' % CONTYPES_NS, {
                'PartName': name,
                'ContentType': WORKSHEET_TYPE
            })

        if sheet._charts or sheet._images:
            name = '/xl/drawings/drawing%d.xml' % drawing_id
            if name not in seen:
                SubElement(root, '{%s}Override' % CONTYPES_NS, {
                    'PartName': name,
                    'ContentType': DRAWING_TYPE
                })

            drawing_id += 1

            for chart in sheet._charts:
                name = '/xl/charts/chart%d.xml' % chart_id
                if name not in seen:
                    SubElement(root, '{%s}Override' % CONTYPES_NS, {
                        'PartName': name,
                        'ContentType': CHART_TYPE
                    })

                chart_id += 1

                if chart._shapes:
                    name = '/xl/drawings/drawing%d.xml' % drawing_id
                    if name not in seen:
                        SubElement(root, '{%s}Override' % CONTYPES_NS, {
                            'PartName': name,
                            'ContentType': CHARTSHAPE_TYPE
                        })

                    drawing_id += 1

        if sheet._comment_count > 0:
            name = '/xl/comments%d.xml' % comments_id
            if name not in seen:
                SubElement(root, '{%s}Override' % CONTYPES_NS, {
                    'PartName': name,
                    'ContentType': COMMENTS_TYPE
                })
            comments_id += 1

    for idx, _ in enumerate(workbook._external_links, 1):
        el = Element('{%s}Override' % CONTYPES_NS, {
            'PartName': '/xl/externalLinks/externalLink{0}.xml'.format(idx),
            'ContentType': EXTERNAL_LINK
        })
        root.append(el)

    return tostring(root)