Python-从SharePoint网站下载文件

DKS*_*DKS 3 python sharepoint

我需要将文件下载并上传到Sharepoint网站。这必须使用python完成。我的网站将为https://ourOrganizationName.sharepoint.com/,后接其他链接最初,我认为我可以使用Request,BeautifulSoup等功能来执行此操作,但是我根本无法转到正文中的“检查元素”该网站。

我尝试了诸如Sharepoint,HttpNtlmAuth,office365等的库,但是我不成功。它总是返回403。

我尽我所能尝试了google,但再次失败了。甚至Youtube也帮不了我。

有人可以帮我怎么做吗?带有文档链接的关于库的建议非常感谢。

谢谢

Vad*_*hev 5

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

下载档案

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)
Run Code Online (Sandbox Code Playgroud)

上传一个文件

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
Run Code Online (Sandbox Code Playgroud)

用法

安装最新版本(从GitHub):

pip install git+https://github.com/vgrem/Office365-REST-Python-Client.git
Run Code Online (Sandbox Code Playgroud)

查阅file_operations.py更多细节

  • 仍然无法工作::-))但我发现了它https://github.com/vgrem/Office365-REST-Python-Client/blob/master/examples/sharepoint/file_operations.py (2认同)
  • 它不会让我编辑答案,因为用户不接受编辑。以下是使其正常工作的导入: from office365.runtime.auth.authentication_context import AuthenticationContext from office365.sharepoint.client_context import ClientContext from office365.sharepoint.file import File (2认同)