protect_document
Add a password to secure a Word document, ensuring only authorized users can access its content. Ideal for safeguarding sensitive information.
Instructions
Add password protection to a Word document.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| filename | Yes | ||
| password | Yes |
Implementation Reference
- The core handler function that implements password-based encryption protection for Word documents using msoffcrypto library. Handles file reading, encryption, writing back, error recovery, and metadata cleanup.async def protect_document(filename: str, password: str) -> str: """Add password protection to a Word document. Args: filename: Path to the Word document password: Password to protect the document with """ 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: return f"Cannot protect document: {error_message}" try: # Read the original file content with open(filename, "rb") as infile: original_data = infile.read() # Create an msoffcrypto file object from the original data file = msoffcrypto.OfficeFile(io.BytesIO(original_data)) file.load_key(password=password) # Set the password for encryption # Encrypt the data into an in-memory buffer encrypted_data_io = io.BytesIO() file.encrypt(password=password, outfile=encrypted_data_io) # Overwrite the original file with the encrypted data with open(filename, "wb") as outfile: outfile.write(encrypted_data_io.getvalue()) base_path, _ = os.path.splitext(filename) metadata_path = f"{base_path}.protection" if os.path.exists(metadata_path): os.remove(metadata_path) return f"Document {filename} encrypted successfully with password." except Exception as e: # Attempt to restore original file content on failure try: if 'original_data' in locals(): with open(filename, "wb") as outfile: outfile.write(original_data) return f"Failed to encrypt document {filename}: {str(e)}. Original file restored." else: return f"Failed to encrypt document {filename}: {str(e)}. Could not restore original file." except Exception as restore_e: return f"Failed to encrypt document {filename}: {str(e)}. Also failed to restore original file: {str(restore_e)}"
- word_document_server/main.py:773-777 (registration)MCP tool registration in the main server file. Defines the tool entrypoint with @mcp.tool() decorator and delegates execution to the implementation in protection_tools.py.@mcp.tool() async def protect_document(filename: str, password: str): """Add password protection to a Word document.""" return await protection_tools.protect_document(filename, password)
- word_document_server/tools/__init__.py:28-31 (registration)Import statement exposing the protect_document function from protection_tools.py for use in the tools package, facilitating access in main.py.from word_document_server.tools.protection_tools import ( protect_document, add_restricted_editing, add_digital_signature, verify_document )