示例#1
0
def write_root_rels(workbook):
    """Write the relationships xml."""
    root = Element('Relationships', xmlns=PKG_REL_NS)
    relation_tag = '{%s}Relationship' % PKG_REL_NS

    rel = Relationship(type="officeDocument", target=ARC_WORKBOOK, id="rId1")
    root.append(rel.to_tree())

    rel = Relationship(
        "",
        target=ARC_CORE,
        id='rId2',
    )
    rel.type = "%s/metadata/core-properties" % PKG_REL_NS
    root.append(rel.to_tree())

    rel = Relationship("extended-properties", target=ARC_APP, id='rId3')

    root.append(rel.to_tree())

    if workbook.vba_archive is not None:
        # See if there was a customUI relation and reuse its id
        arc = fromstring(workbook.vba_archive.read(ARC_ROOT_RELS))
        rels = arc.findall(relation_tag)
        rId = None
        for rel in rels:
            if rel.get('Target') == ARC_CUSTOM_UI:
                rId = rel.get('Id')
                break
        if rId is not None:
            vba = Relationship("", target=ARC_CUSTOM_UI, id=rId)
            vba.type = CUSTOMUI_NS
            root.append(vba.to_tree())

    return tostring(root)
示例#2
0
文件: workbook.py 项目: shobull/hue
def write_root_rels(workbook):
    """Write the relationships xml."""
    root = Element("Relationships", xmlns=PKG_REL_NS)
    relation_tag = "{%s}Relationship" % PKG_REL_NS

    rel = Relationship(type="officeDocument", target=ARC_WORKBOOK, id="rId1")
    root.append(rel.to_tree())

    rel = Relationship("", target=ARC_CORE, id="rId2")
    rel.type = "%s/metadata/core-properties" % PKG_REL_NS
    root.append(rel.to_tree())

    rel = Relationship("extended-properties", target=ARC_APP, id="rId3")

    root.append(rel.to_tree())

    if workbook.vba_archive is not None:
        # See if there was a customUI relation and reuse its id
        arc = fromstring(workbook.vba_archive.read(ARC_ROOT_RELS))
        rels = arc.findall(relation_tag)
        rId = None
        for rel in rels:
            if rel.get("Target") == ARC_CUSTOM_UI:
                rId = rel.get("Id")
                break
        if rId is not None:
            vba = Relationship("", target=ARC_CUSTOM_UI, id=rId)
            vba.type = CUSTOMUI_NS
            root.append(vba.to_tree())

    return tostring(root)
示例#3
0
def write_external_book_rel(book):
    """Serialise link to external file"""
    root = Element("Relationships", xmlns=PKG_REL_NS)
    rel = Relationship("", target=book.Target, targetMode=book.TargetMode, id="rId1")
    rel.type = book.Type
    root.append(rel.to_tree())
    return root
示例#4
0
def write_rels(worksheet, comments_id=None, vba_controls_id=None):
    """Write relationships for the worksheet to xml."""
    root = Element('Relationships', xmlns=PKG_REL_NS)
    rels = worksheet._rels

    # VBA
    if worksheet.vba_controls is not None:
        rel = Relationship("vmlDrawing",
                           id=worksheet.vba_controls,
                           target='/xl/drawings/vmlDrawing%s.vml' %
                           vba_controls_id)
        rels.append(rel)

    # Comments
    if worksheet._comment_count > 0:
        rel = Relationship(type="comments",
                           id="comments",
                           target='/xl/comments%s.xml' % comments_id)
        rels.append(rel)

        if worksheet.vba_controls is None:
            rel = Relationship(type="vmlDrawing",
                               id="commentsvml",
                               target='/xl/drawings/commentsDrawing%s.vml' %
                               comments_id)
            rels.append(rel)

    for idx, rel in enumerate(rels, 1):
        if rel.id is None:
            rel.id = "rId{0}".format(idx)
        root.append(rel.to_tree())

    return root
