- 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.
- A minimal, production-friendly
DocxParserimplementation (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.
- 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
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.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.
-
parse_docx
- Uses
python-docxto 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.
- Uses
-
chunk_text
- Slides a window of size
chunk_sizeacross the full text. - Within the overlap region it prefers:
- Paragraph boundary (
\n\n) - Sentence-ending punctuation (
.,!,?) - Word boundary (whitespace)
- Paragraph boundary (
- 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.
- Slides a window of size
-
process_document
- Orchestrates parsing and chunking, attaches
document_metadataand a deterministicdocument_idto each chunk. Chunks are ready for embedding and storage in a vector database.
- Orchestrates parsing and chunking, attaches
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:
- Embed each chunk’s
textwith your embedding model and persist the vectors along withdocument_metadataanddocument_idin your vector database. Use the metadata to provide provenance during retrieval and answer generation. - Extend
parse_docxto extract headings, tables, footnotes, and other structured content to improve chunk semantics and retrieval precision. - Tune
chunk_size/chunk_overlapfor your embedding model and retrieval latency: larger chunks reduce the number of vectors but may reduce relevance granularity.
- Keep paragraph breaks (
\n\n) intact when possible to make chunks more semantically meaningful. - Store
document_idandfilenamewith vectors for traceability. - For multi-document ingestion, compute a file hash or use a content hash for deduplication.
- python-docx documentation
- Introduction to LLMs and OpenAI courses
- Fundamentals of RAG
- Vector database essentials