Skip to main content
Glama

merge_table_cells

Merge selected cells in a table within a Microsoft Word document by specifying a rectangular area. This tool enables users to streamline table formatting and enhance document structure.

Instructions

Merge cells in a rectangular area of a table.

Input Schema

NameRequiredDescriptionDefault
end_colYes
end_rowYes
filenameYes
start_colYes
start_rowYes
table_indexYes

Input Schema (JSON Schema)

{ "properties": { "end_col": { "title": "End Col", "type": "integer" }, "end_row": { "title": "End Row", "type": "integer" }, "filename": { "title": "Filename", "type": "string" }, "start_col": { "title": "Start Col", "type": "integer" }, "start_row": { "title": "Start Row", "type": "integer" }, "table_index": { "title": "Table Index", "type": "integer" } }, "required": [ "filename", "table_index", "start_row", "start_col", "end_row", "end_col" ], "type": "object" }

Implementation Reference

  • The primary handler function for the 'merge_table_cells' tool. Performs input validation, loads the document, calls the core merge_cells helper, saves the document, and returns success/error messages.
    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)}"
  • MCP tool registration using FastMCP @mcp.tool() decorator. Delegates execution to the format_tools.merge_table_cells handler.
    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 helper function that performs the actual cell merging using python-docx table.cell().merge(). Called by the handler.
    def merge_cells(table, start_row, start_col, end_row, end_col): """ Merge cells in a rectangular area. Args: table: The table containing cells to merge 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) Returns: True if successful, False otherwise """ try: # Validate indices if (start_row < 0 or start_col < 0 or end_row < 0 or end_col < 0 or start_row >= len(table.rows) or end_row >= len(table.rows) or start_row > end_row or start_col > end_col): return False # Check if all rows have enough columns for row_idx in range(start_row, end_row + 1): if (start_col >= len(table.rows[row_idx].cells) or end_col >= len(table.rows[row_idx].cells)): return False # Get the start and end cells start_cell = table.cell(start_row, start_col) end_cell = table.cell(end_row, end_col) # Merge the cells start_cell.merge(end_cell) return True except Exception as e: print(f"Error merging cells: {e}") return False

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