python - Python Sharepoint API 身份验证成功但无法读取 Excel 文件
问题描述
所以基本上对我的共享点的身份验证是成功的,但是 Pandas 无法读取 xlsx 文件(存储为字节对象)。
我收到错误:“ValueError:文件不是可识别的 excel 文件”
代码:
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
关于这里可能出现什么问题的任何想法?
解决方案
上面大杉的方法对我有用!为了更加清楚:我必须在实际的 Excel 应用程序中打开 Excel 文件,而不是 OneDrive。我通过单击文件-> 信息-> 在桌面应用程序中打开来做到这一点。
进入 Excel 应用程序后,我转到文件 -> 信息 -> 复制路径。我将该路径粘贴为我的 URL 并且它有效。
推荐阅读
- node.js - 执行 react-native-init 命令时出错
- facebook - 如何允许 Facebook 同时从 localhost 和托管生产域的开发中登录?
- java - 如何在Java中保存字体文件?
- javascript - 如何格式化作为对象数组中的值的日期
- java - 尝试在 Pi 上的 java 中的 RXTX 代码中设置 lib 路径
- python-3.x - 如何在python中选择要平均的矩阵位置?
- python - 如何在 macos 上将 python scipts 安装到 /usr/local/bin?
- c# - 在 Asp.net Core MVC 中执行 POST 时填充为 NULL 的 IFormFile 属性类型
- postgresql - 如何连接postgres中的非零列名?
- ckeditor - 带cdn的行距