如何将 Sharepoint 列表另存为文件?
How to save a Sharepoint list as a file?
我需要将共享点列表保存为 excel/csv 文件。为此,我遵循了以下两个 SO 链接,
Get SharePoint List with Python
我的代码是这样的,
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.")
我收到以下错误:
shareplum.errors.ShareplumRequestError: Shareplum HTTP Post Failed : 403 Client Error: Forbidden for url: https://companyname.sharepoint.com/sites/analyticsandml/_vti_bin/lists.asmx
请帮我解决这个问题。
注意: @Lee_MSFT 运行 您的代码版本,我得到的错误快照如下,
回溯:
使用基于 shareplum 的代码后,出现以下错误,
我的观点: 当您使用 office365 而没有其他附加安全层(如 SecureAuth)时,基于 Shareplum 的解决方案有效。如果启用了 MFA,就像我的情况一样,它会给你无效的凭据错误。
使用 Office365-REST-Python-Client 获取 SharePoint 列表数据并输出为 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("user@xxx.onmicrosoft.com", "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())
更新:
在 python 3.8 中尝试 SharePlum。
import json
import pandas
from shareplum import Site
from shareplum import Office365
authcookie = Office365('https://xxx.sharepoint.com', username='user@xxx.onmicrosoft.com', 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")
调试截图:
我需要将共享点列表保存为 excel/csv 文件。为此,我遵循了以下两个 SO 链接,
Get SharePoint List with Python
我的代码是这样的,
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.")
我收到以下错误:
shareplum.errors.ShareplumRequestError: Shareplum HTTP Post Failed : 403 Client Error: Forbidden for url: https://companyname.sharepoint.com/sites/analyticsandml/_vti_bin/lists.asmx
请帮我解决这个问题。
注意: @Lee_MSFT 运行 您的代码版本,我得到的错误快照如下,
回溯:
使用基于 shareplum 的代码后,出现以下错误,
我的观点: 当您使用 office365 而没有其他附加安全层(如 SecureAuth)时,基于 Shareplum 的解决方案有效。如果启用了 MFA,就像我的情况一样,它会给你无效的凭据错误。
使用 Office365-REST-Python-Client 获取 SharePoint 列表数据并输出为 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("user@xxx.onmicrosoft.com", "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())
更新:
在 python 3.8 中尝试 SharePlum。
import json
import pandas
from shareplum import Site
from shareplum import Office365
authcookie = Office365('https://xxx.sharepoint.com', username='user@xxx.onmicrosoft.com', 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")
调试截图: