Skip to main content
Glama

fill_merge_fields

Populate merge fields in Word documents using provided data to automate document personalization and template processing.

Instructions

Fill merge fields in a document with provided data.

Args: filepath: Path to the document or template data: Dictionary mapping field names to values

Returns: Dictionary with status and modified document info

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
filepathYes
dataYes

Implementation Reference

  • The handler function 'fill_merge_fields' iterates through document paragraphs and runs, replacing placeholders of the form {{field_name}} with values provided in the data dictionary.
    def fill_merge_fields(filepath: str, data: dict[str, str]) -> dict[str, Any]:
        """
        Fill merge fields in a document with provided data.
    
        Args:
            filepath: Path to the document or template
            data: Dictionary mapping field names to values
    
        Returns:
            Dictionary with status and modified document info
        """
        logger.info("Filling merge fields", extra={"tool": "fill_merge_fields", "filepath": filepath})
    
        try:
            doc = safe_open_document(filepath)
            fields_filled = 0
    
            # Simple merge field filling by replacing in text
            for paragraph in doc.paragraphs:
                for run in paragraph.runs:
                    for field_name, field_value in data.items():
                        if f"{{{{{field_name}}}}}" in run.text:
                            run.text = run.text.replace(f"{{{{{field_name}}}}}", str(field_value))
                            fields_filled += 1
    
            safe_save_document(doc, filepath)
            logger.info(f"Filled {fields_filled} merge fields", extra={"filepath": filepath})
    
            return {
                "status": "success",
                "filepath": filepath,
                "fields_filled": fields_filled,
                "message": f"Filled {fields_filled} merge fields",
            }
        except DocxMcpError as e:
            logger.warning(e.message, extra={"tool": "fill_merge_fields", "error_code": e.error_code})
            return {"status": "error", "error": e.message, "error_code": e.error_code}
        except Exception as e:
            logger.error(f"Unexpected error filling merge fields: {str(e)}")
            return {"status": "error", "error": str(e)}
  • The tool is registered using the @app.tool() decorator immediately preceding the function definition (implied by the structure).
    @app.tool()
    def fill_merge_fields(filepath: str, data: dict[str, str]) -> dict[str, Any]:
        """
        Fill merge fields in a document with provided data.
    
        Args:
            filepath: Path to the document or template
            data: Dictionary mapping field names to values
    
        Returns:
            Dictionary with status and modified document info
        """
        logger.info("Filling merge fields", extra={"tool": "fill_merge_fields", "filepath": filepath})
    
        try:
            doc = safe_open_document(filepath)
            fields_filled = 0
    
            # Simple merge field filling by replacing in text
            for paragraph in doc.paragraphs:
                for run in paragraph.runs:
                    for field_name, field_value in data.items():
                        if f"{{{{{field_name}}}}}" in run.text:
                            run.text = run.text.replace(f"{{{{{field_name}}}}}", str(field_value))
                            fields_filled += 1
    
            safe_save_document(doc, filepath)
            logger.info(f"Filled {fields_filled} merge fields", extra={"filepath": filepath})
    
            return {
                "status": "success",
                "filepath": filepath,
                "fields_filled": fields_filled,
                "message": f"Filled {fields_filled} merge fields",
            }
        except DocxMcpError as e:
            logger.warning(e.message, extra={"tool": "fill_merge_fields", "error_code": e.error_code})
            return {"status": "error", "error": e.message, "error_code": e.error_code}
        except Exception as e:
            logger.error(f"Unexpected error filling merge fields: {str(e)}")
            return {"status": "error", "error": str(e)}

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/Andrew82106/LLM_Docx_Agent_MCP'

If you have feedback or need assistance with the MCP directory API, please join our Discord server