用Python pandas 阅读SharePoint EXCEL文件

2022-02-27 00:00:00 python pandas excel sharepoint office365

问题描述

我正在尝试使用How to read SharePoint Online (Office365) Excel files into Python specifically pandas with Work or School Account?答案中的此代码,但a获取XLRDError:不支持的格式,或损坏的文件:预期的BOF记录;找到b‘ <;!Doct‘。我想问题出在我放置道路的方式上。有没有人知道如何获得这种Sharepoint路径,如下例所示?我得到的路径看起来更像是这样的";https://company.sharepoint.com/sites/site/Shared%20Documents/Forms/AllItems.aspx";

(&Q;
#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)

解决方案

我是通过在桌面中打开文件并转到文件&>信息&>复制路径来完成此操作的。此路径应该可以工作。

相关文章