I'm attempting to read an Excel file from SharePoint using the office365 library in Python. I've tried the following code:
from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.sharepoint.client_context import ClientContext
from office365.sharepoint.files.file import File
import io
import pandas as pd
url = 'https://company.sharepoint.com/:x:/r/sites/folder1/folder1/_layouts/15/Doc.aspx?sourcedoc=%random_letters_and_numbers%7D&file=excel%20file%20name.xlsx&action=default&mobileredirect=true'
username = '[email protected]'
password = 'password'
ctx_auth = AuthenticationContext(url)
if ctx_auth.acquire_token_for_user(username, password):
ctx = ClientContext(url, ctx_auth)
web = ctx.web
ctx.load(web)
ctx.execute_query()
print("Authentication successful")
response = File.open_binary(ctx, url)
bytes_file_obj = io.BytesIO()
bytes_file_obj.write(response.content)
bytes_file_obj.seek(0)
df = pd.read_excel(bytes_file_obj)
However, when I execute the code, I encounter the following error on the last line:
ValueError: File is not a recognized Excel file
Could someone please help me troubleshoot this issue? Is there something I'm missing or doing wrong in my code?
Thank you in advance for your assistance!