Skip to main content
Glama

modify_table_cell

Edit or insert content into a specific table cell in a Microsoft Word document while preserving the existing non-header cell style. Specify file, table index, row, column, and content to update.

Instructions

Modify or add content to a specific table cell, following the style of existing non-header cells.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
columnYes
contentYes
filenameYes
rowYes
table_indexYes

Implementation Reference

  • The core handler function that implements the logic to modify a specific cell in a table within a Word document. It validates inputs, loads the document, accesses the table and cell, copies styling from other cells, sets the new content, and saves the document.
    async def modify_table_cell(filename: str, table_index: int, row: int, column: int, content: str) -> str: """Modify or add content to a specific table cell. Args: filename: Path to the Word document table_index: Index of the table (0-based) row: Row index (0-based) column: Column index (0-based) content: Text content to set in the cell """ filename = ensure_docx_extension(filename) # Ensure numeric parameters are the correct type try: table_index = int(table_index) row = int(row) column = int(column) except (ValueError, TypeError): return "Invalid parameter: table_index, row, and column must be integers" 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 < 0 or row >= len(table.rows): return f"Invalid row index. Table has {len(table.rows)} rows (0-{len(table.rows)-1})." if column < 0 or column >= len(table.columns): return f"Invalid column index. Table has {len(table.columns)} columns (0-{len(table.columns)-1})." # Get the target cell cell = table.cell(row, column) # Find a reference cell to copy style from (preferably a non-header cell) reference_cell = None reference_style = None # Look for a non-header cell in the same table to copy style from for r in range(len(table.rows)): for c in range(len(table.columns)): if r != row or c != column: # Don't use the target cell itself try: ref_cell = table.cell(r, c) if ref_cell.text.strip(): # Find a cell with content reference_cell = ref_cell break except IndexError: continue if reference_cell: break # If no reference cell found, use the first cell with content if not reference_cell: for r in range(len(table.rows)): for c in range(len(table.columns)): try: ref_cell = table.cell(r, c) if ref_cell.text.strip(): reference_cell = ref_cell break except IndexError: continue if reference_cell: break # Clear existing content and add new content cell.text = content # If we found a reference cell, copy its formatting if reference_cell and reference_cell.paragraphs: ref_paragraph = reference_cell.paragraphs[0] if ref_paragraph.runs: ref_run = ref_paragraph.runs[0] # Apply the same formatting to the new content if cell.paragraphs: target_paragraph = cell.paragraphs[0] if target_paragraph.runs: target_run = target_paragraph.runs[0] # Copy font properties if hasattr(ref_run.font, 'bold') and ref_run.font.bold is not None: target_run.font.bold = ref_run.font.bold if hasattr(ref_run.font, 'italic') and ref_run.font.italic is not None: target_run.font.italic = ref_run.font.italic if hasattr(ref_run.font, 'underline') and ref_run.font.underline is not None: target_run.font.underline = ref_run.font.underline if hasattr(ref_run.font, 'size') and ref_run.font.size is not None: target_run.font.size = ref_run.font.size if hasattr(ref_run.font, 'name') and ref_run.font.name is not None: target_run.font.name = ref_run.font.name if hasattr(ref_run.font, 'color') and ref_run.font.color.rgb is not None: target_run.font.color.rgb = ref_run.font.color.rgb doc.save(filename) return f"Cell content updated successfully at table {table_index}, row {row}, column {column}." except Exception as e: return f"Failed to modify table cell: {str(e)}"
  • The MCP tool registration using @mcp.tool() decorator. This wrapper function defines the tool interface and delegates to the actual implementation imported as modify_table_cell_func.
    @mcp.tool() async def modify_table_cell(filename: str, table_index: int, row: int, column: int, content: str): """Modify or add content to a specific table cell, following the style of existing non-header cells.""" return await modify_table_cell_func(filename, table_index, row, column, content)
  • Import of the handler function from content_tools.py, aliased for use in the registration wrapper.
    from word_document_server.tools.content_tools import modify_table_cell as modify_table_cell_func

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/franlealp1/mcp-word'

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