Python-从SharePoint si下载文件

2024-05-20 19:23:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要下载文件并将其上载到Sharepoint网站。这必须使用python完成。 我的网站将作为https://ourOrganizationName.sharepoint.com/Followed通过进一步的链接 最初我以为我可以使用请求、美化组等来完成这项工作,但我根本不能去“检查元素”网站的主体。

我尝试过诸如Sharepoint、HttpNtlmAuth、office365等库,但没有成功。它总是返回403。

我尽我所能尝试了谷歌,但还是没有成功。就连Youtube也帮不了我。

有人能帮我怎么做吗?建议图书馆与文献链接是非常感谢。

谢谢


Tags: 文件httpscom元素youtube网站链接建议
1条回答
网友
1楼 · 发布于 2024-05-20 19:23:23

您是否尝试过^{} library,它支持SharePoint Online authentication,并允许下载/上载文件,如下所示:

下载文件

ctx_auth = AuthenticationContext(url)
ctx_auth.acquire_token_for_user(username, password)   
ctx = ClientContext(url, ctx_auth)
response = File.open_binary(ctx, "/Shared Documents/User Guide.docx")
with open("./User Guide.docx", "wb") as local_file:
    local_file.write(response.content)

上载文件

ctx_auth = AuthenticationContext(url)
ctx_auth.acquire_token_for_user(username, password)   
ctx = ClientContext(url, ctx_auth)

path = "./User Guide.docx" #local path
with open(path, 'rb') as content_file:
   file_content = content_file.read()
target_url = "/Shared Documents/{0}".format(os.path.basename(path))  # target url of a file 
File.save_binary(ctx, target_url, file_content) # upload a file

用法

安装最新版本的(来自GitHub):

pip install git+https://github.com/vgrem/Office365-REST-Python-Client.git

有关详细信息,请参阅^{}

相关问题 更多 >