python - 使用 Sharepy 上传多个 Excel 文件到 Sharepoint

标签 python sharepoint

我正在尝试创建一个 python 脚本,将文件从桌面文件夹导入到 Sharepoint 站点文件夹。

我正在使用 Sharepy 来执行此任务。

这是我当前的脚本:

import sharepy
import os
import glob

s = sharepy.connect("https://xxx.sharepoint.com", username='<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="423a3a3a023a3a3a6c212d2f" rel="noreferrer noopener nofollow">[email protected]</a>', password='xxx')

filesToUpload = (glob.glob("C:/Users/xxx/Desktop/xxx/*.xlsx"))

for fileToUpload in filesToUpload:
            headers = {"accept": "application/json;odata=verbose",
            "content-type": "application/x-www-urlencoded; charset=UTF-8"}

            with open(fileToUpload, 'rb') as read_file:
                content = read_file.read()

            p = s.post(f"https://xxx.sharepoint.com/xxx/yyy/_api/web\
            /GetFolderByServerRelativeUrl('/xxx/yyy/Shared Documents/zzz/aaa/')/Files/add(url='fileToUpload',overwrite=true)", data=content, headers=headers)

当我一次执行一个时,它工作得很好,但在这个循环中却不行。目前我的文件夹中有大约 5 个 xlsx Excel 工作表。

如何通过一个请求上传多个文件?

最佳答案

您可以尝试将要导入的文件放入要循环的列表中吗?

试试这个:

import os
import glob

s = sharepy.connect("https://xxx.sharepoint.com", username='<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f58d8d8db58d8d8ddb969a98" rel="noreferrer noopener nofollow">[email protected]</a>', password='xxx')

filesToUpload = [f for f in glob.glob("*.txt")]   #New line

for fileToUpload in filesToUpload:
            headers = {"accept": "application/json;odata=verbose",
            "content-type": "application/x-www-urlencoded; charset=UTF-8"}

            with open(fileToUpload, 'rb') as read_file:
                content = read_file.read()

            p = s.post(f"https://xxx.sharepoint.com/xxx/yyy/_api/web\
            /GetFolderByServerRelativeUrl('/xxx/yyy/Shared Documents/zzz/aaa/')/Files/add(url='fileToUpload',overwrite=true)", data=content, headers=headers)

关于python - 使用 Sharepy 上传多个 Excel 文件到 Sharepoint,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59996180/

相关文章:

sharepoint - 从 SharePoint 在线租户获取所有用户并通过 Powershell 设置用户配置文件属性

.net - 与 LINQ 一起使用的集合类的要求

python - 如何删除数据集中的重复值 : python

python - 分组依据和计数值

asp.net - 动态设置 DataFormWebPart 中 ParameterBinding 的 DefaultValue

javascript - 如何在 Sharepoint 2013 中通过标题获取网站的 URL?

javascript - IE8 无法识别输入文本框

python - 我从网络摄像头抓取帧时得到0字节图像

python - 了解 Keras LSTM ( lstm_text_generation.py ) - RAM 内存问题

python - 根据另一个字符串列表对字符串列表进行排序