I am trying to read some files from a SharePoint 365 location using the office-365 python rest API. I created an app principal and used it to get the files.
I am able to list the directory using the code below, so I have no problem authenticating.
from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.sharepoint.client_context import ClientContext
from office365.runtime.auth.client_credential import ClientCredential
from office365.sharepoint.files.file import File
import io
import pandas as pd
site_url = 'https://*.sharepoint.com/sites/sharepoint_to_dbfs/'
app_principal = {
'client_id': '*',
'client_secret': '*',
}
ctx_auth = AuthenticationContext(site_url)
if ctx_auth.acquire_token_for_app(client_id=app_principal['client_id'], client_secret=app_principal['client_secret']):
ctx = ClientContext(site_url, ctx_auth)
web = ctx.web
ctx.load(web)
ctx.execute_query()
print('Authenticated into sharepoint app for: ',web.properties['Title'])
lists = ctx.web.lists
ctx.load(lists)
ctx.execute_query()
for l in lists:
print("This is a list object: {0}".format(l.properties['Title']))
list_object = ctx.web.lists.get_by_title('Documents')
items = list_object.items
ctx.load(items)
ctx.execute_query()
for l in items:
print("This is a item object: {0}".format(l.properties['Title']))
else:
print(ctx_auth.get_last_error())
However, when trying to get an excel file loaded onto a pandas df I am getting an error.
file_url = "/sites/sharepoint_to_dbfs/Test/Book.xlsx"
response= File.open_binary(ctx, file_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
# load Excel file from BytesIO stream
df = pd.read_excel(bytes_file_obj, engine='openpyxl')
The error is:
File is not a zip file.
When checking the response I got, I see a 404 error and the following content. The file however exists on that path.
b'{"error":{"code":"-2130575338, Microsoft.SharePoint.SPException","message":{"lang":"en-US","value":"The file /sites/sharepoint_to_dbfs/Test/Book.xlsx does not exist."}}}'
I am trying to read some files from a SharePoint 365 location using the office-365 python rest API. I created an app principal and used it to get the files.
I am able to list the directory using the code below, so I have no problem authenticating.
However, when trying to get an excel file loaded onto a pandas df I am getting an error.
The error is:
When checking the response I got, I see a 404 error and the following content. The file however exists on that path.