def test_document_property_is_a_Document_instance(self): factory = WordprocessingDocumentFactory() factory.add(MainDocumentPart, '') package = create_zip_archive(factory.to_zip_dict()) # TODO the interface for creating a new WordprocessingDocument sucks document = WordprocessingDocument(path=package) part = document.main_document_part assert isinstance(part.document, Document), part.document
def test_numbering_property_is_a_Numbering_instance(self): factory = WordprocessingDocumentFactory() factory.add(NumberingDefinitionsPart, '') factory.add(MainDocumentPart, '') package = create_zip_archive(factory.to_zip_dict()) # TODO the interface for creating a new WordprocessingDocument sucks document = WordprocessingDocument(path=package) part = document.main_document_part.numbering_definitions_part assert isinstance(part.numbering, Numbering), part.numbering
def load_document(self): # It's likely that we could replace this logic with a # WordprocessingDocumentFactory document = WordprocessingDocument(path=None) package = document.package document_part = package.create_part(uri='/word/document.xml', ) if self.numbering_dict is not None: numbering_xml = DXB.numbering(self.numbering_dict) self.relationships.append({ 'external': False, 'target_path': 'numbering.xml', 'data': numbering_xml, 'relationship_id': 'numbering', 'relationship_type': NumberingDefinitionsPart.relationship_type, # noqa }) if self.styles_xml: self.relationships.append({ 'external': False, 'target_path': 'styles.xml', 'data': self.styles_xml, 'relationship_id': 'styles', 'relationship_type': StyleDefinitionsPart.relationship_type, }) 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, ) # 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 return document
def load_document(self): self.document = WordprocessingDocument(path=self.path) return self.document
def test_image_parts(self): image_document = WordprocessingDocument( path='tests/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 setUp(self): self.document = WordprocessingDocument( path='tests/fixtures/no_break_hyphen.docx', )
class WordprocessingDocumentTestCase(unittest.TestCase): def setUp(self): self.document = WordprocessingDocument( path='tests/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_numbering_definitions_part(self): part = self.document.main_document_part.numbering_definitions_part self.assertNotEqual(part, None) self.assertEqual(part.root_element, None) def test_image_parts(self): image_document = WordprocessingDocument( path='tests/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')