예제 #1
0
    def _load(self):
        self.document = WordprocessingDocument(path=None)
        package = self.document.package
        document_part = package.create_part(
            uri='/word/document.xml',
        )

        if self.relationships:
            for relationship in self.relationships:
                target_mode = 'Internal'
                if relationship['external']:
                    target_mode = 'External'
                target_uri = relationship['target_path']
                if 'data' in relationship:
                    full_target_uri = posixpath.join(
                        package.uri,
                        'word',
                        target_uri,
                    )
                    package.streams[full_target_uri] = BytesIO(
                        relationship['data'],
                    )
                    package.create_part(uri=full_target_uri)
                document_part.create_relationship(
                    target_uri=target_uri,
                    target_mode=target_mode,
                    relationship_type=relationship['relationship_type'],
                    relationship_id=relationship['relationship_id'],
                )

        package.streams[document_part.uri] = BytesIO(self.document_xml)
        package.create_relationship(
            target_uri=document_part.uri,
            target_mode='Internal',
            relationship_type=MainDocumentPart.relationship_type,
        )

        self.numbering_root = None
        if self.numbering_dict is not None:
            self.numbering_root = parse_xml_from_string(
                DXB.numbering(self.numbering_dict),
            )

        # This is the standard page width for a word document (in points), Also
        # the page width that we are looking for in the test.
        self.page_width = 612

        self.parse_begin(self.document.main_document_part.root_element)
예제 #2
0
    def _load(self):
        self.document = WordprocessingDocument(path=self.path)
        main_document_part = self.document.main_document_part
        if main_document_part is None:
            raise MalformedDocxException

        self.numbering_root = None
        numbering_part = main_document_part.numbering_definitions_part
        if numbering_part:
            self.numbering_root = numbering_part.root_element

        self.page_width = self._get_page_width(main_document_part.root_element)
        self.styles_manager = StylesManager(
            main_document_part.style_definitions_part, )
        self.styles = self.styles_manager.styles
        self.parse_begin(main_document_part)
예제 #3
0
파일: DocxParser.py 프로젝트: 07pack/pydocx
    def _load(self):
        self.document = WordprocessingDocument(path=self.path)
        main_document_part = self.document.main_document_part
        if main_document_part is None:
            raise MalformedDocxException

        self.root_element = main_document_part.root_element
        self.numbering_root = None
        numbering_part = main_document_part.numbering_definitions_part
        if numbering_part:
            self.numbering_root = numbering_part.root_element

        pgSzEl = find_first(self.root_element, 'pgSz')
        if pgSzEl is not None:
            # pgSz is defined in twips, convert to points
            pgSz = int(pgSzEl.attrib['w'])
            self.page_width = pgSz / TWIPS_PER_POINT

        self.styles_dict = self._parse_styles()
        self.parse_begin(self.root_element)
예제 #4
0
 def test_image_parts(self):
     image_document = WordprocessingDocument(
         path='pydocx/fixtures/has_image.docx', )
     parts = image_document.main_document_part.image_parts
     self.assertEqual(len(parts), 1)
     self.assertEqual(parts[0].uri, '/word/media/image1.gif')
예제 #5
0
 def setUp(self):
     self.document = WordprocessingDocument(
         path='pydocx/fixtures/no_break_hyphen.docx', )