5 回答

TA貢獻(xiàn)1942條經(jīng)驗(yàn) 獲得超3個(gè)贊
您可以通過(guò)工作表索引訪問(wèn)它,檢查以下代碼......
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貢獻(xiàn)1155條經(jīng)驗(yàn) 獲得超0個(gè)贊
您可以嘗試將組件“sheetname”添加到網(wǎng)址中,如下所示。
https://site/lib/workbook.xlsx#'Sheet1'!A1

TA貢獻(xiàn)1788條經(jīng)驗(yàn) 獲得超4個(gè)贊
我正在使用的更新 ( Office365-REST-Python-Client==2.3.11) 允許更輕松地訪問(wèn) SharePoint 存儲(chǔ)庫(kù)中的 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現(xiàn)在我們有了一個(gè)名為 的Excel 文件的二進(jìn)制副本excel_file。繼續(xù)閱讀它,pd.DataFrame就像存儲(chǔ)在本地驅(qū)動(dòng)器中的普通 Excel 文件一樣直接。例如。:
pd.read_excel(excel_file) # -> pd.DataFrame
因此,如果您對(duì)特定的工作表(例如 )感興趣'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貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超3個(gè)贊
我知道您說(shuō)過(guò)您不能使用 BytesIO 對(duì)象,但是對(duì)于那些像我一樣以 BytesIO 對(duì)象形式讀取文件的人來(lái)說(shuō),您可以使用 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貢獻(xiàn)1856條經(jīng)驗(yàn) 獲得超11個(gè)贊
看來(lái)構(gòu)建的訪問(wèn)數(shù)據(jù)的 URL 不正確。您應(yīng)該在瀏覽器中測(cè)試完整的 URL 是否正常工作,然后修改代碼即可開(kāi)始。您可以嘗試進(jìn)行一些更改,我已經(jīng)驗(yàn)證使用此邏輯形成的 URL 將返回 JSON 數(shù)據(jù)。
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)?
添加回答
舉報(bào)