add_table
Create and insert a table into a Word document by specifying rows, columns, and optional data. Simplifies document formatting for structured content.
Instructions
Add a table to a Word document.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| cols | Yes | ||
| data | No | ||
| filename | Yes | ||
| rows | Yes |
Implementation Reference
- Core handler function that implements the add_table tool logic: validates inputs, loads the document, creates a table with specified dimensions, optionally populates it with data, applies 'Table Grid' style if available, and saves the document.async def add_table(filename: str, rows: int, cols: int, data: Optional[List[List[str]]] = None) -> str: """Add a table to a Word document. Args: filename: Path to the Word document rows: Number of rows in the table cols: Number of columns in the table data: Optional 2D array of data to fill the table """ filename = ensure_docx_extension(filename) 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: # Suggest creating a copy return f"Cannot modify document: {error_message}. Consider creating a copy first or creating a new document." try: doc = Document(filename) table = doc.add_table(rows=rows, cols=cols) # Try to set the table style try: table.style = 'Table Grid' except KeyError: # If style doesn't exist, add basic borders pass # Fill table with data if provided if data: for i, row_data in enumerate(data): if i >= rows: break for j, cell_text in enumerate(row_data): if j >= cols: break table.cell(i, j).text = str(cell_text) doc.save(filename) return f"Table ({rows}x{cols}) added to {filename}" except Exception as e: return f"Failed to add table: {str(e)}"
- word_document_server/main.py:718-720 (registration)MCP tool registration using FastMCP @mcp.tool() decorator. Defines the tool schema via function signature and docstring, and delegates execution to the core handler in content_tools.async def add_table(filename: str, rows: int, cols: int, data: Optional[List[List[str]]] = None): """Add a table to a Word document.""" return await content_tools.add_table(filename, rows, cols, data)
- word_document_server/main.py:718-720 (schema)Tool schema defined by the function parameters (filename: str, rows: int, cols: int, data: Optional[List[List[str]]]) and docstring in the MCP registration.async def add_table(filename: str, rows: int, cols: int, data: Optional[List[List[str]]] = None): """Add a table to a Word document.""" return await content_tools.add_table(filename, rows, cols, data)
- word_document_server/tools/__init__.py:16-20 (registration)Central import/export of content tools including add_table, facilitating access for the main server registration.from word_document_server.tools.content_tools import ( add_heading, add_paragraph, add_table, add_picture, add_page_break, add_table_of_contents, delete_paragraph, search_and_replace )