5 回答
TA贡献1942条经验 获得超3个赞
您可以通过工作表索引访问它,检查以下代码......
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))
TA贡献1788条经验 获得超4个赞
我正在使用的更新 ( Office365-REST-Python-Client==2.3.11) 允许更轻松地访问 SharePoint 存储库中的 Excel 文件。
# from original_question import pd,\
# username,\
# password,\
# UserCredential,\
# File,\
# BytesIO
user_credentials = UserCredential(user_name=username,
password=password)
file_url = ('https://sample.sharepoint.com'
'/sites/SAMPLE/{*recursive_folders}'
'/sample_worksheet.xlsx')
## absolute path of excel file on SharePoint
excel_file = BytesIO()
## initiating binary object
excel_file_online = File.from_url(abs_url=file_url)
## requesting file from SharePoint
excel_file_online = excel_file_online.with_credentials(
credentials=user_credentials)
## validating file with accessible credentials
excel_file_online.download(file_object=excel_file).execute_query()
## writing binary response of the
## file request into bytes object
BytesIO现在我们有了一个名为 的Excel 文件的二进制副本excel_file。继续阅读它,pd.DataFrame就像存储在本地驱动器中的普通 Excel 文件一样直接。例如。:
pd.read_excel(excel_file) # -> pd.DataFrame
因此,如果您对特定的工作表(例如 )感兴趣'employee_list',您最好将其阅读为
employee_list = pd.read_excel(excel_file,
sheet_name='employee_list')
# -> pd.DataFrame
或者
data = pd.read_excel(excel_file,
sheet_name=None) # -> dict
employee_list = data.get('employee_list')
# -> [pd.DataFrame, None]
TA贡献1828条经验 获得超3个赞
我知道您说过您不能使用 BytesIO 对象,但是对于那些像我一样以 BytesIO 对象形式读取文件的人来说,您可以使用 arg sheet_namein pd.read_excel:
url = "https://sharepoint.site.com/sites/MySite/MySheet.xlsx"
sheet_name = 'Sheet X'
response = File.open_binary(ctx, relative_url)
bytes_file_obj = io.BytesIO()
bytes_file_obj.write(response.content)
bytes_file_obj.seek(0)
df = pd.read_excel(bytes_file_obj, sheet_name = sheet_name) //call sheet name
TA贡献1856条经验 获得超11个赞
看来构建的访问数据的 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)
添加回答
举报