highlight_table_header
Highlight table headers in Microsoft Word documents by specifying filename, table index, and custom colors for header and text using the MCP server tool.
Instructions
Apply special highlighting to table header row.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| filename | Yes | ||
| header_color | No | 4472C4 | |
| table_index | Yes | ||
| text_color | No | FFFFFF |
Implementation Reference
- word_document_server/main.py:247-251 (registration)Tool registration using @mcp.tool() decorator. Thin wrapper that delegates to format_tools.highlight_table_header.@mcp.tool() def highlight_table_header(filename: str, table_index: int, header_color: str = "4472C4", text_color: str = "FFFFFF"): """Apply special highlighting to table header row.""" return format_tools.highlight_table_header(filename, table_index, header_color, text_color)
- Main handler function that loads the document, validates inputs, calls the core highlight_header_row helper, and saves the document.async def highlight_table_header(filename: str, table_index: int, header_color: str = "4472C4", text_color: str = "FFFFFF") -> str: """Apply special highlighting to table header row. Args: filename: Path to the Word document table_index: Index of the table (0-based) header_color: Background color for header (hex string, default blue) text_color: Text color for header (hex string, default white) """ 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 header highlighting success = highlight_header_row(table, header_color, text_color) if success: doc.save(filename) return f"Header highlighting applied successfully to table {table_index}." else: return f"Failed to apply header highlighting." except Exception as e: return f"Failed to apply header highlighting: {str(e)}"
- Core utility function that applies background shading and bold white text formatting to the first row (header) of the table.def highlight_header_row(table, header_color="4472C4", text_color="FFFFFF"): """ Apply special shading to header row. Args: table: The table to format header_color: Background color for header (hex string) text_color: Text color for header (hex string) Returns: True if successful, False otherwise """ try: if table.rows: for cell in table.rows[0].cells: # Apply background shading set_cell_shading(cell, fill_color=header_color) # Apply text formatting for paragraph in cell.paragraphs: for run in paragraph.runs: run.bold = True if text_color and text_color != "auto": # Convert hex to RGB try: text_color = text_color.lstrip('#') r = int(text_color[0:2], 16) g = int(text_color[2:4], 16) b = int(text_color[4:6], 16) run.font.color.rgb = RGBColor(r, g, b) except: pass # Skip if color format is invalid return True except Exception as e: print(f"Error highlighting header row: {e}") return False