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 = '[email protected]'
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)?
添加回答
舉報