0
votes

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()
2
What error are you running into when you run this code? Is it not working as intended?Hofbr

2 Answers

0
votes

Here's a basic template code I use for downloading from my work's share-point. I think the code you posted would work.

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)