I am looking for Python approach to accessing data in SPO

Steinbacher Baich 20 Reputation points
2025-08-08T09:59:17.1466667+00:00

I am looking for Python approach to accessing data in SPO.

Thanks in advance.

My currently Python:

import pysharepoint as ps 
sharepoint_base_url = "https://<abc>[.sharepoint.com/"
username] = "username"
password = "password" 
site = ps.SPInterface(sharepoint_base_url, username, password) 
source_path = "Shared Documents/Shared/<Location>"
sink_path = "/full_sink_path/"
filename = "filename.ext"
sharepoint_site = "https://<abc>[.sharepoint.com/sites/]<site_name>" site.download_file_sharepoint(source_path, sink_path, filename, sharepoint_site) site.upload_file_sharepoint(source_path, sink_path, filename, sharepoint_site)

Microsoft 365 and Office | SharePoint | Development
0 comments No comments
{count} votes

Accepted answer
  1. Steven-N 5,575 Reputation points Microsoft External Staff Moderator
    2025-08-08T11:32:58.7266667+00:00

    Hi Steinbacher Baich

    Thanks for reaching out to Microsoft Q&A forum support  

    Based on your description, I understand that you are looking for the way to using Python script to accessing data in SharePoint Oline. I have conducted some research and found the information that may help you. 

    This starter Python code allows you to connect to SharePoint and access lists of files, folders, and individual file contents. You can customize it further to fit your needs. 

    from office365.runtime.auth.authentication_context import AuthenticationContext
    from office365.sharepoint.client_context import ClientContext
    from office365.sharepoint.files.file import File 
    ####inputs########
    # This will be the URL that points to your sharepoint site. 
    # Make sure you change only the parts of the link that start with "Your"
    url_shrpt = 'https://YourOrganisation.sharepoint.com/sites/YourSharepointSiteName'
    username_shrpt = 'YourUsername'
    password_shrpt = 'YourPassword'
    folder_url_shrpt = '/sites/YourSharepointSiteName/Shared%20Documents/YourSharepointFolderName/'
    #######################
    ###Authentication###For authenticating into your sharepoint site###
    ctx_auth = AuthenticationContext(url_shrpt)
    if ctx_auth.acquire_token_for_user(username_shrpt, password_shrpt):
      ctx = ClientContext(url_shrpt, ctx_auth)
      web = ctx.web
      ctx.load(web)
      ctx.execute_query()
      print('Authenticated into sharepoint as: ',web.properties['Title'])
    else:
      print(ctx_auth.get_last_error())
    ############################  
    ####Function for extracting the file names of a folder in sharepoint###
    ###If you want to extract the folder names instead of file names, you have to change "sub_folders = folder.files" to "sub_folders = folder.folders" in the below function
    global print_folder_contents
    def print_folder_contents(ctx, folder_url):
        try:
           
            folder = ctx.web.get_folder_by_server_relative_url(folder_url)
            fold_names = []
            sub_folders = folder.files #Replace files with folders for getting list of folders
            ctx.load(sub_folders)
            ctx.execute_query()
         
            for s_folder in sub_folders:
                
                fold_names.append(s_folder.properties["Name"])
            return fold_names
        except Exception as e:
            print('Problem printing out library contents: ', e)
    ######################################################
    # Call the function by giving your folder URL as input  
    filelist_shrpt=print_folder_contents(ctx,folder_url_shrpt) 
    #Print the list of files present in the folder
    print(filelist_shrpt)
    

    Note: this method works for public SharePoint sites. Also, you’ll need to adjust the SharePoint file URL, as direct browser-copied links typically won’t work in Python without modification. 

    Next one, in the situation that you want to retrieve and print the list of files present in a particular folder in SharePoint, below is the code to access the file contents of a particular file and save it to local disk having known the file name and path in SharePoint.

    #Specify the URL of the sharepoint file. Remember to change only the the parts of the link that start with "Your"
    file_url_shrpt = '/sites/YourSharepointSiteName/Shared%20Documents/YourSharepointFolderName/YourSharepointFileName'
    #Load the sharepoint file content to "response" variable
    response = File.open_binary(ctx, file_url_shrpt)
    #Save the file to your offline path
    with open("Your_Offline_File_Path", 'wb') as output_file:  
        output_file.write(response.content)
    

    For more information, if you have only the URL link to your sharepoint documents, you will have to extract the following parameters from the URL namely: "YourOrganisation", "YourSharepointSiteName", "YourSharepointFolderName" and "YourSharepointFileName". All the above parameters would be embedded in your sharepoint link itself.

    Hope my answer will help you, for further support, kindly let me know in the comment section


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".     

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.