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

如何在Python中将Pandas DataFrame上传至SharePoint列表?

把Python DataFrame上传到SharePoint的两种方案实现及对比

哪个方案更简便?

如果你的需求是将DataFrame的结构化数据导入SharePoint列表作为条目记录,方案2(直接导入数据)更简便,无需额外处理文件转换;如果是要把DataFrame保存为文件(如Excel)存储到SharePoint,方案1更合适(注意:SharePoint列表用于存储结构化条目,文件通常存在文档库,下文会区分两种场景)。


方案2:直接将DataFrame导入SharePoint列表

通过office365-REST-Python-Client库调用SharePoint REST API,把DataFrame每行数据映射为列表的条目记录,这是最贴合"上传至SharePoint列表"需求的方案。

步骤1:安装依赖

pip install office365-rest-python-client pandas

步骤2:实现代码

假设你的SharePoint列表字段与DataFrame列名一一对应(比如列表有Title、Age、Email字段,DataFrame也有同名列):

from office365.sharepoint.client_context import ClientContext
from office365.runtime.auth.user_credential import UserCredential
import pandas as pd

# 配置SharePoint信息
site_url = "https://你的SharePoint站点URL"
username = "你的账号@域名.com"
password = "你的密码"
list_title = "目标SharePoint列表名称"

# 示例DataFrame(替换为你的实际数据)
df = pd.DataFrame({
    "Title": ["Alice", "Bob"],
    "Age": [25, 30],
    "Email": ["alice@example.com", "bob@example.com"]
})

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

# 获取目标列表
target_list = ctx.web.lists.get_by_title(list_title)
ctx.load(target_list)
ctx.execute_query()

# 批量插入DataFrame数据
for _, row in df.iterrows():
    # 映射列表字段与DataFrame列值
    list_item = target_list.items.add({
        "Title": row["Title"],
        "Age": row["Age"],
        "Email": row["Email"]
    })
    ctx.execute_query()

print("DataFrame数据已成功导入SharePoint列表")

注意事项:

  • 需确保DataFrame数据类型与列表字段类型匹配(比如日期格式要转为SharePoint兼容的格式)
  • 数据量较大时,建议使用批量添加接口优化性能,避免循环调用API

方案1:将DataFrame转为BytesIO后上传至SharePoint

如果你的需求是把DataFrame保存为文件(如Excel),上传到SharePoint文档库,或作为列表条目的附件,可按以下方式实现:

场景1:上传到SharePoint文档库

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 = "https://你的SharePoint站点URL"
username = "你的账号@域名.com"
password = "你的密码"
library_title = "Documents"  # 文档库名称
file_name = "dataframe_output.xlsx"

# 将DataFrame转为Excel格式的BytesIO对象
df = pd.DataFrame({
    "Name": ["Alice", "Bob"],
    "Age": [25, 30]
})
output = BytesIO()
df.to_excel(output, index=False)
output.seek(0)  # 重置文件指针到开头

# 连接SharePoint并上传文件
ctx = ClientContext(site_url).with_credentials(UserCredential(username, password))
target_library = ctx.web.lists.get_by_title(library_title)
root_folder = target_library.root_folder

# 执行上传
file = root_folder.upload_file(file_name, output).execute_query()
print(f"文件已上传至:{file.serverRelativeUrl}")

场景2:作为SharePoint列表条目的附件上传

如果要给列表中指定条目添加DataFrame生成的Excel附件:

# 承接上述的连接、DataFrame和BytesIO代码
list_title = "目标SharePoint列表名称"
item_id = 1  # 要添加附件的列表项ID

target_list = ctx.web.lists.get_by_title(list_title)
list_item = target_list.items.get_by_id(item_id)
ctx.load(list_item)
ctx.execute_query()

# 上传附件到该列表项
list_item.attachment_files.add(file_name, output).execute_query()
print(f"附件已添加到列表项ID {item_id}")

内容的提问来源于stack exchange,提问作者Noor Ameera Anas

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.24 17:53:19