Skip to main content
Glama
GongRzhe

Office Word MCP Server

merge_table_cells

Combine adjacent cells in Microsoft Word tables to create unified headers or organize content. Specify the rectangular area to merge using row and column coordinates.

Instructions

Merge cells in a rectangular area of a table.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
filenameYes
table_indexYes
start_rowYes
start_colYes
end_rowYes
end_colYes

Implementation Reference

  • MCP tool handler for 'merge_table_cells'. Registered via @mcp.tool() decorator. Defines input schema via type hints and docstring. Executes by delegating to the helper function in format_tools.py.
    @mcp.tool()
    def merge_table_cells(filename: str, table_index: int, start_row: int, start_col: int, 
                        end_row: int, end_col: int):
        """Merge cells in a rectangular area of a table."""
        return format_tools.merge_table_cells(filename, table_index, start_row, start_col, end_row, end_col)
  • Core implementation of the merge_table_cells tool. Loads the Word document using python-docx, validates inputs, performs the cell merge operation using the merge_cells helper, saves the document, and returns a status message.
    async def merge_table_cells(filename: str, table_index: int, start_row: int, start_col: int, 
                              end_row: int, end_col: int) -> str:
        """Merge cells in a rectangular area of a table.
        
        Args:
            filename: Path to the Word document
            table_index: Index of the table (0-based)
            start_row: Starting row index (0-based)
            start_col: Starting column index (0-based)
            end_row: Ending row index (0-based, inclusive)
            end_col: Ending column index (0-based, inclusive)
        """
        filename = ensure_docx_extension(filename)
        
        # Ensure numeric parameters are the correct type
        try:
            table_index = int(table_index)
            start_row = int(start_row)
            start_col = int(start_col)
            end_row = int(end_row)
            end_col = int(end_col)
        except (ValueError, TypeError):
            return "Invalid parameter: all indices must be integers"
        
        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 table index
            if table_index < 0 or table_index >= len(doc.tables):
                return f"Invalid table index. Document has {len(doc.tables)} tables (0-{len(doc.tables)-1})."
            
            table = doc.tables[table_index]
            
            # Validate merge parameters
            if start_row > end_row or start_col > end_col:
                return "Invalid merge range: start indices must be <= end indices"
            
            if start_row == end_row and start_col == end_col:
                return "Invalid merge range: cannot merge a single cell with itself"
            
            # Apply cell merge
            success = merge_cells(table, start_row, start_col, end_row, end_col)
            
            if success:
                doc.save(filename)
                return f"Cells merged successfully in table {table_index} from ({start_row},{start_col}) to ({end_row},{end_col})."
            else:
                return f"Failed to merge cells. Check that indices are valid."
        except Exception as e:
            return f"Failed to merge cells: {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