title: Upload excel file to SharePoint Online using Python
body: <p>I am trying to upload my excel spreadsheet to a document library on my SharePoint Online site.  The Sharepoint URL and the folder location on the SharePoint site are listed in the excel Spreadsheet.</p>
  <p>Here is the code that I have right now:</p>
  <pre><code>import numpy as np
  import pandas as pd
  import xlwings as xw
  from xlwings.constants import Direction
  import sys
  import requests
  from requests_ntlm import HttpNtlmAuth
  pd.options.mode.chained_assignment = None
  
  def Upload():
  
      wb = xw.Book.caller()
      ws = wb.sheets['Sheet1']
  
      #Read filename from excel
      fileName = sys.argv[1]
  
      #Enter SharePoint ONline site and target library
      SP_URL = ws.range('C7').value
      folder_URL = ws.range('C8').value
  
      #Set up the url for requesting file upload
      request_URL = SP_URL + '/_api/web/getfolderbyserverrelativeurl(\'' + 
      folder_URL + '\')/Files/asdd(url=\'' + fileName + '\',overwrite=true)'
  
      #read in the file that we are going to upload
      file = open(fileName, 'rb')
  
      headers = {'Content-Type': 'application/json; odata=verbose', 'accept': 
      'application/json;odata=verbose'}
      r = requests.post(SP_URL + 
      &quot;/_api/contextinfo&quot;,auth=HttpNtlmAuth('Domain\\username','password'), 
      headers=headers)
      formDigestValue = r.json()['d']['GetContextWebInformation'] 
      ['FormDigestValue']
      headers = {'Content-Type': 'application/json; odata=verbose', 'accept': 
      'application/json;odata=verbose', 'x-requestdigest' : formDigestValue}
      uploadResult = 
      requests.post(request_URL,auth=HttpNtlmAuth('Domain\\username','password'), 
      headers=headers, data=file.read())
  </code></pre>
  <p>I am receiving the following error:</p>
  <blockquote>
  <p>formDigestValue = r.json()['d']['GetContextWebInformation']['FormDigestValue']</p>
  <p>KeyError: 'd'</p>
  </blockquote>