Skip to main content
Glama

add_table

Insert structured tables into Microsoft Word documents to organize data and improve document layout. Specify filename, rows, and columns to create tables programmatically.

Instructions

Add a table to a Word document.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
filenameYes
rowsYes
colsYes
dataNo

Implementation Reference

  • Core handler function that implements the logic to add a table to a Word document, including validation, styling, data filling, and saving.
    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. This wrapper function registers 'add_table' as an MCP tool and delegates to the core handler in content_tools.
    @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)
  • Tool schema defined by function signature and docstring used by MCP/FastMCP for input validation, description, and parameter types.
    @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)
  • Export of add_table function from content_tools module, making it available for import in main.py and other modules.
    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