talira

python-docx

Create and modify Word documents with Python

open sourceMITself-hostablefree

python-docx is a Python library for reading, creating and updating Microsoft Word 2007+ (.docx) files without Word installed. It opens an existing document or starts a new one and works through an object model of paragraphs, runs and fonts, tables and cells, sections and page setup, headers and footers, styles, inline pictures and, since 1.2.0, comments; anything it doesn't understand is kept as-is when the file is saved. Libraries such as docxtpl build on it, and docx2python is a read-only alternative for pulling text, tables and form values out of a document.

Problems it solves

  • read the values people typed into the tables of a Word form and load them into a database from Python
  • generate Word reports from a Python script without Microsoft Office installed on the server
  • loop over every paragraph in a .docx and find the ones that use the Heading 2 style
  • change the font, size and colour of specific runs of text in an existing Word document
  • add a table with a header row to a Word document and fill it from a list of dicts
  • insert a chart image into a Word document at a fixed width from a Python job
  • set the page orientation, margins and headers and footers of a .docx programmatically
  • add review comments to paragraphs in a Word file from code
  • read the author, title and last-modified-by properties of hundreds of .docx files
  • replace a placeholder word in a Word document while keeping the rest of its formatting
  • build a Word document on a Linux server in a Django or Flask app and send it to the browser

Features

  • open existing .docx files or create new ones, and save them
  • paragraphs, runs and character formatting (bold, italic, font, size, colour)
  • tables: add, read and modify rows, columns and cells
  • sections and page setup: orientation, size and margins
  • headers and footers
  • styles: apply and define paragraph, character and table styles
  • inline pictures sized in inches, centimetres or points
  • comments (added in 1.2.0)
  • core document properties (author, title, created and modified dates)
  • hyperlinks: read a link's URL and fragment
View 1 more
  • content it doesn't model is preserved when the document is saved

Limitations

  • Doesn't open the old binary .doc format from Word 2003 and earlier: it reads and writes Word 2007+ .docx files only1
  • Doesn't add floating pictures that text wraps around: python-docx only supports inline pictures2

Alternatives

The same job

These do what python-docx does.

  1. Aspose.Words Create, edit, render and convert Word documents to multiple formats without using external software
  2. rdocx Native DOCX creation, editing, comparison, layout, and rendering
  3. docx2python Extract docx headers, footers, text, footnotes, endnotes, properties, comments and images
  4. docx4j JAXB-based Java library for Word docx, Powerpoint pptx, and Excel xlsx files
  5. jubarte Word-faithful DOCX redlines and rendering, without Word

Compare python-docx with the top 3