Skip to content

Update index.rst #207

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
83 changes: 83 additions & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
----------
Expand Down