gpt4 book ai didi

python - 使用 python pandas 读取 sharepoint excel 文件

转载 作者:行者123 更新时间:2023-12-04 19:49:39 29 4
gpt4 key购买 nike

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

#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)

最佳答案

我通过在桌面上打开文件并转到文件 > 信息 > 复制路径来做到这一点。此路径应该有效。

关于python - 使用 python pandas 读取 sharepoint excel 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69488725/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com