├── README.md ├── sharepoint_read_file_python.py └── sharepoint_upload_file_python.py /README.md: -------------------------------------------------------------------------------- 1 | Accessing and Uploading Files From or To SharePoint Using Python 2 | -------------------------------------------------------------------------------- /sharepoint_read_file_python.py: -------------------------------------------------------------------------------- 1 | #pip install Office365-REST-Python-Client 2 | #pip install git+https://github.com/vgrem/Office365-REST-Python-Client.git 3 | 4 | # courtesy: https://stackoverflow.com/questions/59979467/accessing-microsoft-sharepoint-files-and-data-using-python 5 | 6 | #Importing required libraries 7 | 8 | from office365.runtime.auth.authentication_context import AuthenticationContext 9 | from office365.sharepoint.client_context import ClientContext 10 | from office365.sharepoint.files.file import File 11 | 12 | #Constrtucting SharePoint URL and credentials 13 | 14 | sharepoint_base_url = 'https://mycompany.sharepoint.com/teams/sharepointname/' 15 | sharepoint_user = 'user' 16 | sharepoint_password = 'pwd' 17 | folder_in_sharepoint = '/teams/sharepointname/Shared%20Documents/YourFolderName/' 18 | 19 | #Constructing Details For Authenticating SharePoint 20 | 21 | auth = AuthenticationContext(sharepoint_base_url) 22 | 23 | auth.acquire_token_for_user(sharepoint_user, sharepoint_password) 24 | ctx = ClientContext(sharepoint_base_url, auth) 25 | web = ctx.web 26 | ctx.load(web) 27 | ctx.execute_query() 28 | print('Connected to SharePoint: ',web.properties['Title']) 29 | 30 | 31 | #Constructing Function for getting file details in SharePoint Folder 32 | 33 | def folder_details(ctx, folder_in_sharepoint): 34 | folder = ctx.web.get_folder_by_server_relative_url(folder_in_sharepoint) 35 | fold_names = [] 36 | sub_folders = folder.files 37 | ctx.load(sub_folders) 38 | ctx.execute_query() 39 | for s_folder in sub_folders: 40 | fold_names.append(s_folder.properties["Name"]) 41 | return fold_names 42 | 43 | #Getting folder details 44 | 45 | file_list = folder_details(ctx, folder_in_sharepoint) 46 | 47 | #Printing list of files from sharepoint folder 48 | print(filelist_shrpt) 49 | 50 | #Reading File from SharePoint Folder 51 | sharepoint_file = '/teams/SustainabilityDataAccelerator/Shared%20Documents/General/Agro/2018_indirects_sustainable_sourcing_template.xlsx' 52 | file_response = File.open_binary(ctx, sharepoint_file) 53 | 54 | #Saving file to local 55 | with open("sharepointfile.csv", 'wb') as output_file: 56 | output_file.write(file_response.content) 57 | 58 | -------------------------------------------------------------------------------- /sharepoint_upload_file_python.py: -------------------------------------------------------------------------------- 1 | #pip install Office365-REST-Python-Client 2 | #pip install git+https://github.com/vgrem/Office365-REST-Python-Client.git 3 | 4 | # courtesy: https://stackoverflow.com/questions/59979467/accessing-microsoft-sharepoint-files-and-data-using-python 5 | 6 | #Importing required libraries 7 | 8 | from office365.runtime.auth.authentication_context import AuthenticationContext 9 | from office365.sharepoint.client_context import ClientContext 10 | from office365.sharepoint.files.file import File 11 | 12 | #Constrtucting SharePoint URL and credentials 13 | 14 | sharepoint_base_url = 'https://mycompany.sharepoint.com/teams/sharepointname/' 15 | sharepoint_user = 'user' 16 | sharepoint_password = 'pwd' 17 | folder_in_sharepoint = '/teams/sharepointname/Shared%20Documents/YourFolderName/' 18 | 19 | #Constructing Details For Authenticating SharePoint 20 | 21 | auth = AuthenticationContext(sharepoint_base_url) 22 | 23 | auth.acquire_token_for_user(sharepoint_user, sharepoint_password) 24 | ctx = ClientContext(sharepoint_base_url, auth) 25 | web = ctx.web 26 | ctx.load(web) 27 | ctx.execute_query() 28 | print('Connected to SharePoint: ',web.properties['Title']) 29 | 30 | #Read filename 31 | fileName = 'C:\\path\\file.xlsx' 32 | 33 | with open(fileName, 'rb') as content_file: 34 | file_content = content_file.read() 35 | 36 | name = os.path.basename(fileName) 37 | 38 | list_title = "Documents" 39 | 40 | target_list = ctx.web.lists.get_by_title(list_title) 41 | info = FileCreationInformation() 42 | 43 | libraryRoot = ctx.web.get_folder_by_server_relative_url(folder_url_shrpt) 44 | 45 | target_file = libraryRoot.upload_file(name, file_content).execute_query() 46 | print("File has been uploaded to url: {0}".format(target_file.serverRelativeUrl)) 47 | --------------------------------------------------------------------------------