首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何读取SharePoint在线(Office365)的Excel文件到Python中,特别是有工作或学校账户的熊猫?

如何读取SharePoint在线(Office365)的Excel文件到Python中,特别是有工作或学校账户的熊猫?
EN

Stack Overflow用户
提问于 2019-03-13 03:27:59
回答 2查看 19.3K关注 0票数 6

这个问题与下面的链接非常相似。How to read SharePoint Online (Office365) Excel files in Python with Work or School Account?

从本质上说,我想从SharePoint导入一个excel文件到pandas中进行进一步的分析。

问题是当我运行下面的代码时,我得到了以下错误。

代码语言:javascript
复制
XLRDError: Unsupported format, or corrupt file: Expected BOF record; found b'\r\n<!DOCT'

我的代码:

代码语言:javascript
复制
from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.sharepoint.client_context import ClientContext
from office365.sharepoint.file import File 

url = 'https://companyname.sharepoint.com/SitePages/Home.aspx'
username = 'fakeaccount@company.com'
password = 'password!'
relative_url = '/Shared%20Documents/Folder%20Number1/Folder%20Number2/Folder3/Folder%20Number%Four/Target_Excel_File_v4.xlsx?d=w8f97c2341898_random_numbers_and_letters_a065c12cbcsf=1&e=KXoU4s'


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()
  #this gives me a KeyError: 'Title'
  #print("Web title: {0}".format(web.properties['Title']))
  print('Authentication Successful')
else:
  print(ctx_auth.get_last_error())


import io
import pandas as pd

response = File.open_binary(ctx, relative_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 file into pandas dataframe
df = pd.read_excel(bytes_file_obj)

print(df)
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-03-28 22:53:36

对于那些像我一样在这个问题上结束的人,我发现必须将完整的URL路径指向File,而不仅仅是路径:

代码语言:javascript
复制
#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)
票数 13
EN

Stack Overflow用户

发布于 2020-09-08 19:49:08

也许值得注意的是,官方存储库包含了许多关于sharepoint、drive和团队的常见操作的示例。

安装注意事项:

pip install Office365-REST-Python-Client

还有一个office365包,但上面的包看起来是正确的enter link description here

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55129328

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档