Skip to main content

企业微信文档 SDK

Project description

企业微信文档 SDK

[!WARNING] 本项目为 100% vibe coding 实验产物,仅用于学习与测试交流;请勿直接用于生产环境项目

wecom-doc-sdk 是一个面向企业微信文档相关服务端 API 的 Python SDK,当前已支持“文档管理”“文档内容”“管理智能表格内容”“微盘上传相关能力”与“设置文档权限”中的部分能力,并提供了 wecom-doc-sdk CLI 用于生成脚手架模板和批量创建智能表格资源。

特性

  • 基于 httpx.Client 的同步客户端,复用连接并统一处理超时
  • 内置 access_token 获取、缓存和提前刷新逻辑
  • 使用 pydantic v2 建模请求与响应,便于校验和序列化
  • 完整类型标注,适合编辑器补全与静态检查
  • 对企业微信业务错误与请求错误做了统一异常封装
  • 提供 wecom-doc-sdk CLI,可生成 YAML 模板并一键创建微盘空间、目录与智能表格
  • 代码和公开模型带中文注释,尽量降低接入与维护成本

当前支持

当前已封装企业微信以下能力:

  • 文档管理
  • 新建文档/表格/智能表格
  • 获取文档基础信息
  • 获取分享链接
  • 重命名文档
  • 删除文档
  • 文档内容
  • 获取文档内容
  • 批量编辑文档内容
  • 管理智能表格内容
  • 子表:添加、删除、更新、查询
  • 视图:添加、删除、更新、查询
  • 字段:添加、删除、更新、查询
  • 记录:添加、删除、更新、查询
  • 编组:添加、更新、删除、查询
  • 微盘与上传
  • 文档图片上传
  • 微盘文件上传
  • 微盘空间:创建、成员添加、成员移除、空间信息查询
  • 微盘文件:创建、分享链接、分块上传初始化、分块上传、分块上传完成
  • 智能表格附件辅助上传
  • 将微盘文件上传后写入附件字段
  • 根据 bytes 内容自动选择直传或分块上传,再写入附件字段
  • 设置文档权限
  • 获取文档权限信息
  • 修改文档加入规则
  • 修改文档成员与权限
  • 修改文档安全设置

对应入口为 WeComClient.documentsWeComClient.document_contentWeComClient.smartsheetWeComClient.uploadsWeComClient.permissions

安装

要求 Python 3.10+

使用 pip

pip install wecom-doc-sdk

使用 uv

uv add wecom-doc-sdk

安装完成后可直接使用 CLI:

wecom-doc-sdk --help

如果是本地开发安装:

uv sync --dev

快速开始

创建客户端

from wecom_doc_sdk import WeComClient

with WeComClient(
    corp_id="YOUR_CORP_ID",
    corp_secret="YOUR_CORP_SECRET",
) as client:
    sheets = client.smartsheet.get_sheet({"docid": "DOCID"})
    print(sheets.ok, sheets.sheet_list)

查询文档权限信息

from wecom_doc_sdk import WeComClient

with WeComClient(
    corp_id="YOUR_CORP_ID",
    corp_secret="YOUR_CORP_SECRET",
) as client:
    response = client.permissions.get_doc_auth({"docid": "DOCID"})

    print(response.ok, response.access_rule, response.secure_setting)

创建文档并获取分享链接

from wecom_doc_sdk import WeComClient
from wecom_doc_sdk.models.enums import DocType

with WeComClient(
    corp_id="YOUR_CORP_ID",
    corp_secret="YOUR_CORP_SECRET",
) as client:
    created = client.documents.create_doc(
        {
            "spaceid": "SPACEID",
            "fatherid": "FATHERID",
            "doc_type": DocType.DOC,
            "doc_name": "项目周报",
        }
    )
    share = client.documents.doc_share({"docid": created.docid})
    print(created.ok, created.docid, share.share_url)

使用 CLI 生成模板并创建资源

先生成一份带注释的模板:

wecom-doc-sdk init-template template.yaml

如果你已经有现成的微盘空间和目录,也可以生成复用模式模板:

wecom-doc-sdk init-template template.yaml --mode use_existing

确认模板内容后,执行脚手架创建微盘空间、目录、智能表格、子表和字段:

wecom-doc-sdk scaffold template.yaml \
  --corp-id YOUR_CORP_ID \
  --corp-secret YOUR_CORP_SECRET

只想预览本次会创建什么资源时,可以先运行:

wecom-doc-sdk scaffold template.yaml \
  --corp-id YOUR_CORP_ID \
  --corp-secret YOUR_CORP_SECRET \
  --dry-run

获取文档内容

from wecom_doc_sdk import WeComClient

with WeComClient(
    corp_id="YOUR_CORP_ID",
    corp_secret="YOUR_CORP_SECRET",
) as client:
    response = client.document_content.get({"docid": "DOCID"})
    print(response.ok, response.content is not None)

使用 Pydantic 模型查询字段

from wecom_doc_sdk import WeComClient
from wecom_doc_sdk.models.fields import GetFieldsRequest

with WeComClient(
    corp_id="YOUR_CORP_ID",
    corp_secret="YOUR_CORP_SECRET",
) as client:
    response = client.smartsheet.get_fields(
        GetFieldsRequest(
            docid="DOCID",
            sheet_id="SHEET_ID",
            limit=100,
        )
    )

    if response.fields:
        for field in response.fields:
            print(field.field_id, field.field_title, field.field_type)

直接使用 dict 新增记录

SDK 的公开 API 同时接受 Pydantic 模型或 dict

from wecom_doc_sdk import WeComClient

with WeComClient(
    corp_id="YOUR_CORP_ID",
    corp_secret="YOUR_CORP_SECRET",
) as client:
    response = client.smartsheet.add_records(
        {
            "docid": "DOCID",
            "sheet_id": "SHEET_ID",
            "records": [
                {
                    "values": {
                        "标题": [{"type": "text", "text": "第一条任务"}],
                        "进度": 50,
                    }
                }
            ],
        }
    )

    print(response.ok, response.records)

上传附件并写入智能表格

如果你已经有 docidsheet_id、附件字段 ID,以及可上传的微盘位置,可以直接让 SDK 完成“上传到微盘 + 写入附件列”这条链路:

from wecom_doc_sdk import WeComClient

with WeComClient(
    corp_id="YOUR_CORP_ID",
    corp_secret="YOUR_CORP_SECRET",
) as client:
    response = client.smartsheet.upload_bytes_and_add_attachment_record(
        docid="DOCID",
        sheet_id="SHEET_ID",
        field_key="ATTACHMENT_FIELD_ID",
        file_name="example.txt",
        file_bytes=b"hello wecom",
        spaceid="SPACEID",
        fatherid="FOLDERID",
    )

    print(response.ok, response.records)

错误处理

SDK 将错误分成两类:

  • WeComAPIError:企业微信接口已返回响应,但 errcode != 0
  • WeComRequestError:网络异常、HTTP 状态异常或响应解析失败

推荐按下面的方式处理:

from wecom_doc_sdk import WeComClient
from wecom_doc_sdk.exceptions import WeComAPIError, WeComRequestError

try:
    with WeComClient(
        corp_id="YOUR_CORP_ID",
        corp_secret="YOUR_CORP_SECRET",
    ) as client:
        client.smartsheet.get_sheet({"docid": "DOCID"})
except WeComAPIError as exc:
    print("业务错误", exc.errcode, exc.errmsg)
    print("原始响应", exc.raw)
except WeComRequestError as exc:
    print("请求失败", exc)
    print("底层原因", exc.cause)

