Python - Download files from SharePoint site

后端 未结 1 1108
夕颜
夕颜 2020-12-06 02:37

I have a requirement of downloading and uploading the files to Sharepoint sites. This has to be done using python. My site will be as https://ourOrganizationName.sharepoint.

相关标签:
1条回答
  • 2020-12-06 03:36

    Have you tried Office365-REST-Python-Client library, it supports SharePoint Online authentication and allows to download/upload a file as demonstrated below:

    Download a file

    from office365.runtime.auth.authentication_context import AuthenticationContext
    from office365.sharepoint.client_context import ClientContext
    from office365.sharepoint.files.file import File
    
    ctx_auth = AuthenticationContext(url)
    ctx_auth.acquire_token_for_user(username, password)   
    ctx = ClientContext(url, ctx_auth)
    response = File.open_binary(ctx, "/Shared Documents/User Guide.docx")
    with open("./User Guide.docx", "wb") as local_file:
        local_file.write(response.content)
    

    Upload a file

    ctx_auth = AuthenticationContext(url)
    ctx_auth.acquire_token_for_user(username, password)   
    ctx = ClientContext(url, ctx_auth)
    
    path = "./User Guide.docx" #local path
    with open(path, 'rb') as content_file:
       file_content = content_file.read()
    target_url = "/Shared Documents/{0}".format(os.path.basename(path))  # target url of a file 
    File.save_binary(ctx, target_url, file_content) # upload a file
    

    Usage

    Install the latest version (from GitHub):

    pip install git+https://github.com/vgrem/Office365-REST-Python-Client.git
    

    Refer file_operations.py for a more details

    0 讨论(0)
提交回复
热议问题