示例#5
0
def write_rels(worksheet, comments_id=None, vba_controls_id=None):
    """Write relationships for the worksheet to xml."""
    root = Element('Relationships', xmlns=PKG_REL_NS)
    rels = worksheet._rels

    # VBA
    if worksheet.vba_controls is not None:
        rel = Relationship("vmlDrawing", id=worksheet.vba_controls,
                           target='/xl/drawings/vmlDrawing%s.vml' % vba_controls_id)
        rels.append(rel)

    # Comments
    if worksheet._comment_count > 0:
        rel = Relationship(type="comments", id="comments",
                           target='/xl/comments%s.xml' % comments_id)
        rels.append(rel)

        if worksheet.vba_controls is None:
            rel = Relationship(type="vmlDrawing", id="commentsvml",
                           target='/xl/drawings/commentsDrawing%s.vml' % comments_id)
            rels.append(rel)

    for idx, rel in enumerate(rels, 1):
        if rel.id is None:
            rel.id = "rId{0}".format(idx)
        root.append(rel.to_tree())

    return root
示例#6
0
def write_external_book_rel(book):
    """Serialise link to external file"""
    root = Element("Relationships", xmlns=PKG_REL_NS)
    rel = Relationship("",
                       target=book.Target,
                       targetMode=book.TargetMode,
                       id="rId1")
    rel.type = book.Type
    root.append(rel.to_tree())
    return root
示例#7
0
def write_workbook_rels(workbook):
    """Write the workbook relationships xml."""
    root = Element('Relationships', xmlns=PKG_REL_NS)

    for i, _ in enumerate(workbook.worksheets, 1):
        rel = Relationship(type='worksheet', target='worksheets/sheet%s.xml' % i, id='rId%d' % i)
        root.append(rel.to_tree())

    i += 1
    strings =  Relationship(type='sharedStrings', target='sharedStrings.xml', id='rId%d' % i)
    root.append(strings.to_tree())

    i += 1
    styles =  Relationship(type='styles', target='styles.xml', id='rId%d' % i)
    root.append(styles.to_tree())

    i += 1
    styles =  Relationship(type='theme', target='theme/theme1.xml', id='rId%d' % i)
    root.append(styles.to_tree())

    if workbook.vba_archive:
        i += 1
        vba =  Relationship(type='vbaProject', target='vbaProject.bin', id='rId%d' % i)
        root.append(vba.to_tree())

    external_links = workbook._external_links
    if external_links:
        for idx, link in enumerate(external_links, 1):
            ext =  Relationship(type='externalLink',
                                target='externalLinks/externalLink%d.xml' % idx,
                                id='rId%d' % (i +idx))
            root.append(ext.to_tree())

    return tostring(root)
示例#8
0
文件: workbook.py 项目: shobull/hue
def write_workbook_rels(workbook):
    """Write the workbook relationships xml."""
    root = Element("Relationships", xmlns=PKG_REL_NS)

    for i, _ in enumerate(workbook.worksheets, 1):
        rel = Relationship(type="worksheet", target="worksheets/sheet%s.xml" % i, id="rId%d" % i)
        root.append(rel.to_tree())

    i += 1
    strings = Relationship(type="sharedStrings", target="sharedStrings.xml", id="rId%d" % i)
    root.append(strings.to_tree())

    i += 1
    styles = Relationship(type="styles", target="styles.xml", id="rId%d" % i)
    root.append(styles.to_tree())

    i += 1
    styles = Relationship(type="theme", target="theme/theme1.xml", id="rId%d" % i)
    root.append(styles.to_tree())

    if workbook.vba_archive:
        i += 1
        vba = Relationship(type="vbaProject", target="vbaProject.bin", id="rId%d" % i)
        vba.type = "http://schemas.microsoft.com/office/2006/relationships/vbaProject"
        root.append(vba.to_tree())

    external_links = workbook._external_links
    if external_links:
        for idx, link in enumerate(external_links, 1):
            ext = Relationship(
                type="externalLink", target="externalLinks/externalLink%d.xml" % idx, id="rId%d" % (i + idx)
            )
            root.append(ext.to_tree())

    return tostring(root)