Skip to main content
Glama
GongRzhe

Office Word MCP Server

get_paragraph_text_from_document

Extract text from a specific paragraph in a Word document by specifying the filename and paragraph index.

Instructions

Get text from a specific paragraph in a Word document.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
filenameYes
paragraph_indexYes

Implementation Reference

  • Tool registration using FastMCP @mcp.tool() decorator. This sync wrapper delegates to the async implementation in extended_document_tools.
    @mcp.tool()
    def get_paragraph_text_from_document(filename: str, paragraph_index: int):
        """Get text from a specific paragraph in a Word document."""
        return extended_document_tools.get_paragraph_text_from_document(filename, paragraph_index)
  • Async wrapper function that handles file validation, calls the core get_paragraph_text utility, and returns JSON-formatted result.
    async def get_paragraph_text_from_document(filename: str, paragraph_index: int) -> str:
        """Get text from a specific paragraph in a Word document.
        
        Args:
            filename: Path to the Word document
            paragraph_index: Index of the paragraph to retrieve (0-based)
        """
        filename = ensure_docx_extension(filename)
        
        if not os.path.exists(filename):
            return f"Document {filename} does not exist"
        
    
        if paragraph_index < 0:
            return "Invalid parameter: paragraph_index must be a non-negative integer"
        
        try:
            result = get_paragraph_text(filename, paragraph_index)
            return json.dumps(result, indent=2)
        except Exception as e:
            return f"Failed to get paragraph text: {str(e)}"
  • Core implementation: Loads the DOCX using python-docx, retrieves the specific paragraph, extracts text, style, and heading status, returns as dict.
    def get_paragraph_text(doc_path: str, paragraph_index: int) -> Dict[str, Any]:
        """
        Get text from a specific paragraph in a Word document.
        
        Args:
            doc_path: Path to the Word document
            paragraph_index: Index of the paragraph to extract (0-based)
        
        Returns:
            Dictionary with paragraph text and metadata
        """
        import os
        if not os.path.exists(doc_path):
            return {"error": f"Document {doc_path} does not exist"}
        
        try:
            doc = Document(doc_path)
            
            # Check if paragraph index is valid
            if paragraph_index < 0 or paragraph_index >= len(doc.paragraphs):
                return {"error": f"Invalid paragraph index: {paragraph_index}. Document has {len(doc.paragraphs)} paragraphs."}
            
            paragraph = doc.paragraphs[paragraph_index]
            
            return {
                "index": paragraph_index,
                "text": paragraph.text,
                "style": paragraph.style.name if paragraph.style else "Normal",
                "is_heading": paragraph.style.name.startswith("Heading") if paragraph.style else False
            }
        except Exception as e:
            return {"error": f"Failed to get paragraph text: {str(e)}"}

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/GongRzhe/Office-Word-MCP-Server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server