export_google_file
Export Google Workspace files to PDF, XLSX, HTML, Markdown, or plain text formats for offline use or sharing.
Instructions
Export a Google Workspace file to PDF, XLSX, HTML zip, Markdown, or plain text.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| file_id_or_url | Yes | ||
| mime_type | Yes | ||
| output_path | No |
Implementation Reference
- google_workspace_mcp/tools.py:357-387 (handler)Main handler function for 'export_google_file' tool. Decorated with @mcp.tool() for automatic registration. Accepts file_id_or_url, mime_type, and optional output_path parameters. Exports Google Workspace files to PDF, XLSX, HTML zip, Markdown, or plain text formats by calling client.export_drive_file() and saving the content to disk.
@mcp.tool() def export_google_file( file_id_or_url: str, mime_type: str, output_path: str | None = None, ) -> dict[str, Any]: """Export a Google Workspace file to PDF, XLSX, HTML zip, Markdown, or plain text.""" client = get_client() metadata, content = client.export_drive_file(file_id_or_url, mime_type) if output_path: destination = Path(output_path) destination.parent.mkdir(parents=True, exist_ok=True) else: folder = client.export_root folder.mkdir(parents=True, exist_ok=True) extension = { "application/pdf": ".pdf", "application/zip": ".zip", "text/markdown": ".md", "text/plain": ".txt", XLSX_MIME: ".xlsx", }.get(mime_type, ".bin") destination = folder / f"{safe_filename(metadata.get('name', metadata.get('id', 'export')))}{extension}" destination.write_bytes(content) return { "file_id": metadata.get("id"), "name": metadata.get("name"), "mime_type": mime_type, "output_path": str(destination), "bytes": len(content), } - Helper method export_drive_file() that performs the actual Google Drive API call. Extracts the file ID, retrieves metadata, and makes a GET request to the Drive export endpoint with the specified MIME type. Returns a tuple of (metadata, content_bytes).
def export_drive_file(self, file_id_or_url: str, mime_type: str) -> tuple[dict[str, Any], bytes]: file_id = extract_file_id(file_id_or_url) metadata = self.get_drive_file(file_id) content = self._request( "GET", f"https://www.googleapis.com/drive/v3/files/{file_id}/export", scopes=[DRIVE_SCOPE], params={"mimeType": mime_type}, expect_json=False, ) return metadata, content - Helper function extract_file_id() that parses Google file IDs from various URL formats (docs, sheets, drive URLs) or validates standalone IDs. Used by export_drive_file to extract the file identifier from user-provided URLs.
def extract_file_id(value: str, kind: str | None = None) -> str: value = value.strip() if kind == "doc": match = DOC_URL_RE.search(value) if match: return match.group(1) elif kind == "sheet": match = SHEET_URL_RE.search(value) if match: return match.group(1) else: for pattern in (DOC_URL_RE, SHEET_URL_RE, DRIVE_FILE_RE): match = pattern.search(value) if match: return match.group(1) match = DRIVE_OPEN_RE.search(value) if match: return match.group(1) if ID_RE.match(value): return value raise ValueError(f"Could not extract a Google file ID from: {value}") - Helper function safe_filename() that sanitizes filenames by replacing non-alphanumeric characters (except ._-) with underscores. Used to create safe output filenames when exporting Google files.
def safe_filename(name: str) -> str: return re.sub(r"[^A-Za-z0-9._-]+", "_", name).strip("._") or "file"