尝试使用 python 从 SharePoint Online 读取 Excel 文件时出现 BadZipFile 错误

问题描述 投票:0回答:1

我正在尝试在 python 上读取 SharePoint excel 文件。我正在使用 jupyter 笔记本。身份验证成功并打印出身份验证成功。但是,在我尝试使用 pandas 读取文件的部分,我收到错误消息 BadZipFile: File is not a zip file

这是我的代码

from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.sharepoint.client_context import ClientContext
from office365.sharepoint.files.file import File

url = 'the sharepoint excel file url'
username = 'my work email'
password = 'my 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')

import io
import pandas as pd

bytes_file_obj = io.BytesIO()
bytes_file_obj.write(response.content)
bytes_file_obj.seek(0)


df = pd.read_excel(bytes_file_obj,engine='openpyxl')
print(df)'

我想知道如何修复此错误或如何调整我的代码以避免此错误。而且我认为我无法在 SharePoint 上压缩该文件。我尝试直接从 SharePoint 读取它,而不是先将其下载到我的桌面上。我该如何解决这个问题?

python-3.x pandas sharepoint-online
1个回答
0
投票

我也遇到了同样的问题,这个解决方案对我有用。

我必须通过在桌面中打开文件并复制“info”下的路径来从 Excel 中获取文件的路径。

然后将路径粘贴到您的 url 变量中:

url = '路径在此处'

© www.soinside.com 2019 - 2024. All rights reserved.