如何使用工作或学校帐户将SharePoint Online(Office365)Excel文件读入Python?
这个问题与下面的链接非常相似。 基本上,我想将一个excel文件从SharePoint导入pandas,以便进一步分析 问题是,当我运行下面的代码时,会出现以下错误如何使用工作或学校帐户将SharePoint Online(Office365)Excel文件读入Python?,python,excel,pandas,sharepoint,Python,Excel,Pandas,Sharepoint,这个问题与下面的链接非常相似。 基本上,我想将一个excel文件从SharePoint导入pandas,以便进一步分析 问题是,当我运行下面的代码时,会出现以下错误 XLRDError: Unsupported format, or corrupt file: Expected BOF record; found b'\r\n<!DOCT' 对于在本期中最终与我一样的读者,我发现必须将完整URL路径设置为文件,而不仅仅是路径: #import all the libraries fro
XLRDError: Unsupported format, or corrupt file: Expected BOF record; found b'\r\n<!DOCT'
对于在本期中最终与我一样的读者,我发现必须将完整URL路径设置为
文件
,而不仅仅是路径:
#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)
也许值得注意的是,官方存储库中有许多关于sharepoint、drive和团队常见操作的示例 安装注意事项: pip安装Office365 REST Python客户端
还有一个office365软件包,但上面的似乎是正确的您使用的是Anaconda吗?我试图在Anaconda()中安装office365库,但失败。遇到上述问题comment@papelr这不是评论的目的,也不是它们的工作原理。将你的问题作为新问题发布。我没有报告你的评论。我添加了一条注释以帮助您获得答案。为什么我会出现此错误?这是一个excel文件。。
#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)