Example #1
0
    def it_can_iterate_parts_related_by_reltypes(self, rel_types_fixture,
                                                 rels_prop_):
        rels_, reltypes, expected_parts = rel_types_fixture
        rels_prop_.return_value = rels_
        part = Part(None, None)

        parts = set(part.iter_parts_related_by(reltypes))

        assert parts == expected_parts
Example #2
0
 def _update_part(self):
     if self.part is None:
         # Create a new part for custom properties
         partname = PackURI('/docProps/custom.xml')
         self.part = Part(partname, CT.OFC_CUSTOM_PROPERTIES,
                          serialize_part_xml(self._element),
                          self.doc.part.package)
         self.doc.part.package.relate_to(self.part, RT.CUSTOM_PROPERTIES)
         self._element = parse_xml(self.part.blob)
     else:
         self.part._blob = serialize_part_xml(self._element)
Example #3
0
 def footnote_part(self):
     """The footnote part of the document."""
     try:
         footnote_part = self.doc.part.rels.part_with_reltype(RT.FOOTNOTES)
     except KeyError:
         # Create a new empty footnotes part
         partname = PackURI('/word/footnotes.xml')
         content_type = CT.WML_FOOTNOTES
         xml_path = os.path.join(os.path.dirname(__file__), 'templates',
                                 'footnotes.xml')
         with open(xml_path, 'rb') as f:
             xml_bytes = f.read()
         footnote_part = Part(partname, content_type, xml_bytes,
                              self.doc.part.package)
         self.doc.part.relate_to(footnote_part, RT.FOOTNOTES)
     return footnote_part
Example #4
0
 def footer_part(self, content=None):
     """The footer part of the document."""
     footer_rels = [
         rel for rel in self.doc.part.rels.values() if rel.reltype == RT.FOOTER]
     next_id = len(footer_rels) + 1
     # Create a new header part
     partname = PackURI('/word/footer%s.xml' % next_id)
     content_type = CT.WML_FOOTER
     if not content:
         xml_path = os.path.join(
             os.path.dirname(__file__), 'templates', 'footer.xml')
         with open(xml_path, 'rb') as f:
             content = f.read()
     footer_part = Part(
         partname, content_type, content, self.doc.part.package)
     self.doc.part.relate_to(footer_part, RT.FOOTER)
     return footer_part
Example #5
0
    def add_relationship(self, src_part, dst_part, relationship):
        """Add relationship and it's target part"""
        if relationship.is_external:
            new_rid = dst_part.rels.get_or_add_ext_rel(relationship.reltype,
                                                       relationship.target_ref)
            return dst_part.rels[new_rid]

        part = relationship.target_part

        # Determine next partname
        name = FILENAME_IDX_RE.match(part.partname).group(1)
        used_part_numbers = [
            FILENAME_IDX_RE.match(p.partname).group(2)
            for p in dst_part.package.iter_parts()
            if p.partname.startswith(name)
        ]
        used_part_numbers = [
            int(idx) for idx in used_part_numbers if idx is not None
        ]

        for n in range(1, len(used_part_numbers) + 2):
            if n not in used_part_numbers:
                next_part_number = n
                break
        next_partname = PackURI('%s%d.%s' %
                                (name, next_part_number, part.partname.ext))

        new_part = Part(next_partname, part.content_type, part.blob,
                        dst_part.package)
        new_rel = dst_part.rels.get_or_add(relationship.reltype, new_part)

        # Sort relationships by rId to get the same rId when adding them to the
        # new part. This avoids fixing references.
        def sort_key(r):
            match = RID_IDX_RE.match(r.rId)
            return int(match.group(1))

        for rel in sorted(part.rels.values(), key=sort_key):
            self.add_relationship(part, new_part, rel)

        return new_rel
Example #6
0
 def partname_set_fixture(self):
     old_partname = PackURI("/old/part/name")
     new_partname = PackURI("/new/part/name")
     part = Part(old_partname, None, None, None)
     return part, new_partname
Example #7
0
 def partname_get_fixture(self):
     partname = PackURI("/part/name")
     part = Part(partname, None, None, None)
     return part, partname
Example #8
0
 def part(self):
     part = Part(None, None)
     return part
Example #9
0
 def package_get_fixture(self, package_):
     part = Part(None, None, None, package_)
     return part, package_
Example #10
0
 def content_type_fixture(self):
     content_type = "content/type"
     part = Part(None, content_type, None, None)
     return part, content_type
Example #11
0
 def blob_fixture(self, blob_):
     part = Part(None, None, blob_, None)
     return part, blob_
Example #12
0
 def part(self):
     return Part(None, None)
Example #13
0
 def rels_fixture(self, Relationships_, partname_, rels_):
     part = Part(partname_, None)
     return part, Relationships_, partname_, rels_