Skip to main content
This lesson continues the document ingestion series for retrieval-augmented generation (RAG) systems and focuses on parsing Microsoft Word DOCX files with Python. We’ll build a robust DOCX ingestion pipeline that:
  • extracts text and core metadata from DOCX files,
  • splits large documents into intelligent, overlapping chunks suitable for embedding,
  • preserves paragraph and sentence boundaries when possible to improve retrieval quality for downstream LLM usage.
What you’ll get
  • A minimal, production-friendly DocxParser implementation (parsing, chunking, and orchestration).
  • A chunking strategy that prefers paragraph and sentence boundaries, with word-boundary fallback.
  • A small example script to run and inspect chunks before embedding into a vector store.
Contents
  • Setup
  • Quick note about files
  • Imports and the core class (complete code you can save as main.py)
  • How the parser works (summary)
  • Output example and next steps
  • Links and references
Setup Run these commands to create a Python virtual environment and install the DOCX parsing dependency:
Note: Ensure you run these commands in the directory where you’ll keep your DOCX files (for example, the same directory as main.py).
Make sure a DOCX file named Sample.docx (or another filename you pass to the script) is present in the same directory when you run the example below.
Imports and core class Below is a consolidated implementation of the DOCX ingestion pipeline. Save the code into main.py. This single file contains:
  • parse_docx — read paragraphs and extract basic core properties into metadata.
  • chunk_text — split text into overlapping chunks while preferring paragraph and sentence boundaries, and falling back to word boundaries.
  • process_document — orchestrator that parses and chunks, injecting document metadata into each chunk.
  • _generate_doc_id — convenience helper to create a deterministic document ID.
How the parser works (summary)
  • parse_docx
    • Uses python-docx to read paragraph texts.
    • Removes empty paragraphs and joins paragraphs with a double-newline (\n\n) so that paragraph boundaries are preserved and available to the chunking logic.
    • Extracts core properties (title, author, created, modified) when available and returns them as metadata.
  • chunk_text
    • Slides a window of size chunk_size across the full text.
    • Within the overlap region it prefers:
      1. Paragraph boundary (\n\n)
      2. Sentence-ending punctuation (., !, ?)
      3. Word boundary (whitespace)
    • If none of the above are found in the overlap, it performs a hard cut.
    • Produces overlapping chunks by advancing start to end - chunk_overlap.
  • process_document
    • Orchestrates parsing and chunking, attaches document_metadata and a deterministic document_id to each chunk. Chunks are ready for embedding and storage in a vector database.
DocxParser configuration and outputs Chunk dictionary schema (each chunk returned by process_document): Document metadata keys produced by parse_docx: Example output (sample) When you run python main.py against a small DOCX (Sample.docx), you’ll see printed chunks similar to:
Next steps and recommendations
  • Embed each chunk’s text with your embedding model and persist the vectors along with document_metadata and document_id in your vector database. Use the metadata to provide provenance during retrieval and answer generation.
  • Extend parse_docx to extract headings, tables, footnotes, and other structured content to improve chunk semantics and retrieval precision.
  • Tune chunk_size/chunk_overlap for your embedding model and retrieval latency: larger chunks reduce the number of vectors but may reduce relevance granularity.
Best practices
  • Keep paragraph breaks (\n\n) intact when possible to make chunks more semantically meaningful.
  • Store document_id and filename with vectors for traceability.
  • For multi-document ingestion, compute a file hash or use a content hash for deduplication.
Links and references This parser is a simple, extensible baseline for DOCX ingestion in RAG pipelines and integrates well with embedding models and vector stores for scalable retrieval.

Watch Video