merge_table_cells_vertical
Merge cells vertically in a single column of a Word document table to combine content or create headers.
Instructions
Merge cells vertically in a single column.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| filename | Yes | ||
| table_index | Yes | ||
| col_index | Yes | ||
| start_row | Yes | ||
| end_row | Yes |
Implementation Reference
- word_document_server/main.py:266-270 (registration)Tool registration using FastMCP @mcp.tool() decorator. Delegates implementation to format_tools.merge_table_cells_vertical.@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 handler implementation: input validation, document loading/saving, delegates to core.tables.merge_cells_vertical for the merge operation.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)}"
- Helper wrapper for vertical cell merging, invokes general merge_cells function.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)
- Core cell merging logic: performs validation of merge range and executes python-docx cell.merge(end_cell).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