如何使用 python office 365 rest 在 Sharepoint Online 的列表中上传多个项目
How to upload multiple items in a list in Sharepoint Online using python office 365 rest
我在在线共享点中有一个列表:ListA
此列表有 13 个项目。我还想加,再说3个吧
我还有一个包含 3 列的数据框:标题、科学家、ID
我想创建多个新项目,每行一个,并将它们上传到列表中
df = pd.DataFrame({'Title': ['A', 'B', 'C'], 'Scientist': ['d', 'e', 'f'], 'ID': [1,2,3]})
我已完全通过身份验证。
我使用 add_item() 方法来自:https://github.com/vgrem/Office365-REST-Python-Client/blob/master/office365/sharepoint/lists/list.py
context_auth2 = AuthenticationContext(url=app_settings2['url'])
context_auth2.acquire_token_for_app(client_id=app_settings2['client_id'],
client_secret=app_settings2['client_secret'])
ctx2 = ClientContext(app_settings2['url'], context_auth2)
new_list_ar = ctx2.web.lists.get_by_title("ListA")
new_list_ar_items = new_list_ar.get_items().top(13)
ctx2.load(new_list_ar_items)
ctx2.execute_query() # I get the list items and check their names
new_list_ar.add_item({'Title': df['Title'], 'Scientist': df['Scientist'], 'ID': df['ID']})
ctx2.execute_query()
这行不通。我收到错误:
TypeError: 类型 Series 的 Object 不是 JSON 可序列化的
所以,我想知道是否有办法为数据框中的每一行创建一个新项目到共享点列表
示例测试演示,id由SharePoint自动生成,请勿设置。
import json
import pandas as pd
from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.runtime.client_request import ClientRequest
from office365.sharepoint.client_context import ClientContext
app_settings = {
'url': 'https://xxx.sharepoint.com/sites/lee',
'client_id': 'clientid',
'client_secret': 'secret'
}
context_auth = AuthenticationContext(url=app_settings['url'])
context_auth.acquire_token_for_app(client_id=app_settings['client_id'],
client_secret=app_settings['client_secret'])
ctx = ClientContext(app_settings['url'], context_auth)
list = ctx.web.lists.get_by_title("ListA")
df = pd.DataFrame({'Title': ['A', 'B', 'C'], 'Scientist': ['d', 'e', 'f'], 'ID': [1,2,3]})
for index, row in df.iterrows():
print(row['Title'], row['Scientist'])
list.add_item({'Title': row['Title'], 'Scientist': row['Scientist']})
ctx.execute_query()
我在在线共享点中有一个列表:ListA
此列表有 13 个项目。我还想加,再说3个吧
我还有一个包含 3 列的数据框:标题、科学家、ID
我想创建多个新项目,每行一个,并将它们上传到列表中
df = pd.DataFrame({'Title': ['A', 'B', 'C'], 'Scientist': ['d', 'e', 'f'], 'ID': [1,2,3]})
我已完全通过身份验证。
我使用 add_item() 方法来自:https://github.com/vgrem/Office365-REST-Python-Client/blob/master/office365/sharepoint/lists/list.py
context_auth2 = AuthenticationContext(url=app_settings2['url'])
context_auth2.acquire_token_for_app(client_id=app_settings2['client_id'],
client_secret=app_settings2['client_secret'])
ctx2 = ClientContext(app_settings2['url'], context_auth2)
new_list_ar = ctx2.web.lists.get_by_title("ListA")
new_list_ar_items = new_list_ar.get_items().top(13)
ctx2.load(new_list_ar_items)
ctx2.execute_query() # I get the list items and check their names
new_list_ar.add_item({'Title': df['Title'], 'Scientist': df['Scientist'], 'ID': df['ID']})
ctx2.execute_query()
这行不通。我收到错误: TypeError: 类型 Series 的 Object 不是 JSON 可序列化的
所以,我想知道是否有办法为数据框中的每一行创建一个新项目到共享点列表
示例测试演示,id由SharePoint自动生成,请勿设置。
import json
import pandas as pd
from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.runtime.client_request import ClientRequest
from office365.sharepoint.client_context import ClientContext
app_settings = {
'url': 'https://xxx.sharepoint.com/sites/lee',
'client_id': 'clientid',
'client_secret': 'secret'
}
context_auth = AuthenticationContext(url=app_settings['url'])
context_auth.acquire_token_for_app(client_id=app_settings['client_id'],
client_secret=app_settings['client_secret'])
ctx = ClientContext(app_settings['url'], context_auth)
list = ctx.web.lists.get_by_title("ListA")
df = pd.DataFrame({'Title': ['A', 'B', 'C'], 'Scientist': ['d', 'e', 'f'], 'ID': [1,2,3]})
for index, row in df.iterrows():
print(row['Title'], row['Scientist'])
list.add_item({'Title': row['Title'], 'Scientist': row['Scientist']})
ctx.execute_query()