使用Python从Sharepoint下载文件

问题描述 投票:0回答:1

我正试图用office365模块从Sharepoint下载一个excel文件。这是我的代码。

   from office365.runtime.auth.authentication_context import AuthenticationContext
   from office365.sharepoint.client_context import ClientContext
   from office365.sharepoint.file import File
   app_settings = {
     'url': 'https://xxxxx/sites/DownloadFiles',
     'client_id': 'xxxxxx',
     'client_secret': 'xxxxxx',
   }
   if name == 'main':
      ctx_auth = AuthenticationContext(url=app_settings['url'])
      ctx_auth.acquire_token_for_app(client_id=app_settings['client_id'], 
      client_secret=app_settings['client_secret'])
      ctx = ClientContext(app_settings['url'], ctx_auth)

      path = "F:\myexcel.xlsx"
      response = File.open_binary(ctx, "/Shared%20Documents/myexcel.xlsx")
      response.raise_for_status()
      with open(path, "wb") as local_file:
           local_file.write(response.content)

当我运行该代码时,我得到以下错误。

400 Client Error: Bad Request for url: https://xxx/DownloadFiles/_api/web/getfilebyserverrelativeurl('/Shared%20Documents/myexcel.xlsx')/%5C$value
python python-3.x sharepoint office365api
1个回答
0
投票

我能够在我的服务器上重现同样的问题。SPO. enter image description here

请修改如下代码来解决。

response = File.open_binary(ctx, "/sites/{abc}/Shared%20Documents/source.txt")

如我想从一个网站下载文件,如"https:/xxxx.sharepoint.comsitesabc。",服务器相关url是 "sitesabc"

你可以通过'_spPageContextInfo'对象获得serverrelativeurl。

enter image description here

还有一个SharePoint库''_spPageContextInfo共享梅花',提供了一些简单的操作文件的方法,你可以试一试。

祝贺你,江必克

© www.soinside.com 2019 - 2024. All rights reserved.