Skip to main content

飞书非官方 Python SDK,支持多维表格 (Bitable)、电子表格 (Spreadsheet)、文档 (Doc) 和云空间 (Driver) API

Project description

feishukit

飞书非官方 Python SDK,替代官方 Java 风格的 SDK。

目前支持:

  • 多维表格 Bitable
  • 文档 FeishuDoc
  • 云空间 FeishuDriver
  • 电子表格 FeishuSpreadsheet
  • 用户身份 API FeishuUser

默认推荐使用 tenant access token,也就是应用身份操作飞书资源;只有确实需要“代理当前用户本人”访问资源时,再使用 FeishuUser

feishukit 不内置日志模块,用户可以在调用方法时自行添加日志。

为什么不用官方 SDK?

以“更新一条多维表格记录”为例。

官方 SDK (lark-oapi):

import lark_oapi as lark
from lark_oapi.api.bitable.v1 import *

client = lark.Client.builder() \
    .app_id("YOUR_APP_ID") \
    .app_secret("YOUR_APP_SECRET") \
    .build()

request = UpdateAppTableRecordRequest.builder() \
    .app_token("YOUR_APP_TOKEN") \
    .table_id("YOUR_TABLE_ID") \
    .record_id("YOUR_RECORD_ID") \
    .request_body(AppTableRecord.builder()
        .fields({"状态": "完成"})
        .build()) \
    .build()

response = client.bitable.v1.app_table_record.update(request)

if not response.success():
    raise Exception(f"code: {response.code}, msg: {response.msg}")

feishukit:

from feishukit import Bitable

bt = Bitable(
    app_id="YOUR_APP_ID",
    app_secret="YOUR_APP_SECRET",
    bitable_url="https://xxx.feishu.cn/base/appxxxxx?table=tblxxxxx",
)

bt.update_record("recxxxxx", {"状态": "完成"})

安装

pip install feishukit

唯一运行时依赖为 requests

前置条件

  1. 前往 飞书开发者后台 创建应用,获取 app_idapp_secret
  2. 在应用的权限管理中,按实际要调用的模块申请权限。
  3. 对 Bitable / Spreadsheet / Doc 等云文档资源,将应用添加为目标文档的协作者或文档应用。
  4. 不要把 app_idapp_secret、access token 写入 README、示例输出或提交内容。

tenant access token 和 user access token 是两套身份:

  • tenant access token:应用身份。适合项目服务、同步任务和大多数自动化脚本。
  • user access token:用户身份。适合 notebook、个人脚本,以及必须以当前用户本人权限访问资源的场景。

应用后台里的“应用身份权限”和“用户身份权限”不会自动一致。使用 FeishuUser 前,需要在应用后台开通对应的用户身份权限。

示例脚本

完整示例在 example/

  • bitable_usage.py
  • doc_usage.py
  • driver_usage.py
  • spreadsheet_usage.py
  • user_usage.py

示例中包含写入或删除操作,请只在测试文档、测试表格和测试文件夹上运行。

快速上手

Bitable

多维表格是 feishukit 当前最成熟、最常用的模块。

from feishukit import Bitable

bt = Bitable(
    app_id="cli_xxxx",
    app_secret="xxxx",
    bitable_url="https://xxx.feishu.cn/wiki/xxxxx?table=tblxxxx",
)

records = bt.list_records(size_limit=10)
rid = bt.create_record({"名称": "test", "状态": "进行中"})["record_id"]
bt.update_record(rid, {"状态": "完成"})
bt.delete_record(rid)

详细用法见 Bitable 用户指南

User Token

FeishuUser 使用 device flow 获取 user access token,并能用当前用户身份派生 Bitable、Doc、Spreadsheet、Driver client。

from feishukit import FeishuUser

api = FeishuUser(
    app_id="cli_xxxx",
    app_secret="xxxx",
    token_cache_path="./token_cache.json",
    scopes=None,          # 使用 feishukit 默认常用 user scope
    offline_access=True,  # 缓存 refresh token,避免频繁重新授权
)

user = api.get_current_user()
bt = api.bitable("https://xxx.feishu.cn/base/xxxxx?table=tblxxxx")
doc = api.doc("https://xxx.feishu.cn/wiki/xxxxx")
ss = api.spreadsheet("https://xxx.feishu.cn/sheets/xxxxx?sheet=abc123")
driver = api.driver()

默认仍建议优先使用 tenant token。只有当目标资源必须以“当前用户本人”身份访问,或资源协作权限只授予用户而非应用时,再使用 FeishuUser

详细用法见 User Token 用户指南

Doc

from feishukit import FeishuDoc

doc = FeishuDoc(
    app_id="cli_xxxx",
    app_secret="xxxx",
    doc_url="https://xxx.feishu.cn/wiki/xxxxx",
)

markdown = doc.get_markdown()
doc.append_markdown("## 新增章节\n\n正文")

详细用法见 Doc 用户指南

Driver

from feishukit import FeishuDriver

driver = FeishuDriver(app_id="cli_xxxx", app_secret="xxxx")
root = driver.get_root_folder_meta()
file_token = driver.upload("files", "./report.pdf", parent_type="explorer", parent_node=root["token"])
driver.delete_file(file_token, file_type="file")

详细用法见 Driver 用户指南

Spreadsheet

from feishukit import FeishuSpreadsheet

ss = FeishuSpreadsheet(
    app_id="cli_xxxx",
    app_secret="xxxx",
    spreadsheet_url="https://xxx.feishu.cn/wiki/xxxxx?sheet=abc123",
)

rows = ss.get_rows(sheet_id="abc123", cell_range="A1:C10")
ss.update_values(sheet_id="abc123", cell_range="A1:B2", values=[["Hello", "World"]])

详细用法见 Spreadsheet 用户指南

共享认证

多个业务 client 可以共享同一个 FeishuAPI,避免重复获取 token:

from feishukit import FeishuAPI, Bitable, FeishuDoc

api = FeishuAPI(app_id="cli_xxxx", app_secret="xxxx")
bt = Bitable(bitable_url="...", feishu_api=api)
doc = FeishuDoc(doc_url="...", feishu_api=api)

开发与维护

面向 agent 或维护者的开发规则见 AGENTS.md

更新记录见 CHANGELOG.md

依赖

  • Python >= 3.10
  • requests

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

feishukit-0.0.4.tar.gz (67.4 kB view details)

Uploaded Source

Built Distribution

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

feishukit-0.0.4-py3-none-any.whl (48.1 kB view details)

Uploaded Python 3

File details

Details for the file feishukit-0.0.4.tar.gz.

File metadata

  • Download URL: feishukit-0.0.4.tar.gz
  • Upload date:
  • Size: 67.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.11

File hashes

Hashes for feishukit-0.0.4.tar.gz
Algorithm Hash digest
SHA256 597c55d3be899a4cb1ff0a32f6972ad1a713c7be5faca9a09b75172d3454e416
MD5 96bfac31716e8c6e3d442a33a2491014
BLAKE2b-256 aed9b054e1bf5f5e1dd159966275375ceb96f56163a1f433072d49bf099834d7

See more details on using hashes here.

File details

Details for the file feishukit-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: feishukit-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 48.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.11

File hashes

Hashes for feishukit-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 e08ccfb666e42d6a043864bfcacb0f168825278328b5fe779dd4700fae5c2cf9
MD5 270edeb1738711e79d47c9975744d35b
BLAKE2b-256 91357aa76d9cc3911fd105b2a1c9fa52c61c5eba90fa49066392f7cc5b509fc6

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