diff --git a/docs/_static/img/example-docx-after-modification.png b/docs/_static/img/example-docx-after-modification.png new file mode 100644 index 000000000..4958a8ff9 Binary files /dev/null and b/docs/_static/img/example-docx-after-modification.png differ diff --git a/docs/_static/img/example-docx-before-modification.png b/docs/_static/img/example-docx-before-modification.png new file mode 100644 index 000000000..0762f46e1 Binary files /dev/null and b/docs/_static/img/example-docx-before-modification.png differ diff --git a/docs/index.rst b/docs/index.rst index eb922f510..b6b1dd57c 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -58,6 +58,89 @@ Here's an example of what |docx| can do: document.save('demo.docx') ============================================ =============================================================== +|docx| is also capable of modifying existing documents: + +:: + + import docx + + # open the document template + document = docx.Document('Document.docx') + + # get a list of all inline_shapes of the document + shapes = document.inline_shapes + + # print the number of paragraphs + print('The document has {} inline_shapes.'.format(len(shapes))) + + # the document has no inline_shapes, create one with its attributes: + # height, type, width + + # get the "metadata" of the document + properties = document.core_properties + + # modify the attributes of the "metadata" + # author, category, comments, content_status, created, identifier, keywords, + # language, last_modified_by, last_printed, modified, revision, subject, + # title, version + + # get a list of all paragraphs + paragraphs = document.paragraphs + + # print the number of paragraphs + print('The document has {} paragraphs(s).'.format(len(paragraphs))) + + # get the parapgraph of choice by list index (here: 9 paragraph) + paragraph_of_choice = paragraphs[3] + + # modify the paragraph with its attributes: + # add_run(), alignment, clear(), insert_paragraph_before(), paragraph_format, + # runs, style, text + + # e.g. print the text of the paragraph + print(paragraph_of_choice.text) + + # get a list of all sections + sections = document.sections + + # print the number of sections + print('The document has {} sections(s).'.format(len(sections))) + + # get the section of choice by list index (here: only 1 section) + section_of_choice = sections[0] + + # modify the attributes of the section: + # bottom_margin, footer_distance, gutter, header_distance, left_margin, + # orientation, page_height, page_width, right_margin, start_type, top_margin + + # get tables + tables = document.tables + + # print the number of tables + print('The document has {} table(s).'.format(len(tables))) + + # get the table of choice by list index (here: only 1 table) + table_of_choice = tables[0] + + # modify table which has the following attributes/methods(): + # add_column(), add_row(), alignment, autofit, cell, column_cells, columns, + # row_cells, rows, style, table_direction + + # e.g. add cell text + table_of_choice.cell(row_idx=2, col_idx=2).text = 'hello' + table_of_choice.cell(row_idx=2, col_idx=3).text = 'world' + + # save the modified document (override the existing copied file) + document.save('./Document_copy.docx') + +The document before (left) and after (right) the modification: + +.. |img_before| image:: /_static/img/example-docx-before-modification.png +.. |img_after| image:: /_static/img/example-docx-after-modification.png + +============================================ ============================================ +|img_before| |img_after| +============================================ ============================================ User Guide ----------