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

使用Python连接SharePoint中Excel文件失败,求解决方案

Python连接SharePoint Excel文件的解决方案

问题原因

pandas的pd.read_excel()函数不支持auth参数,这个参数用于HTTP身份验证,但read_excel并未集成该功能,因此直接传入会触发TypeError。


方法一:通过requests下载文件后读取

先使用requests库带认证请求获取Excel文件内容,再传递给pandas读取:

import pandas as pd
import requests
from io import BytesIO

# 替换为你的实际信息
sharepoint_excel_url = "你的SharePoint Excel文件完整URL"
username = "你的账号"
password = "你的密码"

# 发送带认证的请求获取文件
response = requests.get(sharepoint_excel_url, auth=(username, password))
response.raise_for_status()  # 捕获请求错误

# 将字节内容转为可读取的对象,再用pandas解析
df = pd.read_excel(BytesIO(response.content), sheet_name='Sheet1', header=0)

方法二:使用专用库处理SharePoint Online(推荐)

如果是SharePoint Online环境,使用office365-rest-python-client库能更便捷地处理权限和文件操作:

  1. 先安装依赖:
pip install office365-rest-python-client
  1. 示例代码:
from office365.sharepoint.client_context import ClientContext
from office365.runtime.auth.user_credential import UserCredential
import pandas as pd
from io import BytesIO

# 替换为你的实际信息
site_url = "你的SharePoint站点URL(如https://xxx.sharepoint.com/sites/你的站点)"
username = "你的账号"
password = "你的密码"
file_rel_url = "/Shared Documents/你的Excel文件名.xlsx"  # 文件相对于站点的路径

# 建立SharePoint连接
ctx = ClientContext(site_url).with_credentials(UserCredential(username, password))
file = ctx.web.get_file_by_server_relative_url(file_rel_url)
file_content = file.read().execute_query()

# 解析为DataFrame
df = pd.read_excel(BytesIO(file_content), sheet_name='Sheet1', header=0)

内容的提问来源于stack exchange,提问作者Eduard George Ojog

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.07 21:45:39