set_table_alignment_all
Align text in all cells of a table within a Word document. Specify filename, table index, and horizontal or vertical alignment to streamline formatting tasks.
Instructions
Set text alignment for all cells in a table.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| filename | Yes | ||
| horizontal | No | left | |
| table_index | Yes | ||
| vertical | No | top |
Implementation Reference
- word_document_server/main.py:279-284 (handler)MCP tool handler and registration for 'set_table_alignment_all'. The @mcp.tool() decorator registers this function as the tool handler, which delegates execution to the format_tools module.@mcp.tool() def set_table_alignment_all(filename: str, table_index: int, horizontal: str = "left", vertical: str = "top"): """Set text alignment for all cells in a table.""" return format_tools.set_table_alignment_all(filename, table_index, horizontal, vertical)
- Main implementation logic for setting table alignment. Loads the document, validates inputs, retrieves the table, calls low-level set_table_alignment helper, saves the document, and returns status.async def set_table_alignment_all(filename: str, table_index: int, horizontal: str = "left", vertical: str = "top") -> str: """Set text alignment for all cells in a table. Args: filename: Path to the Word document table_index: Index of the table (0-based) horizontal: Horizontal alignment ("left", "center", "right", "justify") vertical: Vertical alignment ("top", "center", "bottom") """ filename = ensure_docx_extension(filename) # Ensure numeric parameters are the correct type try: table_index = int(table_index) except (ValueError, TypeError): return "Invalid parameter: table_index must be an integer" # Validate alignment parameters valid_horizontal = ["left", "center", "right", "justify"] valid_vertical = ["top", "center", "bottom"] if horizontal.lower() not in valid_horizontal: return f"Invalid horizontal alignment. Valid options: {', '.join(valid_horizontal)}" if vertical.lower() not in valid_vertical: return f"Invalid vertical alignment. Valid options: {', '.join(valid_vertical)}" 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 table alignment success = set_table_alignment(table, horizontal, vertical) if success: doc.save(filename) return f"Table alignment set successfully for table {table_index} to {horizontal}/{vertical} for all cells." else: return f"Failed to set table alignment." except Exception as e: return f"Failed to set table alignment: {str(e)}"
- Low-level core function that implements the alignment logic by iterating over all rows and cells in the table and applying set_cell_alignment to each cell.def set_table_alignment(table, horizontal="left", vertical="top"): """ Set text alignment for all cells in a table. Args: table: The table to format horizontal: Horizontal alignment ("left", "center", "right", "justify") vertical: Vertical alignment ("top", "center", "bottom") Returns: True if successful, False otherwise """ try: for row in table.rows: for cell in row.cells: set_cell_alignment(cell, horizontal, vertical) return True except Exception as e: print(f"Error setting table alignment: {e}") return False