You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Python提取企业SharePoint数据?替代Power Automate方案

用Python提取企业SharePoint数据的可行方法

下面是几种成熟的实现方案,适配企业级SharePoint环境:

方案一:使用Office365-REST-Python-Client封装库

这是最省心的方式,库已封装SharePoint API的大部分操作,无需手动处理复杂的认证和请求逻辑。

  1. 安装依赖
pip install office365-rest-python-client
  1. 用户名密码认证(适合交互式脚本)
    如果你的账号有SharePoint站点访问权限,直接用账号密码初始化上下文:
from office365.sharepoint.client_context import ClientContext
from office365.runtime.auth.user_credential import UserCredential

# 替换为你的实际信息
site_url = "https://your-tenant.sharepoint.com/sites/your-target-site"
username = "your-work-account@your-tenant.com"
password = "your-account-password"

# 建立连接
ctx = ClientContext(site_url).with_credentials(UserCredential(username, password))

# 获取目标列表数据(示例:读取名为"CustomerList"的列表)
target_list = ctx.web.lists.get_by_title("CustomerList")
items = target_list.items.get().execute_query()

# 遍历输出数据
for item in items:
    print(f"客户名称: {item.properties['Title']}, 联系邮箱: {item.properties['Email']}")
  1. 应用权限认证(适合无人值守脚本)
    如果需要后台自动运行,建议在Azure AD注册应用,授予SharePoint的Sites.Read.All等权限,然后用应用凭证认证:
from office365.sharepoint.client_context import ClientContext
from office365.runtime.auth.client_credential import ClientCredential

# 替换为你的应用和站点信息
client_id = "azure-ad-app-client-id"
client_secret = "azure-ad-app-client-secret"
site_url = "https://your-tenant.sharepoint.com/sites/your-target-site"

ctx = ClientContext(site_url).with_credentials(ClientCredential(client_id, client_secret))

# 后续列表读取操作和上面一致

方案二:直接调用SharePoint REST API

如果需要更灵活的控制,可以直接调用官方REST接口,适合自定义需求较多的场景。

  1. 获取访问令牌
    通过Azure AD的令牌端点获取授权令牌:
import requests

tenant_id = "your-tenant-id"
client_id = "azure-ad-app-client-id"
client_secret = "azure-ad-app-client-secret"

token_url = f"https://login.microsoftonline.com/{tenant_id}/oauth2/v2.0/token"
payload = {
    "grant_type": "client_credentials",
    "client_id": client_id,
    "client_secret": client_secret,
    "scope": "https://your-tenant.sharepoint.com/.default"
}

response = requests.post(token_url, data=payload)
access_token = response.json()["access_token"]
  1. 调用API读取数据
    用获取到的令牌请求SharePoint的列表接口:
import json

site_url = "https://your-tenant.sharepoint.com/sites/your-target-site"
list_title = "CustomerList"
api_endpoint = f"{site_url}/_api/web/lists/getbytitle('{list_title}')/items"

headers = {
    "Authorization": f"Bearer {access_token}",
    "Accept": "application/json;odata=nometadata"  # 简化返回格式
}

response = requests.get(api_endpoint, headers=headers)
items = response.json()["value"]

# 处理数据
for item in items:
    print(f"客户名称: {item['Title']}, 联系邮箱: {item['Email']}")

关键注意事项

  • 权限配置:确保账号或应用拥有目标站点和列表的读取权限,企业环境中可能需要管理员协助配置。
  • 敏感信息保护:不要硬编码密码、客户端密钥等敏感数据,建议使用环境变量或企业密钥管理工具存储。
  • 分页处理:如果列表数据量较大(超过100条),Office365库会自动处理分页;直接调用REST API时,需要检查返回结果中的@odata.nextLink字段,循环请求下一页数据。

内容的提问来源于stack exchange,提问作者sonakshi pattnaik

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 07:32:55