Skip to main content
Glama
GongRzhe

Office Word MCP Server

merge_table_cells_horizontal

Combine adjacent cells horizontally in a Word table row to create wider cells for headers, labels, or merged content.

Instructions

Merge cells horizontally in a single row.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
filenameYes
table_indexYes
row_indexYes
start_colYes
end_colYes

Implementation Reference

  • Main execution logic for the merge_table_cells_horizontal tool: validates inputs, loads Word document using python-docx, calls helper to perform merge, saves document and returns status.
    async def merge_table_cells_horizontal(filename: str, table_index: int, row_index: int, 
                                         start_col: int, end_col: int) -> str:
        """Merge cells horizontally in a single row.
        
        Args:
            filename: Path to the Word document
            table_index: Index of the table (0-based)
            row_index: Row index (0-based)
            start_col: Starting column index (0-based)
            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)
            row_index = int(row_index)
            start_col = int(start_col)
            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]
            
            # Apply horizontal cell merge
            success = merge_cells_horizontal(table, row_index, start_col, end_col)
            
            if success:
                doc.save(filename)
                return f"Cells merged horizontally in table {table_index}, row {row_index}, columns {start_col}-{end_col}."
            else:
                return f"Failed to merge cells horizontally. Check that indices are valid."
        except Exception as e:
            return f"Failed to merge cells horizontally: {str(e)}"
  • MCP tool registration using @mcp.tool() decorator. Defines tool schema via type hints and delegates execution to format_tools.merge_table_cells_horizontal.
    @mcp.tool()
    def merge_table_cells_horizontal(filename: str, table_index: int, row_index: int, 
                                   start_col: int, end_col: int):
        """Merge cells horizontally in a single row."""
        return format_tools.merge_table_cells_horizontal(filename, table_index, row_index, start_col, end_col)
  • Supporting helper function that performs the actual horizontal cell merging by invoking the general merge_cells function on the specified row range.
    def merge_cells_horizontal(table, row_index, start_col, end_col):
        """
        Merge cells horizontally in a single row.
        
        Args:
            table: The table containing cells to merge
            row_index: Row index (0-based)
            start_col: Starting column index (0-based)
            end_col: Ending column index (0-based, inclusive)
            
        Returns:
            True if successful, False otherwise
        """
        return merge_cells(table, row_index, start_col, row_index, end_col)

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