2

I am trying to download excel files from my work SharePoint site to a local folder using python. I have written a code to successfully authenticate into the sharepoint site.But need help in downloading the Excel files from the sharepoint document library. I am new to Python and would really appreciate your help :) Below is my code :

import urllib.request
import requests
from requests_ntlm import HttpNtlmAuth


def sharepointlogin():
    site = "https://abc.sharepoint.com/site"
    username = "*******"
    password = "*******"

    response = requests.get(site, auth=HttpNtlmAuth(username, password))
    print(response.status_code)

def filedownload():

    print('Downloading file')

    url = 'https://abc.sharepoint.com'
    urllib.request.urlretrieve(url, 'C:\Downloads\filename.xlsx')

    print("File Downloaded")

    print("Download complete")


sharepointlogin()

filedownload()
Ashraf Khan
  • 29
  • 1
  • 1
  • 3

1 Answers1

1
import requests

from getpass import getpass
from requests_ntlm import HttpNtlmAuth

url = "https://share.something.com/path/file.xlsx"

session = requests.Session()
session.verify = False

username = input("Enter your username: ")
password = getpass("Enter your password: ")

session.auth = HttpNtlmAuth(username, password)
response = session.get(url)

with open("output.xlsx", wb) as f:
    f.write(response.content)
Hofbr
  • 868
  • 9
  • 31