Skip to main content
Glama
GongRzhe

Office Word MCP Server

apply_table_alternating_rows

Apply alternating row colors to Word document tables to improve readability and visual distinction between rows.

Instructions

Apply alternating row colors to a table for better readability.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
filenameYes
table_indexYes
color1NoFFFFFF
color2NoF2F2F2

Implementation Reference

  • MCP tool registration and handler function. Thin wrapper that delegates execution to format_tools.apply_table_alternating_rows.
    @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)
  • Primary helper function implementing the tool logic: validates input, loads Word document using python-docx, retrieves specified table, calls core shading function, saves the modified document.
    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 utility function that implements the alternating row shading: loops through each row, selects alternating color, applies set_cell_shading to every cell in the row.
    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

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/GongRzhe/Office-Word-MCP-Server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server