Skip to main content
Glama

get_paragraph_text_from_document

Extract text from a specified paragraph in a Word document by providing the filename and paragraph index for precise content retrieval and management.

Instructions

Get text from a specific paragraph in a Word document.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
filenameYes
paragraph_indexYes

Implementation Reference

  • Primary handler function implementing the tool logic: validates inputs, ensures file extension, loads via helper, serializes to JSON.
    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)}"
  • Tool registration using FastMCP @mcp.tool() decorator. Defines the tool endpoint with type hints and docstring for schema, delegates to implementation.
    async def get_paragraph_text_from_document(filename: str, paragraph_index: int):
        """Get text from a specific paragraph in a Word document."""
        return await extended_document_tools.get_paragraph_text_from_document(filename, paragraph_index)
  • Supporting utility that loads the Document object using python-docx, extracts paragraph by index, includes metadata like style and heading status, returns structured 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/franlealp1/mcp-word'

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