简体   繁体   English

无法使用 office365 和 Python 从 SharePoint 读取 Excel

[英]Unable to read Excel from SharePoint using office365 with Python

Using this answer , I am attempting to read an Excel document from SharePoint into a pandas dataframe. My code is as follows:使用此答案,我试图将 Excel 文档从 SharePoint 读取到pandas dataframe。我的代码如下:

from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.sharepoint.client_context import ClientContext
from office365.sharepoint.file import File 
import io
import pandas as pd

#target url taken from sharepoint and credentials
url = "https://name1.sharepoint.com/sites/name2/name3/name4.xlsx"
username = 'a.b@name1.com'
password = 'Pa55word'

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)

When it gets to the ctx.execute_query() line, I get this error:当它到达ctx.execute_query()行时,我收到此错误:

ClientRequestException: (None, None, '404 Client Error: Not Found for url: https://name1.sharepoint.com/sites/name2/name3/name4.xlsx/_api/Web')

I have tried different documents, but I get the same response.我尝试了不同的文件,但得到了相同的答复。

In the answer linked at the beginning, the url has a cid parameter, which I am wondering if that is the problem.在开头链接的答案中, url有一个cid参数,我想知道这是否是问题所在。 But I haven't been able to find a value to use as my cid , nor have I been able to find out what a cid is.但是我一直无法找到用作我的cid的值,也无法找出cid是什么。 I think the issue maybe in the url variable, but I'm not exactly sure how.我认为这个问题可能在url变量中,但我不确定如何。 Any suggestions much appreciated!非常感谢任何建议!

It seems that the provided url for context generation is incorrect.似乎为上下文生成提供的 url 不正确。 Please have a try following snnipet:请尝试以下 snnipet:

tenant_url= "https://{tenant}.sharepoint.com"
ctx_auth = AuthenticationContext(tenant_url)

site_url="https://{tenant}.sharepoint.com/sites/{yoursite}"

if ctx_auth.acquire_token_for_user("username","password"):
  request = ClientRequest(ctx_auth)
  options = RequestOptions("{0}/_api/web/".format(site_url))
  options.set_header('Accept', 'application/json')
  options.set_header('Content-Type', 'application/json')
  data = request.execute_request_direct(options)
  s = json.loads(data.content)
  web_title = s['Title']
  print("Web title: " + web_title)
else:
  print(ctx_auth.get_last_error())

Full code: https://github.com/kongmengfei/sharedproject/blob/master/PythonConsole-uploadfileSPO/PythonApplication2/ctx_auth_acquire_token.py完整代码: https://github.com/kongmengfei/sharedproject/blob/master/PythonConsole-uploadfileSPO/PythonApplication2/ctx_auth_acquire_token.py

BR BR

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 python office 365 将文件上传到 sharepoint - upload file to sharepoint using python office 365 尝试从 python 对 Z90FCA41331834CCB6DD9B9663FA747C3 发送 email 时出现 smtp 错误 - smtp errors when trying to send email from python vis office365 Python:如何将电子邮件文本正文传递给Office365电子邮件 - Python: How to pass email text body to Office365 Email SharePoint 在线:使用 Office365-REST-Python-Client 库获取站点用户 ID - SharePoint online: Get site user id using Office365-REST-Python-Client library 使用 python Office 365 API 获取 Sharepoint 文件的最后修改日期/时间 - Get the last modified date/time for a Sharepoint File using python Office 365 API 使用Python发送时,Office365邮件加密去除电子邮件附件 - Office365 message encryption strips out email attachments when sending with Python 如何在python3.6中的AWS Lambda中存储和访问microsoft office365帐户令牌 - How to store and access microsoft office365 account token inside AWS Lambda in python3.6 无法从Python 3.6读取Excel文件 - Unable to read excel file from Python 3.6 ImportError:无法使用Office 365项目从“O365”导入名称“帐户” - ImportError: cannot import name 'Account' from 'O365' using Office 365 Project Python - 如何阅读 Sharepoint excel 表特定工作表 - Python - how to read Sharepoint excel sheet specific worksheet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM