簡體   English   中英

使用 python pandas 讀取 sharepoint excel 文件

[英]Read sharepoint excel file with python pandas

“我正在嘗試使用如何將 SharePoint Online (Office365) Excel 文件中的代碼讀入 Python,特別是帶有工作或學校帳戶的熊貓?答案但得到 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)

我通過在桌面中打開文件並轉到文件>信息>復制路徑來做到這一點。 這條路應該行得通。

看起來您使用的是共享鏈接而不是文件路徑。 您需要復制正確的路徑。 就是這樣:

  1. 打開共享點文件夾
  2. 單擊文件中的 3 個點,然后單擊詳細信息
  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, sheet_name = None)
print(df)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM