简体   繁体   English

使用 Python 从组织 SharePoint 下载或读取 Excel 文件

[英]Download or Read Excel file from Organization SharePoint using Python

I am trying to read the excel file that I stored in Sharepoint.我正在尝试读取存储在 Sharepoint 中的 excel 文件。 But I wasn't able to.但我做不到。 I have referred to most of the existing articles but that didn't solve my issue.我已经参考了大多数现有文章,但这并没有解决我的问题。

Code I referenced From: How to read SharePoint Online (Office365) Excel files in Python with Work or School Account?我引用的代码来自: 如何使用工作或学校帐户阅读 Python 中的 SharePoint Online (Office365) Excel 文件?

Read xlsx stored on sharepoint location with openpyxl in python? 使用 python 中的 openpyxl 读取存储在 sharepoint 位置的 xlsx?

But no luck.但没有运气。

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)

Error I am getting:我得到的错误: 在此处输入图像描述

I hope this works for you.我希望这对你有用。 Once you download the file you can read it as per its respective file format.下载文件后,您可以根据其各自的文件格式阅读它。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM