简体   繁体   English

Python Sharepoint API 认证成功但无法读取 ZC1D81AF5835844B4E9D93DC91

[英]Python Sharepoint API Authentication Successful But Can't Read Excel File

So basically the authentication to my sharepoint is successful, but then Pandas can't read the xlsx file (which is stored as a byte object).所以基本上对我的 sharepoint 的身份验证是成功的,但是 Pandas 无法读取 xlsx 文件(存储为字节对象)。

I get the error: "ValueError: File is not a recognized excel file"我收到错误消息:“ValueError:文件不是可识别的 excel 文件”

Code:代码:

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-name]**-my.sharepoint.com/:x:/p/**[email-prefix]**/EYSZCv_Su0tBkarOa5ggMfsB-5DAB-FY8a0-IKukCIaPOw?e=iW2K6r' # this is just the link you get when clicking "copy link" on sharepoint
username = '...'
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)
df

Any thoughts on to what could be going wrong here?关于这里可能出现什么问题的任何想法?

I also got the same error (& arrived at this page).我也遇到了同样的错误(并到达了这个页面)。

I could solve this, changing the url link.我可以解决这个问题,改变 url 链接。
Using file path (got from 'copy path' on opened excel file), maybe it will work...使用文件路径(从打开的 excel 文件上的“复制路径”获得),也许它会工作......

example:例子:

url = 'https://**[company-name]**-my.sharepoint.com/personal/**[email-prefix]**/Documents/filename.xlsx?web=1'

Osugi's method above worked for me: For added clarity, I had to open the Excel file in the actual Excel application. Osugi 的上述方法对我有用:为了更清楚起见,我必须在实际的 Excel 应用程序中打开 Excel 文件。 not OneDrive.不是 OneDrive。 I did this by clicking File -> info -> Open in Desktop App.我通过单击文件-> 信息-> 在桌面应用程序中打开来做到这一点。

Once in the Excel application, I went File -> info -> Copy path.在 Excel 应用程序中,我进入文件 -> 信息 -> 复制路径。 I pasted that path as my URL and it worked.我将该路径粘贴为我的 URL 并且它有效。

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

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