我正在尝试创建一个 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/