talira

docx2python

Extract docx headers, footers, text, footnotes, endnotes, properties, comments and images

open sourceMITself-hostablefree

docx2python is a Python library that reads a .docx file and returns its content as plain Python data: the body, headers, footers, footnotes and endnotes as nested lists (tables, rows, cells, paragraphs), the document's properties, its comments and its images. Tables always come out as n×m lists, with merged cells filled in, so a Word table maps straight onto rows of data, and it also extracts checkbox and dropdown selections, math equations, paragraph styles such as Heading 1, list numbering and, optionally, basic formatting as HTML. It began as an expansion of python-docx2txt; unlike python-docx it doesn't build documents.

Problems it solves

  • pull the answers out of a filled-in Word form, including which checkboxes were ticked and what was picked in the dropdowns
  • turn the tables in a .docx into lists of rows I can load into pandas or a database
  • merged cells in a Word table break my parser, so I need every row to come out with the same number of columns
  • extract the footnotes, endnotes, headers and footers from Word files, not just the body text
  • get all the text out of hundreds of Word documents to index them for search
  • save every image embedded in a Word document to a folder with Python
  • find which paragraphs in a .docx are headings so I can split the document into sections
  • read the comments reviewers left in a Word document along with who wrote them and when
  • convert a Word table to Markdown for an LLM prompt
  • keep bold and italic formatting as HTML tags when extracting text from Word
  • my placeholders like #NAME# are split across runs in the Word XML so search and replace misses them

Features

  • body, headers, footers, footnotes and endnotes as nested lists with paragraphs at depth 4
  • tables always returned as n×m lists; merged cells duplicated or left empty
  • Par objects with each paragraph's style, list position, runs and XML element
  • checkbox and dropdown selections
  • comments as (reference text, author, date, comment text)
  • document properties such as creator and lastModifiedBy
  • images, as bytes or saved to a folder, with placeholders in the text
  • math equations
  • bullets and numbered lists rendered as text with indentation
  • optional HTML for bold, italic, underline, strike, sub/superscript, highlight, font size and colour
View 3 more
  • hyperlinks as <a href> tags
  • merges runs Word splits arbitrarily and repeated links, and exposes the XML for light edits
  • supports Word's strict Open XML namespace

Limitations

  • Doesn't create Word documents: it extracts content from existing .docx files, and edits only lightly through their XML (search and replace)1

Alternatives

The same job

These do what docx2python does.

  1. python-docx Create and modify Word documents with Python
  2. Docling Get your documents ready for gen AI
  3. Unstructured Converts PDFs, Office files, HTML and email into structured data for LLM pipelines

Compare docx2python with the top 3