Pandas: Problem while trying reading Excel File from a sharepoint site

Viewed 39

I am trying to read an excel file from SharePoint to python and I get the following error:

ValueError: Excel file format cannot be determined, you must specify an engine manually.

The python script goes something like that:

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/A_team/"
username = 'myemail@mail.com'
password = 'password'
relative_url = "sites/A_team/Documents_python/Folder/Book.xlsx"


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")
else:
    print(ctx_auth.get_last_error())

response = File.open_binary(ctx, relative_url)




bytes_file_obj = io.BytesIO()
bytes_file_obj.write(response.content)
bytes_file_obj.seek(0) #set file object to start




pd.read_excel(bytes_file_obj)

Does anyone have an idea what should I do?

I tried to add an engine, but then I get the following error:

BadZipFile: File is not a zip file

0 Answers
Related