【问题标题】:Read sharepoint excel file with python pandas使用 python pandas 读取 sharepoint excel 文件
【发布时间】:2021-10-07 23:07:37
【问题描述】:

“我正在尝试使用来自 How to read SharePoint Online (Office365) Excel files into Python specifically pandas with Work or School Account? 答案的代码,但得到 XLRDError: Unsupported format, or corrupt file: Expected BOF record; found b'\r\n

#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 excel pandas sharepoint office365


【解决方案1】:

我通过在桌面中打开文件并转到文件 > 信息 > 复制路径来做到这一点。这条路径应该可行。

【讨论】:

    【解决方案2】:

    看起来您使用的是共享链接而不是文件路径。您需要复制正确的路径。方法如下:

    1. 打开共享点文件夹
    2. 点击文件中的三个点,然后点击详细信息
    3. 向下滚动并复制路径 路径应类似于:'/user/folder/Documents/Target_Excel_File_v4.xlsx'

    使用 sharepoint url 进行身份验证,然后使用复制的路径打开您的二进制文件。

    #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/user/folder'
    path = '/user/folder/Documents/Target_Excel_File_v4.xlsx'
    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, path)
    
    #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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-15
      • 2016-02-01
      • 2023-02-10
      相关资源
      最近更新 更多