format_table_cell_text
Modify text styling within a specific table cell in Word documents, including bold, italic, underline, font size, color, and font name adjustments.
Instructions
Format text within a specific table cell.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| bold | No | ||
| col_index | Yes | ||
| color | No | ||
| filename | Yes | ||
| font_name | No | ||
| font_size | No | ||
| italic | No | ||
| row_index | Yes | ||
| table_index | Yes | ||
| text_content | No | ||
| underline | No |
Input Schema (JSON Schema)
{
"properties": {
"bold": {
"default": null,
"title": "Bold",
"type": "boolean"
},
"col_index": {
"title": "Col Index",
"type": "integer"
},
"color": {
"default": null,
"title": "Color",
"type": "string"
},
"filename": {
"title": "Filename",
"type": "string"
},
"font_name": {
"default": null,
"title": "Font Name",
"type": "string"
},
"font_size": {
"default": null,
"title": "Font Size",
"type": "integer"
},
"italic": {
"default": null,
"title": "Italic",
"type": "boolean"
},
"row_index": {
"title": "Row Index",
"type": "integer"
},
"table_index": {
"title": "Table Index",
"type": "integer"
},
"text_content": {
"default": null,
"title": "Text Content",
"type": "string"
},
"underline": {
"default": null,
"title": "Underline",
"type": "boolean"
}
},
"required": [
"filename",
"table_index",
"row_index",
"col_index"
],
"type": "object"
}
Implementation Reference
- Primary handler function implementing the core logic for formatting table cell text: validates inputs, loads document, applies formatting via helper function, saves changes, and returns status.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)}"
- word_document_server/main.py:443-450 (registration)Tool registration using @mcp.tool() decorator in the main server file, which delegates execution to the format_tools implementation.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)
- Supporting helper function that accesses the specific table cell by position and delegates to low-level format_cell_text function for applying the formatting.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