"我正在尝试使用来自如何在Python中读取SharePoint Online(Office365) Excel文件,特别是带有工作或学校帐户的pandas?的答案,但我收到了XLRDError:Unsupported format,or corrupt file: Expected BOF record; found b'\r\n<!DOCT'的错误。我认为问题在于我放置路径的方式。是否有人知道如何获取这种类型的SharePoint路径,就像下面的例子一样?" 我得到的看起来更像这样"https://company.sharepoint.com/sites/site/Shared%20Documents/Forms/AllItems.aspx"
#import all the libraries
from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.sharepoint.client_context import ClientContext
from office365.sharepoint.files.file import File
import io
import pandas as pd
#target url taken from sharepoint and credentials
url = 'https://company.sharepoint.com/Shared%20Documents/Folder%20Number1/Folder%20Number2/Folder3/Folder%20Number4/Target_Excel_File_v4.xlsx?cid=_Random_letters_and_numbers-21dbf74c'
username = 'Dumby_account@company.com'
password = 'Password!'
ctx_auth = AuthenticationContext(url)
if ctx_auth.acquire_token_for_user(username, password):
ctx = ClientContext(url, ctx_auth)
web = ctx.web
ctx.load(web)
ctx.execute_query()
print("Authentication successful")
response = File.open_binary(ctx, url)
#save data to BytesIO stream
bytes_file_obj = io.BytesIO()
bytes_file_obj.write(response.content)
bytes_file_obj.seek(0) #set file object to start
#read excel file and each sheet into pandas dataframe
df = pd.read_excel(bytes_file_obj, sheetname = None)