如何使用python-sharepoint库下载文件

时间:2015-06-24 12:37:30

标签: python sharepoint sharepoint-2013

我正在使用此库https://github.com/ox-it/python-sharepoint连接到SharePoint列表。我可以进行身份​​验证,访问列表字段,包括我想要的文件的完整URL,看起来这个库确实有is_file()open()方法,但我不明白如何调用它们。 任何建议表示赞赏!

from sharepoint import SharePointSite, basic_auth_opener 

opener = basic_auth_opener(server_url, "domain/username", "password")
site = SharePointSite(server_url, opener)

sp_list = site.lists['ListName']
for row in sp_list.rows:
       print row.id, row.Title, row.Author['name'], row.Created, row.EncodedAbsUrl
       #download file
       #row.open() ??

引用自述文件:

  

对文档库的支持有限,但SharePointListRow   对象确实支持is_file()方法和open()方法   访问文件数据。

1 个答案:

答案 0 :(得分:3)

基本上,您在列表行(类型为SharePointListRow)上调用这些方法。 open()方法实际上是 urllib2 的开启方法,你通常会这样使用:

import urllib2
opener = urllib2.build_opener()
response = opener.open('http://www.example.com/')
print ('READ CONTENTS:', response.read())
print ('URL          :', response.geturl())
# ....

所以你应该能够像这样使用它(虽然我没有任何Sharepoint站点来检查):

from sharepoint import SharePointSite, basic_auth_opener 

opener = basic_auth_opener(server_url, "domain/username", "password")
site = SharePointSite(server_url, opener)

sp_list = site.lists['ListName']
for row in sp_list.rows():                # <<<
   print row.id, row.Title, row.Author['name'], row.Created, row.EncodedAbsUrl
   # download file here 

   print ( "This row: ", row.name() )     # <<<
   if row.is_file():                      # <<<
      response = row.open()               # <<<
      file_data = response.read()         # <<<

      # process the file data, e.g. write to disk