3

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()
1
  • What error are you running into when you run this code? Is it not working as intended? Commented Sep 23, 2020 at 5:07

1 Answer 1

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)
Sign up to request clarification or add additional context in comments.

5 Comments

I am getting NameError: name 'output' is not defined. Also response.content is giving me b'403 FORBIDDEN'
You have to put output.xlsx inside "", like "output.xlsx"
Didn't notice that typo. I've updated.
Would this work for SharePoint Online ?
there is no hidden excel it is a sharepoint list that I want to turn into a excel

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.