Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python-如何阅读Sharepoint excel特定工作表_Python_Json_Python 3.x_Excel_Sharepoint - Fatal编程技术网

Python-如何阅读Sharepoint excel特定工作表

Python-如何阅读Sharepoint excel特定工作表,python,json,python-3.x,excel,sharepoint,Python,Json,Python 3.x,Excel,Sharepoint,在Python中,我利用它来访问和阅读包含许多工作表的excel工作簿 当身份验证成功时,我无法将工作表名称的正确路径附加到文件名,以便通过其名称访问第一个或第二个工作表,这就是为什么工作表的输出不是JSON,而是我的代码无法处理的IO字节 我的最终目标是通过名称“employee_list”访问特定的工作表,并将其转换为JSON或Pandas数据框架以供进一步使用 下面的代码片段- import io import json import pandas as pd from office365

在Python中,我利用它来访问和阅读包含许多工作表的excel工作簿

当身份验证成功时,我无法将工作表名称的正确路径附加到文件名,以便通过其名称访问第一个或第二个工作表,这就是为什么工作表的输出不是JSON,而是我的代码无法处理的IO字节

我的最终目标是通过名称“employee_list”访问特定的工作表,并将其转换为JSON或Pandas数据框架以供进一步使用

下面的代码片段-

import io
import json
import pandas as pd
from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.runtime.auth.user_credential import UserCredential
from office365.runtime.http.request_options import RequestOptions
from office365.sharepoint.client_context import ClientContext
from office365.sharepoint.files.file import File
from io import BytesIO


username = 'abc@a.com'
password = 'abcd'
site_url = 'https://sample.sharepoint.com/sites/SAMPLE/_layouts/15/Doc.aspx?OR=teams&action=edit&sourcedoc={739271873}'      
# HOW TO ACCESS WORKSHEET BY ITS NAME IN ABOVE LINE

ctx = ClientContext(site_url).with_credentials(UserCredential(username, password))
request = RequestOptions("{0}/_api/web/".format(site_url))
response = ctx.execute_request_direct(request)
json_data = json.loads(response.content) # ERROR ENCOUNTERED JSON DECODE ERROR SINCE DATA IS IN BYTES

您可以通过图纸索引访问它,请检查以下代码

import xlrd
  
loc = ("File location") 

wb = xlrd.open_workbook(loc) 
sheet = wb.sheet_by_index(0) 

# For row 0 and column 0 
print(sheet.cell_value(1, 0))

您可以尝试将组件“sheetname”添加到url,如下所示

https://site/lib/workbook.xlsx#'Sheet1'!A1

似乎构造用于访问数据的URL是不正确的。您应该在浏览器中测试完整URL是否正常工作,然后修改代码以继续工作。您可以对此进行一些更改,我已经验证了使用此逻辑形成的URL将返回JSON数据

import io
import json
import pandas as pd
from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.runtime.auth.user_credential import UserCredential
from office365.runtime.http.request_options import RequestOptions
from office365.sharepoint.client_context import ClientContext
from office365.sharepoint.files.file import File
from io import BytesIO


username = 'abc@a.com'
password = 'abcd'
site_url = 'https://sample.sharepoint.com/_vti_bin/ExcelRest.aspx/RootFolder/ExcelFileName.xlsx/Model/Ranges('employee_list!A1%7CA10')?$format=json'      
# Replace RootFolder/ExcelFileName.xlsx with actual path of excel file from the root.
# Replace A1 and A10 with actual start and end of cell range.

ctx = ClientContext(site_url).with_credentials(UserCredential(username, password))
request = RequestOptions(site_url)
response = ctx.execute_request_direct(request)
json_data = json.loads(response.content) 
    


来源:

您想从Microsoft excel中执行此操作吗?不,这是一个基于Sharepoint的协作excel工作表,可在Teamsopen_上访问。工作簿不适用于Sharepoint excel文件url链接能否从Teams/Sharepoint下载电子表格,将其保存在
io.BytesIO
缓冲区中,然后使用
xlrd.open_工作簿请注意,XLRD只支持从版本2开始的XLS文件。考虑使用OpenPyxl(或PANASAS.Read Excel/PANDA Excel文件)。文件可以下载到ByTeSo对象并传递给这些选项中的任何一个。