本文介绍了如何将 Sharepoint 列表保存为文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将共享点列表保存为 excel/csv 文件.为此,我遵循了以下两个 SO 链接,

追溯:使用基于 shareplum 的代码后,出现以下错误,

我的观点:当您使用 office365 而没有其他附加安全层(如 SecureAuth)时,基于 Shareplum 的解决方案有效.如果启用了 MFA,就像在我的例子中一样,它会给你无效凭据错误.

解决方案

使用

I need to save a sharepoint list as an excel/ csv file. To do so, I have followed following two SO links,

Get SharePoint List with Python

SharePlum error : "Can't get User Info List"

My code look like this,

import pandas as pd
from shareplum import Site
from requests_ntlm import HttpNtlmAuth

cred = HttpNtlmAuth("email_id", "password")
site = Site('https://companyname.sharepoint.com/sites/analyticsandml', auth=cred)

sp_list = site.List('Client Office List') # this creates SharePlum object
data = sp_list.GetListItems('All Items') # this will retrieve all items from list

data_df = pd.DataFrame(data[0:])
data_df.to_excel("data.xlsx")

print("Content of share point list is saved in a file.")

I'm getting the error below:

shareplum.errors.ShareplumRequestError: Shareplum HTTP Post Failed : 403 Client Error: Forbidden for url: https://companyname.sharepoint.com/sites/analyticsandml/_vti_bin/lists.asmx

Please help me with this.

Note: @Lee_MSFT on running your version of code, I get the error snapshot of which is below,

Traceback:After using the shareplum based code, I get the following error,

My views: Shareplum based solution works when you use office365 with no other added layer of security like SecureAuth. If MFA is enabled, like in my case, it will give you invalid credentials error.

解决方案

Sample demo to use Office365-REST-Python-Client to get SharePoint list data and output as excel.

import json
import pandas
from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.runtime.client_request import ClientRequest
from office365.runtime.utilities.request_options import RequestOptions

url="https://xxx.sharepoint.com/"
ctx_auth = AuthenticationContext(url)
if ctx_auth.acquire_token_for_user("[email protected]", "password"):
  request = ClientRequest(ctx_auth)
  options = RequestOptions("{0}/sites/lee/_api/web/lists/getByTitle('ListA')/items?$select=ID,Title,ProjectType".format(url))
  options.set_header('Accept', 'application/json; odata=minimalmetadata')
  options.set_header('Content-Type', 'application/json')
  data = request.execute_request_direct(options)
  s = json.loads(data.content)
  data=s['value']
  pandas.read_json(json.dumps(data)).to_excel("output.xlsx")
  print("output")

else:
  print (ctx_auth.get_last_error())

Update:

Tried SharePlum in python 3.8.

import json
import pandas
from shareplum import Site
from shareplum import Office365

authcookie = Office365('https://xxx.sharepoint.com', username='[email protected]', password='password').GetCookies()
site = Site('https://xxx.sharepoint.com/sites/lee/', authcookie=authcookie)
sp_list = site.List('ListA')
data = sp_list.GetListItems('All Items')
pandas.read_json(json.dumps(data)).to_excel("output.xlsx")

Debug screenshot:

这篇关于如何将 Sharepoint 列表保存为文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 05:50