Skip to main content
Glama

set_table_width

Adjust table width in Microsoft Word documents to control layout and formatting for better document presentation.

Instructions

Set the overall width of a table.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
filenameYes
table_indexYes
widthYes
width_typeNopoints

Implementation Reference

  • Tool registration using FastMCP @mcp.tool() decorator. Thin wrapper that delegates to the format_tools.set_table_width implementation.
    @mcp.tool() def set_table_width(filename: str, table_index: int, width: float, width_type: str = "points"): """Set the overall width of a table.""" return format_tools.set_table_width(filename, table_index, width, width_type)
  • Primary handler function that orchestrates the tool execution: validates inputs, loads document, converts user-friendly width units to Word XML format, calls core helper, saves document.
    async def set_table_width(filename: str, table_index: int, width: float, width_type: str = "points") -> str: """Set the overall width of a table. Args: filename: Path to the Word document table_index: Index of the table (0-based) width: Table width value width_type: Width type ("points", "inches", "cm", "percent", "auto") """ filename = ensure_docx_extension(filename) # Ensure numeric parameters are the correct type try: table_index = int(table_index) if width_type != "auto": width = float(width) except (ValueError, TypeError): return "Invalid parameter: table_index must be an integer, width must be a number" # Validate width type valid_width_types = ["points", "inches", "cm", "percent", "auto"] if width_type.lower() not in valid_width_types: return f"Invalid width type. Valid options: {', '.join(valid_width_types)}" 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] # Convert width and type for Word format if width_type.lower() == "points": word_width = width word_type = "dxa" elif width_type.lower() == "inches": word_width = width * 72 # 72 points per inch word_type = "dxa" elif width_type.lower() == "cm": word_width = width * 28.35 # ~28.35 points per cm word_type = "dxa" elif width_type.lower() == "percent": word_width = width word_type = "pct" else: # auto word_width = 0 word_type = "auto" # Apply table width success = set_table_width_func(table, word_width, word_type) if success: doc.save(filename) return f"Table width set successfully for table {table_index} to {width} {width_type}." else: return f"Failed to set table width." except Exception as e: return f"Failed to set table width: {str(e)}"
  • Core low-level helper that directly manipulates the Word document XML to set the &lt;w:tblW&gt; element in table properties, converting units to DXA or PCT as needed.
    def set_table_width(table, width, width_type="dxa"): """ Set the overall width of a table. Args: table: The table to modify width: Table width value width_type: Width type ("dxa" for points*20, "pct" for percentage*50, "auto") Returns: True if successful, False otherwise """ try: # Convert width based on type if width_type == "dxa": # DXA units (twentieths of a point) if isinstance(width, (int, float)): width_value = str(int(width * 20)) else: width_value = str(width) elif width_type == "pct": # Percentage (multiply by 50 for Word format) if isinstance(width, (int, float)): width_value = str(int(width * 50)) else: width_value = str(width) else: width_value = str(width) # Get table element and properties tbl = table._tbl # Get or create table properties tbl_pr = tbl.find(qn('w:tblPr')) if tbl_pr is None: tbl_pr = OxmlElement('w:tblPr') tbl.insert(0, tbl_pr) # Remove existing table width existing_width = tbl_pr.find(qn('w:tblW')) if existing_width is not None: tbl_pr.remove(existing_width) # Create new table width element width_element = OxmlElement('w:tblW') width_element.set(qn('w:w'), width_value) width_element.set(qn('w:type'), width_type) tbl_pr.append(width_element) return True except Exception as e: print(f"Error setting table width: {e}") return False

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