python-docx

What it is good for?

Create, read and write Word documents.

A straightforward library to access documents in MS Word format. Available features include text, labeled sections, pictures and tables.

Installed with Python by default

no

Installed with Anaconda

no

How to install it?

  1. pip install python-docx

Example

Create a Word document:

  1. from docx import Document
  2. d = Document()
  3. d.add_heading('Hamlet')
  4. d.add_heading('dramatis personae', 2)
  5. d.add_paragraph('Hamlet, the Prince of Denmark')
  6. d.save('hamlet.docx')

Read a Word document:

  1. document = Document('hamlet.docx')
  2. for para in document.paragraphs:
  3. print(para.text)

Where to learn more?

https://python-docx.readthedocs.org