Skip to content

Instantly share code, notes, and snippets.

@thierryturpin
Created October 13, 2023 08:48
Show Gist options
  • Save thierryturpin/f521fca2a43fba755ff6d1dc888399a3 to your computer and use it in GitHub Desktop.
Save thierryturpin/f521fca2a43fba755ff6d1dc888399a3 to your computer and use it in GitHub Desktop.
Sharepoint download via Python
"""
Demonstrates how to download a file from SharePoint site
"""
import os
import tempfile
from office365.sharepoint.client_context import ClientContext
test_site_url = "https://tturpin.sharepoint.com/sites/Turpin"
client_id = os.environ.get("CLIENT_ID")
client_secret = os.environ.get("CLIENT_SECRET")
ctx = ClientContext(test_site_url).with_client_credentials(
client_id, client_secret
)
file_url = 'Gedeelde documenten/General/PWC_LOGO.png'
download_path = os.path.join(tempfile.mkdtemp(), os.path.basename(file_url))
with open(download_path, "wb") as local_file:
file = ctx.web.get_file_by_server_relative_url(file_url).download(local_file).execute_query()
print("[Ok] file has been downloaded into: {0}".format(download_path))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment