Skip to main content

企业微信文档 SDK

Project description

企业微信文档 SDK

wecom-doc-sdk 是一个面向企业微信文档相关服务端 API 的 Python SDK,当前已支持“文档 -> 管理智能表格内容”与“文档 -> 设置文档权限”中的部分能力,并为后续扩展更多文档接口保留了清晰的客户端与模型结构。

特性

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

当前支持

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

  • 管理智能表格内容
  • 子表:添加、删除、更新、查询
  • 视图:添加、删除、更新、查询
  • 字段:添加、删除、更新、查询
  • 记录:添加、删除、更新、查询
  • 编组:添加、更新、删除、查询
  • 设置文档权限
  • 获取文档权限信息
  • 修改文档加入规则
  • 修改文档成员与权限
  • 修改文档安全设置

对应入口为 WeComClient.smartsheetWeComClient.permissions

安装

要求 Python 3.10+

使用 pip

pip install wecom-doc-sdk

使用 uv

uv add wecom-doc-sdk

如果是本地开发安装:

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)

使用 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)

错误处理

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.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.3.0.tar.gz (40.0 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.3.0-py3-none-any.whl (27.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: wecom_doc_sdk-0.3.0.tar.gz
  • Upload date:
  • Size: 40.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.0 {"installer":{"name":"uv","version":"0.11.0","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.3.0.tar.gz
Algorithm Hash digest
SHA256 7203171eabad770e2e907047755bffdae3156e191e2dd496cc0d8ff0d3b1300e
MD5 8987807491d81cd679a57dfdc8819029
BLAKE2b-256 d20a15791ba449abf0953a5e32eaad3b79657c22c86f15cb9b150a26aac79b04

See more details on using hashes here.

File details

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

File metadata

  • Download URL: wecom_doc_sdk-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 27.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.0 {"installer":{"name":"uv","version":"0.11.0","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.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 115c833c7adc061d0a77c5aa4491a7c7068d8ddf7f61eaf3284886bc8b721772
MD5 33f579d6c20abd4f6da68965416e1946
BLAKE2b-256 1f45c77c2e8631aa7fbf0b4f3ce94abeb136e4de14fc2b99eb67b7ee8d9fc46a

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