【问题标题】:How to download list data from SharePoint Online to a csv (preferably) or json file?如何从 SharePoint Online 下载列表数据到 csv(最好)或 json 文件?
【发布时间】:2020-10-13 16:35:12
【问题描述】:

我已使用 Python 访问 SharePoint Online 中的列表,并希望将列表数据保存到文件(csv 或 json)中以对其进行转换并对某些元数据进行排序以进行迁移

我拥有对我正在连接的 Sharepoint 站点的完全访问权限(客户端 ID、密码..)。

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://company.sharepoint.com/sites/abc',
     'client_id': 'id',
     '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)

获取列表并检查标题:

lists = ctx.web.lists
ctx.load(lists)
ctx.execute_query()
for lista in lists:
    print(lista.properties["Title"])  # this gives me the titles of each list and it works.

lists 是一个 ListCollection 对象

从前面的代码中,我看到我想要获取标题为:“分析A”的列表:

a1 = lists.get_by_title("Analysis A")
ctx.load(a1)
ctx.execute_query()  # a1 is a List item - non-iterable

然后我得到该列表中的数据:

a1w = a1.get_items()
ctx.load(a1w)
ctx.execute_query() # a1w is a ListItemCollection - iterable

想法1:df转json/csv

df1 = pd.DataFrame(a1w) #doens't work)

想法2:

点击此链接:How to save a Sharepoint list as a file?

执行json.loads 命令时出错:

JSONDecodeError: Extra data: line 1 column 5 (char 4)

替代方案:

我尝试了 Shareplum,但无法连接它,就像我使用 office365-python-rest 所做的那样。我的猜测是它没有带有客户端 ID 和客户端密码的授权选项(据我所知)

你会怎么做?还是我错过了什么?

【问题讨论】:

    标签: python json sharepoint sharepoint-online


    【解决方案1】:

    示例测试演示供您参考。

    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")
    items = list.get_items()
    ctx.load(items)
    ctx.execute_query()
    
    dataList = []
    for item in items:
         dataList.append({"Title":item.properties["Title"],"Created":item.properties["Created"]})
         print("Item title: {0}".format(item.properties["Title"]))
    pandas.read_json(json.dumps(dataList)).to_csv("output.csv", index = None,header=True)
    

    【讨论】:

    • 感谢您的解决方案,我试过了,但是生成的 csv 文件是空的。您认为这与我的阅读权限有关吗?它也不会打印任何内容 print("Item EmployeeNumber: {0}".format(item.properties["EmployeeNumber"]))
    【解决方案2】:

    想法 1

    如果没有错误跟踪,很难判断会出现什么问题。但我怀疑这可能与您作为参数传递的格式错误的数据有关。请参阅文档中的 here 以准确了解预期内容。

    还请考虑使用相关的堆栈错误跟踪更新您的问题。

    想法 2

    JSONDecodeError:额外数据:第 1 行第 5 列(字符 4)

    此错误仅表示Json 字符串不是有效格式。您可以使用this 服务来验证 JSON 字符串。这通常会告诉您错误点,然后您可以使用它来手动修复问题。

    如果正在解析的对象是 python 对象,也可能会导致此错误。你可以通过jsonifying 每行来避免这种情况

    data_list= []
    for line in open('file_name.json', 'r'):
        data_list.append(json.loads(line))
    

    这避免了存储中间 python 对象。如果没有任何效果,请参阅此related issue

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-09-22
      • 1970-01-01
      • 1970-01-01
      • 2016-07-23
      • 1970-01-01
      • 1970-01-01
      • 2021-03-31
      相关资源
      最近更新 更多