Skip to main content
Glama
GongRzhe

Office Word MCP Server

delete_paragraph

Remove a specific paragraph from a Microsoft Word document by specifying its index position in the file.

Instructions

Delete a paragraph from a document.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
filenameYes
paragraph_indexYes

Implementation Reference

  • The core async handler function implementing the delete_paragraph tool logic: loads the document, validates the paragraph index, deletes the paragraph using a python-docx workaround (removing the underlying XML element), saves the document, and returns a status message.
    async def delete_paragraph(filename: str, paragraph_index: int) -> str:
        """Delete a paragraph from a document.
        
        Args:
            filename: Path to the Word document
            paragraph_index: Index of the paragraph to delete (0-based)
        """
        filename = ensure_docx_extension(filename)
        
        if not os.path.exists(filename):
            return f"Document {filename} does not exist"
        
        # Check if file is writeable
        is_writeable, error_message = check_file_writeable(filename)
        if not is_writeable:
            return f"Cannot modify document: {error_message}. Consider creating a copy first."
        
        try:
            doc = Document(filename)
            
            # Validate paragraph index
            if paragraph_index < 0 or paragraph_index >= len(doc.paragraphs):
                return f"Invalid paragraph index. Document has {len(doc.paragraphs)} paragraphs (0-{len(doc.paragraphs)-1})."
            
            # Delete the paragraph (by removing its content and setting it empty)
            # Note: python-docx doesn't support true paragraph deletion, this is a workaround
            paragraph = doc.paragraphs[paragraph_index]
            p = paragraph._p
            p.getparent().remove(p)
            
            doc.save(filename)
            return f"Paragraph at index {paragraph_index} deleted successfully."
        except Exception as e:
            return f"Failed to delete paragraph: {str(e)}"
  • MCP tool registration using @mcp.tool() decorator. This sync wrapper function serves as the MCP-exposed tool entry point, delegating execution to the async implementation in content_tools.delete_paragraph.
    @mcp.tool()
    def delete_paragraph(filename: str, paragraph_index: int):
        """Delete a paragraph from a document."""
        return content_tools.delete_paragraph(filename, paragraph_index)
  • The function signature and docstring in the MCP registration define the input schema (filename: str, paragraph_index: int) and purpose for tool invocation.
    def delete_paragraph(filename: str, paragraph_index: int):
        """Delete a paragraph from a document."""
        return content_tools.delete_paragraph(filename, paragraph_index)
  • Imports the delete_paragraph function from content_tools into the tools package namespace, facilitating modular access across the codebase.
    from word_document_server.tools.content_tools import (
        add_heading, add_paragraph, add_table, add_picture,
        add_page_break, add_table_of_contents, delete_paragraph,
        search_and_replace

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