merge_table_cells_horizontal
Combine adjacent cells in a single row of a Word document table. Specify the filename, table index, row index, and column range to streamline data presentation and improve document formatting.
Instructions
Merge cells horizontally in a single row.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| end_col | Yes | ||
| filename | Yes | ||
| row_index | Yes | ||
| start_col | Yes | ||
| table_index | Yes |
Implementation Reference
- Primary handler function that loads the document, validates parameters, calls the core merge helper, and saves the changes.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)}"
- word_document_server/main.py:260-264 (registration)MCP tool registration using @mcp.tool() decorator. Thin wrapper that delegates to the format_tools implementation.@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)
- Core helper function that performs the actual horizontal cell merging by calling the general merge_cells function.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)