这个问题与下面的链接非常相似。 如何使用工作或学校帐户在 Python 中阅读 SharePoint Online (Office365) Excel 文件?
本质上,我想将 SharePoint 中的 excel 文件导入熊猫以进行进一步分析。
问题是当我运行下面的代码时,出现以下错误。
XLRDError: Unsupported format, or corrupt file: Expected BOF record; found b'\r\n<!DOCT'
Run Code Online (Sandbox Code Playgroud)
我的代码:
from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.sharepoint.client_context import ClientContext
from office365.sharepoint.file import File
url = 'https://companyname.sharepoint.com/SitePages/Home.aspx'
username = 'fakeaccount@company.com'
password = 'password!'
relative_url = '/Shared%20Documents/Folder%20Number1/Folder%20Number2/Folder3/Folder%20Number%Four/Target_Excel_File_v4.xlsx?d=w8f97c2341898_random_numbers_and_letters_a065c12cbcsf=1&e=KXoU4s'
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()
#this gives me a KeyError: 'Title'
#print("Web title: {0}".format(web.properties['Title']))
print('Authentication Successful')
else:
print(ctx_auth.get_last_error())
import …Run Code Online (Sandbox Code Playgroud)