【问题标题】:Download or Read Excel file from Organization SharePoint using Python使用 Python 从组织 SharePoint 下载或读取 Excel 文件
【发布时间】:2021-05-07 04:49:12
【问题描述】:

我正在尝试读取存储在 Sharepoint 中的 excel 文件。但我做不到。我已经参考了大多数现有文章,但这并没有解决我的问题。

我引用的代码来自:How to read SharePoint Online (Office365) Excel files in Python with Work or School Account?

Read xlsx stored on sharepoint location with openpyxl in python?

但没有运气。

import io
import json
import pandas as pd
from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.runtime.auth.user_credential import UserCredential
from office365.runtime.http.request_options import RequestOptions
from office365.sharepoint.client_context import ClientContext
from office365.sharepoint.files.file import File
from io import BytesIO

username = '##########################'
password = '##########################'
site_url = "#######################################################"

ctx = ClientContext(site_url).with_credentials(UserCredential(username, password))
request = RequestOptions("{0}/_api/web/".format(site_url))
response = ctx.execute_request_direct(request)
json_data = json.loads(response.content)

我得到的错误:

【问题讨论】:

  • 我前一阵子尝试过这样做,结果只是在本地将sharepoint文件夹与我的机器“同步”,这样更容易读写。

标签: python sharepoint


【解决方案1】:

我希望这对你有用。下载文件后,您可以按照其各自的文件格式阅读它。

from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.sharepoint.client_context import ClientContext
from office365.sharepoint.files.file import File

ctx_auth = AuthenticationContext(url)
ctx_auth.acquire_token_for_user(username, password)   
ctx = ClientContext(url, ctx_auth)
response = File.open_binary(ctx, "/Shared Documents/User Guide.docx")
with open("./User Guide.docx", "wb") as local_file:
    local_file.write(response.content)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-14
    • 1970-01-01
    • 2015-02-15
    • 2019-05-09
    • 1970-01-01
    相关资源
    最近更新 更多