Skip to main content
Glama

add_table

Insert a customizable table into a Word document by specifying filename, row and column count, and optional data. Ideal for organizing content directly in Word files.

Instructions

Add a table to a Word document.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
colsYes
dataNo
filenameYes
rowsYes

Implementation Reference

  • Core implementation of add_table tool: loads Word document, creates new table with specified rows/cols, populates with data if given, sets style, saves 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)}"
  • MCP tool registration using @mcp.tool() decorator. Thin wrapper that delegates to the content_tools.add_table implementation. Input schema inferred from signature.
    @mcp.tool() def add_table(filename: str, rows: int, cols: int, data: list = None): """Add a table to a Word document.""" return content_tools.add_table(filename, rows, cols, data)
  • Package __init__ re-exports add_table from content_tools for easy import across the codebase.
    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 )

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