Skip to main content
Glama

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
NameRequiredDescriptionDefault
colsYes
dataNo
filenameYes
rowsYes

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)}"
  • 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)
  • 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)
  • 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
    )

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