Python - 从 SharePoint 站点下载文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/53671547/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 20:20:51  来源:igfitidea点击:

Python - Download files from SharePoint site

pythonsharepoint

提问by DKS

I have a requirement of downloading and uploading the files to Sharepoint sites. This has to be done using python. My site will be as https://ourOrganizationName.sharepoint.com/Followedby Further links Initially I thought I could do this using Request, BeautifulSoup etc., But I am not at all able to go to "Inspect Element" on the body of the site.

我需要下载文件并将其上传到 Sharepoint 站点。这必须使用python来完成。我的网站将作为https://ourOrganizationName.sharepoint.com/Followedby More links 最初我以为我可以使用 Request、BeautifulSoup 等来做到这一点,但我根本无法转到正文上的“检查元素”的网站。

I have tried libraries such as Sharepoint,HttpNtlmAuth,office365 etc., but I am not successful. It always returning 403.

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

I tried google as much I can but again not successful. Even Youtube hasn't helped me.

我尽可能多地尝试谷歌,但再次没有成功。甚至 Youtube 也没有帮助我。

Could anyone help me how to do that? Suggestion on Libraries with documentation link is really appreciated.

谁能帮我怎么做?非常感谢有关带有文档链接的库的建议。

Thanks

谢谢

回答by Vadim Gremyachev

Have you tried Office365-REST-Python-Clientlibrary, it supports SharePoint Online authenticationand allows to download/upload a file as demonstrated below:

您是否尝试过Office365-REST-Python-Clientlibrary,它支持SharePoint Online 身份验证并允许下载/上传文件,如下所示:

Download a file

下载文件

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)

Upload a file

上传一个文件

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

Usage

用法

Install the latest version(from GitHub):

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

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

Refer file_operations.pyfor a more details

请参阅file_operations.py的详细资料