#First of all, you need to import all the necessary libraries to allow you access SharePoint.
#import all the libraries
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
import requests
import os
import glob
from pandas import DataFrame
#Then proceed to authenticate to your SharePoint account
#target url taken from sharepoint and credentials
url='https://sharepoint.io.com/site3516/COURM/Tools/'
username = 'johndoe@company.com'
password = 'johndoepass!'
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)
#save data to BytesIO stream
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
#read excel file and each sheet into pandas dataframe
df = pd.read_excel(bytes_file_obj, sheetname = None)
#Read path
path = os.getcwd()
xlsx_files = glob.glob(os.path.join(path, "*.xlsx"), recursive = True)
# Read each file in the folder into a DataFrame
for f in xlsx_files:
df = pd.read_excel(f)
print('Location:', f)
print('File Name:', f.split("\\")[-1])
print('Content:')
display(df)
print()