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)
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)
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)
def setUp(self): self.document = WordprocessingDocument( path='pydocx/fixtures/no_break_hyphen.docx', )
class WordprocessingDocumentTestCase(unittest.TestCase): def setUp(self): self.document = WordprocessingDocument( path='pydocx/fixtures/no_break_hyphen.docx', ) def test_document_is_the_open_xml_package(self): self.assertEqual( self.document, self.document.main_document_part.open_xml_package, ) def test_get_parts_of_type_office_document(self): self.assertEqual( self.document.get_parts_of_type( MainDocumentPart.relationship_type, )[0], self.document.main_document_part, ) def test_main_document_part_uri(self): self.assertEqual( self.document.main_document_part.uri, '/word/document.xml', ) def test_main_document_part_root(self): namespace, tag = xml_tag_split( self.document.main_document_part.root_element.tag, ) # We're currently ignoring namespaces, so this comes back as none self.assertEqual(namespace, None) self.assertEqual(tag, 'document') def test_main_document_part_relationship_uri(self): part = self.document.package.get_part( self.document.main_document_part.uri, ) self.assertEqual( part.relationship_uri, '/word/_rels/document.xml.rels', ) def test_main_document_part_styles_uri(self): styles = self.document.main_document_part.style_definitions_part self.assertEqual( styles.uri, '/word/styles.xml', ) def test_main_document_part_font_table_uri(self): font_table = self.document.main_document_part.font_table_part self.assertEqual( font_table.uri, '/word/fontTable.xml', ) def test_nonexistent_part(self): part = self.document.main_document_part.numbering_definitions_part self.assertEqual(part, None) 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')
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')