设计约定

  • access_token 仅在服务端获取和缓存,不应返回给前端
  • 业务成功与否始终以 errcode 判断,不应依赖 errmsg 文案
  • 所有请求统一通过 WeComClient.request_json() 注入 access_token
  • 所有模型统一通过 model_dump(by_alias=True, exclude_none=True) 序列化
  • 当前客户端为同步版;如后续需要,可在现有结构上扩展异步能力

导入建议

为避免根包导出过多类型,推荐按职责导入:

  • 根包 wecom_doc_sdkWeComClientAccessTokenProvider
  • 异常:从 wecom_doc_sdk.exceptions 导入 WeComAPIErrorWeComRequestError
  • 文档权限模型:从 wecom_doc_sdk.models.permissions 导入
  • 文档管理模型:从 wecom_doc_sdk.models.documents 导入
  • 文档内容模型:从 wecom_doc_sdk.models.document_content 导入
  • 上传与微盘模型:从 wecom_doc_sdk.models.uploads 导入
  • 子表模型:从 wecom_doc_sdk.models.sheets 导入
  • 视图模型:从 wecom_doc_sdk.models.views 导入
  • 字段模型:从 wecom_doc_sdk.models.fields 导入
  • 记录模型:从 wecom_doc_sdk.models.records 导入
  • 编组模型:从 wecom_doc_sdk.models.groups 导入

例如:from wecom_doc_sdk.models.fields import GetFieldsRequest

项目结构

references/          # 官方文档入口与实现参考索引
src/wecom_doc_sdk/
├── apis/           # 接口封装
├── models/         # Pydantic 模型与枚举
├── client.py       # 客户端、鉴权与统一请求入口
└── exceptions.py   # 异常定义

维护时可参考 references/wecom-docs.md 中整理的官方文档入口与当前实现对应条目。

开发

安装开发依赖:

uv sync --dev

常用检查命令:

uv run ruff check .
uv run black --check .
uv run ty check
uv run pytest

发布

本地构建与检查:

uv build
uvx twine check dist/*

发布到 TestPyPI:

uv publish --index testpypi

发布到 PyPI:

uv publish

项目约定与贡献规范见 CONTRIBUTING.md

适用范围

这个库当前聚焦企业微信文档能力,已覆盖文档管理、文档内容、智能表格内容管理和部分文档权限管理。后续可以在保持现有客户端与模型风格一致的前提下,继续扩展更多企业微信文档相关 API 模块。

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

wecom_doc_sdk-0.5.0.tar.gz (77.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

wecom_doc_sdk-0.5.0-py3-none-any.whl (50.1 kB view details)

Uploaded Python 3

File details

Details for the file wecom_doc_sdk-0.5.0.tar.gz.

File metadata

  • Download URL: wecom_doc_sdk-0.5.0.tar.gz
  • Upload date:
  • Size: 77.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.6 {"installer":{"name":"uv","version":"0.11.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for wecom_doc_sdk-0.5.0.tar.gz
Algorithm Hash digest
SHA256 c21651876b8b14804637015900a275643b9714b9d09a74114bef2065f02af080
MD5 f1bde73af64d625a52f73216449134d3
BLAKE2b-256 b8a8928b409a1b5e3c5bf27ff9e9ba702f9c2908898aa3fef1b59eb6ee8e39d2

See more details on using hashes here.

File details

Details for the file wecom_doc_sdk-0.5.0-py3-none-any.whl.

File metadata

  • Download URL: wecom_doc_sdk-0.5.0-py3-none-any.whl
  • Upload date:
  • Size: 50.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.6 {"installer":{"name":"uv","version":"0.11.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for wecom_doc_sdk-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2efc911e57aa54ab5c6e21e22dbb733ec15ac25a50c0bfc7f0270aa76522dd93
MD5 f270093afae91e82bc2e65d73e615744
BLAKE2b-256 a572358bc89f437b979684a85f6429c4da717949ab5d5f3d0a0167f21f729522

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page