An intelligent document editing system that combines LangGraph, OpenAI, and docx2python to perform CRUD operations on Word documents using natural language.
- โ Natural Language Interface - Edit documents with plain English
- โ Anchor-Based Navigation - Precise paragraph addressing with depth-4 enumeration
- โ Breadcrumb Trails - Hierarchical context for every paragraph
- โ OpenAI-Powered CRUD - Intelligent operation routing and execution
- โ LangGraph Integration - Agent-based workflows with tool calling
- โ JSON Export - Structured document index with metadata
User Natural Language Query
โ
OpenAI LLM (Reasoning)
โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ LangGraph Agent โ
โ (graph.py) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Agent Tools โ
โ โข get_paragraph() โ
โ โข update_paragraph() โ
โ โข get_document_outline()โ
โ โข search_document() โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ DOCX Manager โ
โ (Index & Operations) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ DOCX Indexer โ
โ (Structure Parser) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
DOCX File
# Clone and setup
cd /Users/yash/Documents/rfp/DOCX-agent
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
# Install dependencies
pip install -r requirements.txtpython test_agent.pyThis will:
- Index the sample DOCX file
- Test all CRUD operations
- Generate
document_index.json - Display example prompts
# Set OpenAI API key
export OPENAI_API_KEY="sk-your-key-here"
# Start LangGraph dev server
cd main
langgraph devOpen http://localhost:8000 in your browser.
Read Operations:
"Show me the document outline"
"What's in section 2.1?"
"Find all mentions of CPX"
Update Operations:
"Change the title to 'Updated RFP Response'"
"Update section 2.1 to say 'Company Profile'"
Search Operations:
"Find paragraphs about implementation"
"Where is the pricing section?"
For the lightweight demo flow you can run just the FastAPI backend and the optional Microsoft Teams adapter.
- Configure environment
cd /Users/yash/Documents/rfp/DOCX-agent cp .env.example .env # update values if needed
- Start the backend API
uvicorn backend.app:app --reload --port 8080
- Uses in-memory LangGraph execution
- Persists session metadata to
backend/sessions.csv - Reads demo DOCX files from the directories listed in
DOCUMENT_SEARCH_DIRS
- (Optional) Run the Teams bridge
Set
cd teams python app.pyBACKEND_API_URLin your environment if the backend is not onhttp://localhost:8080. - Load a document in chat
Send
/load master.docx(or another filename) from the web/Teams client to attach the sample document.
from docx2python import docx2python
with docx2python("document.docx") as docx:
pars = docx.body # depth-4: [table][row][col][par]
first = pars[0][0][0][0] # First paragraphEvery paragraph gets a unique anchor:
{
"anchor": ["body", 0, 0, 0, 5],
"breadcrumb": "RFP PROPOSAL RESPONSE > Table of Contents",
"style": "Heading 2",
"text": "Table of Contents",
"level": 2
}Anchor Format: ["body", table, row, column, paragraph]
The LLM acts as an intelligent router:
User: "Update the pricing section to include 20% discount"
OpenAI reasoning:
1. ๐ Search for "pricing" โ find anchor
2. ๐ Get current text โ understand context
3. โ๏ธ Modify text with discount
4. ๐พ Call update_paragraph(anchor, new_text)
5. โ
Return success message
Four core tools in tools.py:
# READ
await get_paragraph(["body", 0, 0, 0, 5])
await get_document_outline()
await search_document("CPX")
# UPDATE
await update_paragraph(["body", 0, 0, 0, 5], "New text")# Manual, error-prone
anchor = ["body", 2, 5, 0, 12]
update_paragraph(anchor, "New text") # How do you know the anchor?"Update the introduction paragraph"
โ AI finds it, validates context, updates correctly
- No Manual Anchor Lookup - AI finds the right paragraph
- Context Understanding - "pricing section" vs "first pricing mention"
- Multi-Step Operations - "Compare sections 3 and 4"
- Error Prevention - Validates before updating
- Natural Language - "Change X to Y in section Z"
from react_agent.docx_indexer import DocxIndexer
indexer = DocxIndexer("document.docx")
paragraphs = indexer.index()
outline = indexer.get_outline()
matches = indexer.find_by_text("search term")
indexer.save_index("output.json")from react_agent.docx_manager import get_docx_manager
manager = get_docx_manager("document.docx")
para = manager.get_paragraph(["body", 0, 0, 0, 5])
outline = manager.get_outline()
results = manager.search("query")
manager.update_paragraph(anchor, "new text")from react_agent.tools import (
get_paragraph,
update_paragraph,
get_document_outline,
search_document
)
# All tools are async
result = await get_document_outline()python test_agent.py# Index a document
python main/src/react_agent/docx_indexer.py response/master.docx output.json
# Test manager
python -c "
from react_agent.docx_manager import get_docx_manager
manager = get_docx_manager('response/master.docx')
print(f'Found {len(manager.get_all_paragraphs())} paragraphs')
"See TESTING.md for comprehensive prompt examples.
Simple Queries:
- "Show me the outline"
- "What's in section 3?"
- "Find 'implementation'"
Complex Queries:
- "Update all pricing sections to include 15% discount"
- "Show me the breadcrumb for the team section"
- "List all subsections under 'About CPX'"
Edit main/src/react_agent/docx_manager.py:
def get_docx_manager(docx_path: Optional[str] = None) -> DocxManager:
if docx_path is None:
docx_path = "/path/to/your/default.docx" # Change here
return DocxManager(docx_path)Edit main/langgraph.json or set environment variables:
export OPENAI_API_KEY="sk-..."
export OPENAI_MODEL="gpt-4"Edit main/src/react_agent/prompts.py:
DEFAULT_SYSTEM_PROMPT = """
Your custom instructions here...
"""DOCX-agent/
โโโ main/
โ โโโ src/
โ โโโ react_agent/
โ โโโ docx_indexer.py # DOCX structure parser
โ โโโ docx_manager.py # Document operations
โ โโโ tools.py # LangGraph agent tools
โ โโโ graph.py # Agent graph definition
โ โโโ state.py # Agent state
โ โโโ prompts.py # System prompts
โโโ response/
โ โโโ master.docx # Sample document
โโโ test_agent.py # Comprehensive tests
โโโ TESTING.md # Testing guide
โโโ README.md # This file
โโโ requirements.txt # Python dependencies
- docx2python - Parse DOCX structure (depth-4)
- python-docx - Edit DOCX content
- langgraph - Agent orchestration
- langchain - LLM integration
- openai - GPT-4 API
[
{
"anchor": ["body", 0, 0, 0, 0],
"breadcrumb": "RFP PROPOSAL RESPONSE",
"style": "Heading 1",
"text": "RFP PROPOSAL RESPONSE",
"level": 1
},
{
"anchor": ["body", 0, 0, 0, 5],
"breadcrumb": "Table of Contents",
"style": "Heading 2",
"text": "Table of Contents",
"level": 2
}
]{
"success": true,
"message": "Paragraph updated successfully"
}source venv/bin/activate
pip install -r requirements.txtUpdate the path in docx_manager.py or pass as parameter.
export OPENAI_API_KEY="sk-..."- ๐ RFP Response Automation - Update proposals with client-specific info
- ๐ Contract Management - Search and modify contract terms
- ๐ Report Generation - Populate templates with data
- ๐ Document QA - Ask questions about document content
- โ๏ธ Batch Editing - Update multiple sections at once
- Support for tables and images
- Track change history
- Multi-document operations
- Export to PDF
- Template system
- Collaboration features
See LICENSE file for details.
This is a prototype system. Feel free to extend and customize for your needs!
For questions or issues, refer to TESTING.md for detailed examples and troubleshooting.
Built with โค๏ธ using LangGraph, OpenAI, and docx2python