def it_can_start_a_bookmark(self, _body_prop_, body_, bookmark_): _body_prop_.return_value = body_ body_.start_bookmark.return_value = bookmark_ document = Document(None, None) bookmark = document.start_bookmark("foobar") body_.start_bookmark.assert_called_once_with("foobar") assert bookmark is bookmark_
def it_can_add_a_heading(self, add_heading_fixture, add_paragraph_, paragraph_): level, style = add_heading_fixture add_paragraph_.return_value = paragraph_ document = Document(None, None) paragraph = document.add_heading("Spam vs. Bacon", level) add_paragraph_.assert_called_once_with(document, "Spam vs. Bacon", style) assert paragraph is paragraph_
def it_can_add_a_page_break(self, add_paragraph_, paragraph_, run_): add_paragraph_.return_value = paragraph_ paragraph_.add_run.return_value = run_ document = Document(None, None) paragraph = document.add_page_break() add_paragraph_.assert_called_once_with(document) paragraph_.add_run.assert_called_once_with() run_.add_break.assert_called_once_with(WD_BREAK.PAGE) assert paragraph is paragraph_
def it_can_add_a_section( self, add_section_fixture, Section_, section_, document_part_ ): document_elm, start_type, expected_xml = add_section_fixture Section_.return_value = section_ document = Document(document_elm, document_part_) section = document.add_section(start_type) assert document.element.xml == expected_xml sectPr = document.element.xpath('w:body/w:sectPr')[0] Section_.assert_called_once_with(sectPr, document_part_) assert section is section_
def write(cls, report_document: Document): """ Write the chapter 'Document history' with its table. Args: report_document: .docx file where the report is written. """ doc_history = cls(report_document) # add a heading to the chapter report_document.add_paragraph(doc_history.TITLE, doc_history.TITLE_STYLE) doc_history.add_table()
def it_provides_access_to_its_bookmarks(self, document_part_, bookmarks_): document_part_.bookmarks = bookmarks_ document = Document(None, document_part_) bookmarks = document.bookmarks assert bookmarks is bookmarks_
def add_section_fixture(self, request, Section_): sentinel, start_type, new_sentinel = request.param document_cxml = 'w:document/w:body/(w:p,%s)' % sentinel document = Document(element(document_cxml), None) expected_xml = xml('w:document/w:body/(w:p,w:p/w:pPr/%s,%s)' % (sentinel, new_sentinel)) section_ = Section_.return_value return document, start_type, Section_, section_, expected_xml
def block_width_fixture(self, sections_prop_, section_): document = Document(None, None) sections_prop_.return_value = [None, section_] section_.page_width = 6000 section_.left_margin = 1500 section_.right_margin = 1000 expected_value = 3500 return document, expected_value
def it_provides_access_to_its_sections(self, document_part_, Sections_, sections_): document_elm = element('w:document') Sections_.return_value = sections_ document = Document(document_elm, document_part_) sections = document.sections Sections_.assert_called_once_with(document_elm, document_part_) assert sections is sections_
def replace_doc_text(file, replacements): document = Document(file) paragraphs = document.paragraphs changes = False for paragraph in paragraphs: text = paragraph.text for original, replace in replacements.items(): if original in replace and replace in text: continue if original in text: changes = True text = text.replace(original, replace) paragraph.text = text if changes: print("changing {}".format(file)) document.save(file)
def get_doc(): word_file = Document('examples/example1.docx') # Extracting all headings and paragraphs for attributes in word_file.paragraphs: if attributes.style.name == 'Heading 1': heading_one.append(attributes.text) else: heading_two.append(attributes.text) return heading_one, heading_two
def write(report_document: Document): """ Add a table of content. The table of content will not appear at the first time. It has to be updated by pressing Ctrl + A, and then F9. Args: report_document: .docx file where the report is written. """ # add the heading of the table of content report_document.add_paragraph('Table of content', 'Table of content') # access to XML run element <w:r> paragraph = report_document.add_paragraph() run = paragraph.add_run() r = run._r # create new XML elements, set their attributes and add them to the run element # so that the table of content is considered as such and can be updated fldChar = OxmlElement('w:fldChar') fldChar.set(qn('w:fldCharType'), 'begin') r.append(fldChar) instrText = OxmlElement('w:instrText') instrText.set(qn('xml:space'), 'preserve') instrText.text = 'TOC \\o "1-2" \\h \\z \\u' # "1-2" correspond to heading levels r.append(instrText) fldChar2 = OxmlElement('w:fldChar') fldChar2.set(qn('w:fldCharType'), 'separate') fldChar3 = OxmlElement('w:t') fldChar3.text = 'Press "Ctrl + A" to select everything and then "F9" to update fields.' fldChar2.append(fldChar3) r.append(fldChar2) fldChar4 = OxmlElement('w:fldChar') fldChar4.set(qn('w:fldCharType'), 'end') r.append(fldChar4)
def write(cls, report_document: Document, text_input_document: Document, list_of_tables: List[str], parameters_dictionary: Dict[str, Union[str, int]] ): """ Write the chapter 'Participants’ characteristics' with its table if some participants were described. Args: report_document: .docx file where the report is written. text_input_document: .docx file where all inputs are written. list_of_tables: List of all table names. parameters_dictionary: Dictionary of all input parameters (key = parameter name, value = parameter value) """ participant_appendix = cls(report_document, text_input_document, list_of_tables, parameters_dictionary) if participant_appendix.described_rows[0] != 0: # add a heading to the chapter report_document.add_paragraph(participant_appendix.TITLE, participant_appendix.TITLE_STYLE) participant_appendix.add_table()
def add_page_break_fixture(self, add_paragraph_, paragraph_, run_): document = Document(None, None) add_paragraph_.return_value = paragraph_ paragraph_.add_run.return_value = run_ return document, paragraph_, run_
def tables_fixture(self, body_prop_, tables_): document = Document(None, None) body_prop_.return_value.tables = tables_ return document, tables_
def styles_fixture(self, document_part_, styles_): document = Document(None, document_part_) document_part_.styles = styles_ return document, styles_
def settings_fixture(self, document_part_, settings_): document = Document(None, document_part_) document_part_.settings = settings_ return document, settings_
def save_fixture(self, document_part_): document = Document(None, document_part_) file_ = 'foobar.docx' return document, file_
def sections_fixture(self, Sections_, sections_): document_elm = element('w:document') document = Document(document_elm, None) Sections_.return_value = sections_ return document, Sections_, sections_
def inline_shapes_fixture(self, document_part_, inline_shapes_): document = Document(None, document_part_) document_part_.inline_shapes = inline_shapes_ return document, inline_shapes_
def core_props_fixture(self, document_part_, core_properties_): document = Document(None, document_part_) document_part_.core_properties = core_properties_ return document, core_properties_
def body_fixture(self, _Body_, body_): document_elm = element('w:document/w:body') body_elm = document_elm[0] document = Document(document_elm, None) return document, body_elm, _Body_, body_
def add_table_fixture(self, _block_width_prop_, body_prop_, table_): document = Document(None, None) rows, cols, style = 4, 2, 'Light Shading Accent 1' body_prop_.return_value.add_table.return_value = table_ _block_width_prop_.return_value = width = 42 return document, rows, cols, style, width, table_
def add_picture_fixture(self, request, add_paragraph_, run_, picture_): document = Document(None, None) path, width, height = 'foobar.png', 100, 200 add_paragraph_.return_value.add_run.return_value = run_ run_.add_picture.return_value = picture_ return document, path, width, height, run_, picture_
def add_paragraph_fixture(self, request, body_prop_, paragraph_): text, style = request.param document = Document(None, None) body_prop_.return_value.add_paragraph.return_value = paragraph_ return document, text, style, paragraph_
def it_raises_on_heading_level_out_of_range(self): document = Document(None, None) with pytest.raises(ValueError): document.add_heading(level=-1) with pytest.raises(ValueError): document.add_heading(level=10)
def add_heading_fixture(self, request, add_paragraph_, paragraph_): level, style = request.param document = Document(None, None) text = 'Spam vs. Bacon' add_paragraph_.return_value = paragraph_ return document, text, level, style, paragraph_
def paragraphs_fixture(self, body_prop_, paragraphs_): document = Document(None, None) body_prop_.return_value.paragraphs = paragraphs_ return document, paragraphs_
def document(self): """ A |Document| object providing access to the content of this document. """ return Document(self._element, self)
def part_fixture(self, document_part_): document = Document(None, document_part_) return document, document_part_