apply_table_alternating_rows
Apply alternating row colors to tables in Word documents to enhance readability. Specify filename, table index, and colors for customization.
Instructions
Apply alternating row colors to a table for better readability.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| color1 | No | FFFFFF | |
| color2 | No | F2F2F2 | |
| filename | Yes | ||
| table_index | Yes |
Implementation Reference
- word_document_server/main.py:241-246 (registration)Tool registration using @mcp.tool() decorator. This is the entry point handler called by the MCP server, which delegates to the implementation in format_tools.@mcp.tool() def apply_table_alternating_rows(filename: str, table_index: int, color1: str = "FFFFFF", color2: str = "F2F2F2"): """Apply alternating row colors to a table for better readability.""" return format_tools.apply_table_alternating_rows(filename, table_index, color1, color2)
- Main handler function that performs input validation, loads the document, selects the table, calls the core shading helper, saves the document, and returns status.async def apply_table_alternating_rows(filename: str, table_index: int, color1: str = "FFFFFF", color2: str = "F2F2F2") -> str: """Apply alternating row colors to a table for better readability. Args: filename: Path to the Word document table_index: Index of the table (0-based) color1: Color for odd rows (hex string, default white) color2: Color for even rows (hex string, default light gray) """ 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" 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 alternating row shading success = apply_alternating_row_shading(table, color1, color2) if success: doc.save(filename) return f"Alternating row shading applied successfully to table {table_index}." else: return f"Failed to apply alternating row shading." except Exception as e: return f"Failed to apply alternating row shading: {str(e)}"
- Core implementation that loops through table rows, alternates colors, and applies shading to each cell using set_cell_shading.def apply_alternating_row_shading(table, color1="FFFFFF", color2="F2F2F2"): """ Apply alternating row colors for better readability. Args: table: The table to format color1: Color for odd rows (hex string) color2: Color for even rows (hex string) Returns: True if successful, False otherwise """ try: for i, row in enumerate(table.rows): fill_color = color1 if i % 2 == 0 else color2 for cell in row.cells: set_cell_shading(cell, fill_color=fill_color) return True except Exception as e: print(f"Error applying alternating row shading: {e}") return False