merge_table_cells_vertical
Combine adjacent cells vertically in a specified column of a Word table to simplify layout or structure. Input includes filename, table index, column index, and row range.
Instructions
Merge cells vertically in a single column.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| col_index | Yes | ||
| end_row | Yes | ||
| filename | Yes | ||
| start_row | Yes | ||
| table_index | Yes |
Input Schema (JSON Schema)
{
"properties": {
"col_index": {
"title": "Col Index",
"type": "integer"
},
"end_row": {
"title": "End Row",
"type": "integer"
},
"filename": {
"title": "Filename",
"type": "string"
},
"start_row": {
"title": "Start Row",
"type": "integer"
},
"table_index": {
"title": "Table Index",
"type": "integer"
}
},
"required": [
"filename",
"table_index",
"col_index",
"start_row",
"end_row"
],
"type": "object"
}
Implementation Reference
- word_document_server/main.py:266-270 (registration)MCP tool registration using @mcp.tool() decorator. Thin synchronous wrapper that delegates to the async implementation in format_tools.@mcp.tool() def merge_table_cells_vertical(filename: str, table_index: int, col_index: int, start_row: int, end_row: int): """Merge cells vertically in a single column.""" return format_tools.merge_table_cells_vertical(filename, table_index, col_index, start_row, end_row)
- Primary asynchronous handler function. Validates inputs, loads the Word document using python-docx, calls the core merge_cells_vertical helper on the table, saves the document, and returns success/error message.async def merge_table_cells_vertical(filename: str, table_index: int, col_index: int, start_row: int, end_row: int) -> str: """Merge cells vertically in a single column. Args: filename: Path to the Word document table_index: Index of the table (0-based) col_index: Column index (0-based) start_row: Starting row index (0-based) end_row: Ending row index (0-based, inclusive) """ filename = ensure_docx_extension(filename) # Ensure numeric parameters are the correct type try: table_index = int(table_index) col_index = int(col_index) start_row = int(start_row) end_row = int(end_row) 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 vertical cell merge success = merge_cells_vertical(table, col_index, start_row, end_row) if success: doc.save(filename) return f"Cells merged vertically in table {table_index}, column {col_index}, rows {start_row}-{end_row}." else: return f"Failed to merge cells vertically. Check that indices are valid." except Exception as e: return f"Failed to merge cells vertically: {str(e)}"
- Core utility function for vertical cell merging. Delegates to the general merge_cells function with appropriate row/col indices for vertical span.def merge_cells_vertical(table, col_index, start_row, end_row): """ Merge cells vertically in a single column. Args: table: The table containing cells to merge col_index: Column index (0-based) start_row: Starting row index (0-based) end_row: Ending row index (0-based, inclusive) Returns: True if successful, False otherwise """ return merge_cells(table, start_row, col_index, end_row, col_index)