Skip to main content
Glama
GongRzhe

Office Word MCP Server

format_table_cell_text

Apply text formatting to specific table cells in Word documents, including bold, italic, underline, color, font size, and font name adjustments.

Instructions

Format text within a specific table cell.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
filenameYes
table_indexYes
row_indexYes
col_indexYes
text_contentNo
boldNo
italicNo
underlineNo
colorNo
font_sizeNo
font_nameNo

Implementation Reference

  • Registration of the MCP tool 'format_table_cell_text' using @mcp.tool() decorator. This thin wrapper delegates to the async handler in format_tools.py.
    def format_table_cell_text(filename: str, table_index: int, row_index: int, col_index: int,
                               text_content: str = None, bold: bool = None, italic: bool = None,
                               underline: bool = None, color: str = None, font_size: int = None,
                               font_name: str = None):
        """Format text within a specific table cell."""
        return format_tools.format_table_cell_text(filename, table_index, row_index, col_index,
                                                   text_content, bold, italic, underline, color, font_size, font_name)
  • Core handler function executing the tool logic: input validation, document loading/saving, table/cell access, delegation to low-level helper.
    async def format_table_cell_text(filename: str, table_index: int, row_index: int, col_index: int,
                                     text_content: Optional[str] = None, bold: Optional[bool] = None, italic: Optional[bool] = None,
                                     underline: Optional[bool] = None, color: Optional[str] = None, font_size: Optional[int] = None,
                                     font_name: Optional[str] = None) -> str:
        """Format text within a specific table cell.
        
        Args:
            filename: Path to the Word document
            table_index: Index of the table (0-based)
            row_index: Row index (0-based)
            col_index: Column index (0-based)
            text_content: Optional new text content for the cell
            bold: Set text bold (True/False)
            italic: Set text italic (True/False)
            underline: Set text underlined (True/False)
            color: Text color (hex string like "FF0000" or color name like "red")
            font_size: Font size in points
            font_name: Font name/family
        """
        filename = ensure_docx_extension(filename)
        
        # Ensure numeric parameters are the correct type
        try:
            table_index = int(table_index)
            row_index = int(row_index)
            col_index = int(col_index)
            if font_size is not None:
                font_size = int(font_size)
        except (ValueError, TypeError):
            return "Invalid parameter: table_index, row_index, col_index must be integers, font_size must be 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]
            
            # Validate row and column indices
            if row_index < 0 or row_index >= len(table.rows):
                return f"Invalid row index. Table has {len(table.rows)} rows (0-{len(table.rows)-1})."
            
            if col_index < 0 or col_index >= len(table.rows[row_index].cells):
                return f"Invalid column index. Row has {len(table.rows[row_index].cells)} cells (0-{len(table.rows[row_index].cells)-1})."
            
            # Apply cell text formatting
            success = format_cell_text_by_position(table, row_index, col_index, text_content, 
                                                  bold, italic, underline, color, font_size, font_name)
            
            if success:
                doc.save(filename)
                format_desc = []
                if text_content is not None:
                    format_desc.append(f"content='{text_content[:30]}{'...' if len(text_content) > 30 else ''}'")
                if bold is not None:
                    format_desc.append(f"bold={bold}")
                if italic is not None:
                    format_desc.append(f"italic={italic}")
                if underline is not None:
                    format_desc.append(f"underline={underline}")
                if color is not None:
                    format_desc.append(f"color={color}")
                if font_size is not None:
                    format_desc.append(f"size={font_size}pt")
                if font_name is not None:
                    format_desc.append(f"font={font_name}")
                
                format_str = ", ".join(format_desc) if format_desc else "no changes"
                return f"Cell text formatted successfully in table {table_index}, cell ({row_index},{col_index}): {format_str}."
            else:
                return f"Failed to format cell text. Check that indices are valid."
        except Exception as e:
            return f"Failed to format cell text: {str(e)}"
  • Supporting utility that retrieves the specific table cell by position and delegates formatting to format_cell_text.
    def format_cell_text_by_position(table, row_index, col_index, text_content=None, 
                                     bold=None, italic=None, underline=None, color=None, 
                                     font_size=None, font_name=None):
        """
        Format text in a specific table cell by position.
        
        Args:
            table: The table containing the cell
            row_index: Row index (0-based)
            col_index: Column index (0-based)
            text_content: Optional new text content for the cell
            bold: Set text bold (True/False)
            italic: Set text italic (True/False)
            underline: Set text underlined (True/False)
            color: Text color (hex string or color name)
            font_size: Font size in points
            font_name: Font name/family
            
        Returns:
            True if successful, False otherwise
        """
        try:
            if (0 <= row_index < len(table.rows) and 
                0 <= col_index < len(table.rows[row_index].cells)):
                cell = table.rows[row_index].cells[col_index]
                return format_cell_text(cell, text_content, bold, italic, underline, 
                                       color, font_size, font_name)
            else:
                return False
        except Exception as e:
            print(f"Error formatting cell text by position: {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