Skip to main content
Glama

add_image_caption

Add captions to images in Word documents by specifying image index, text, and type (Figure, Table, or Equation) for better document organization and accessibility.

Instructions

Add a caption to an image in the document.

Args: filepath: Path to the document image_index: Index of the image (0-based) caption_text: Caption text caption_type: Type of caption (Figure, Table, Equation)

Returns: Dictionary with status

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
filepathYes
image_indexYes
caption_textYes
caption_typeNoFigure

Implementation Reference

  • The implementation of the add_image_caption tool, which adds a caption to an image in a Word document.
    def add_image_caption(
        filepath: str,
        image_index: int,
        caption_text: str,
        caption_type: str = "Figure",
    ) -> dict[str, Any]:
        """
        Add a caption to an image in the document.
    
        Args:
            filepath: Path to the document
            image_index: Index of the image (0-based)
            caption_text: Caption text
            caption_type: Type of caption (Figure, Table, Equation)
    
        Returns:
            Dictionary with status
        """
        logger.info("Adding image caption", extra={"tool": "add_image_caption", "filepath": filepath})
    
        try:
            doc = safe_open_document(filepath)
    
            # Find image location (simplified - assumes last paragraph with image)
            # In a production system, would need more robust image tracking
            if len(doc.paragraphs) > 0:
                # Add caption paragraph after last paragraph
                caption_paragraph = doc.add_paragraph()
                caption_run = caption_paragraph.add_run(f"{caption_type}: {caption_text}")
                caption_run.italic = True
                caption_paragraph.style = "Caption"
    
                safe_save_document(doc, filepath)
                logger.info("Caption added successfully", extra={"filepath": filepath})
    
                return {
                    "status": "success",
                    "filepath": filepath,
                    "caption": caption_text,
                    "message": "Caption added successfully",
                }
            else:
                raise DocumentError("Document has no paragraphs", filepath)
        except DocxMcpError as e:
            logger.warning(e.message, extra={"tool": "add_image_caption", "error_code": e.error_code})
            return {"status": "error", "error": e.message, "error_code": e.error_code}
        except Exception as e:
  • The registration of the add_image_caption tool using the @app.tool() decorator.
    @app.tool()

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