SuperAPI AI 工具集

google-docs

办公与效率

查看项目

更新于 10-15 11:24

技能介绍

用途

google-docs 是一个用于读取、写入、创建和管理 Google Docs 的技能(skill),通过 OAuth 认证访问 Google Docs API 与 Drive API,适合以脚本或程序化方式自动处理 Google 文档。

主要能力

该技能提供以下操作:

  • 读取(Read):通过 read_document() 获取文档内容。
  • 信息(Info):通过 get_document_info() 获取文档标题、ID 和 URL。
  • 创建(Create):通过 create_document() 创建新文档。
  • 复制(Copy):通过 copy_document() 复制文档。
  • 重命名(Rename):通过 rename_document() 修改文档标题。
  • 插入(Insert):通过 insert_text() 在指定位置插入文本。
  • 追加(Append):通过 append_text() 在文档末尾添加文本。
  • 替换(Replace):通过 replace_all_text() 执行查找并替换。
  • 导出(Export):通过 export_document() 导出为 text、HTML、PDF 或 DOCX 格式。
  • 列表(List):通过 list_documents() 列出可访问的文档。

使用方式

预检(始终优先执行)

每次使用前需运行以下命令检查认证状态:

python3 00-system/skills/google/google-master/scripts/google_auth.py --check --service docs

退出码含义:

  • 0:可以继续处理用户请求。
  • 1:需要登录,运行 python3 00-system/skills/google/google-master/scripts/google_auth.py --login。
  • 2:缺少凭据或依赖,参见 setup-guide.md。

常用命令示例

读取文档:

python3 00-system/skills/google/google-docs/scripts/docs_operations.py read <document_id>

创建文档:

python3 00-system/skills/google/google-docs/scripts/docs_operations.py create "My Document" --content "Initial content here"

插入文本:

python3 00-system/skills/google/google-docs/scripts/docs_operations.py insert <document_id> "Text to insert" --index 1

追加文本:

python3 00-system/skills/google/google-docs/scripts/docs_operations.py append <document_id> "Text to append at end"

查找并替换:

python3 00-system/skills/google/google-docs/scripts/docs_operations.py replace <document_id> "old text" "new text"

导出文档:

python3 00-system/skills/google/google-docs/scripts/docs_operations.py export <document_id> --format pdf --output ./report.pdf

列出文档:

python3 00-system/skills/google/google-docs/scripts/docs_operations.py list --query "report"

复制文档:

python3 00-system/skills/google/google-docs/scripts/docs_operations.py copy <document_id> "Copy of My Document"

重命名文档:

python3 00-system/skills/google/google-docs/scripts/docs_operations.py rename <document_id> "New Title"

文档 ID 获取方式

文档 ID 位于 Google Docs 的 URL 中:

https://docs.google.com/document/d/[DOCUMENT_ID]/edit

编程接口示例

生成报告并保存到 Google Docs:

from docs_operations import create_document, append_text

doc = create_document("Weekly Report - Jan 2024")
append_text(doc['document_id'], "Key findings from this week...")
print(f"Report created: {doc['url']}")

基于模板创建文档并替换占位符:

from docs_operations import copy_document, replace_all_text

new_doc = copy_document(template_id, "Invoice #1234")
replace_all_text(new_doc['document_id'], "{{CLIENT}}", "Acme Corp")
replace_all_text(new_doc['document_id'], "{{AMOUNT}}", "$5,000")

首次配置

  1. 安装依赖:
pip install google-auth google-auth-oauthlib google-api-python-client
  1. 在 Google Cloud Console 创建 OAuth 凭据(启用 Google Docs API 与 Drive API,选择 "Desktop app")。

  2. 在 Nexus 根目录的 .env 文件中添加:

GOOGLE_CLIENT_ID=your-client-id.apps.googleusercontent.com
GOOGLE_CLIENT_SECRET=your-client-secret
GOOGLE_PROJECT_ID=your-project-id
  1. 运行登录命令:
python3 00-system/skills/google/google-master/scripts/google_auth.py --login

适用场景

  • 通过脚本自动生成报告并保存到 Google Docs。
  • 基于模板复制文档并批量替换占位符内容(如发票、合同)。
  • 批量读取、导出或整理 Google Docs 文档。
  • 需要在自动化流程中操作 Google Docs 的开发场景。

限制

  • 使用前必须完成 OAuth 认证配置,否则无法调用。
  • 需要安装指定的 Python 依赖包。
  • 需在 Google Cloud Console 中创建凭据并启用 Google Docs API 与 Drive API。
  • 更多常见错误与解决方案参见 error-handling 参考文档;首次设置参见 setup-guide 参考文档。

关于该技能的收费、价格、平台支持范围及实际使用效果,当前资料暂未提供。