Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

read csv from SharePoint with Pandas (Python authentication code)

I have a csv in the documents section of a SharePoint site. I would like to import it in Pandas. Of course is I use just the normal code below I get HTTP error 403 Forbidden.

import pandas
df = pandas.read_csv('link from sharepoint')

How do I get SharePoint authentication to work using Python so Pandas can read the csv file.

I have already searched and tried code in several internet posts, but either the code is too generic that I do not know what it means e.g.

username = 'YourDomain\\account'

or

user = r'SERVER\user'

or it just didn't.

Is there a simple way to get authentication to work and import the file in Pandas?

like image 570
MCG Code Avatar asked Oct 17 '22 07:10

MCG Code


1 Answers

Relatively easy solution with sharepy:

import io
import sharepy
import pandas as pd

URL = 'https://yoursharepointsite.sharepoint.com'
FILE_URL = '/sites/...path to your file.../yourfilename.csv'
SHAREPOINT_USER = 'yourusername'
SHAREPOINT_PASSWORD = 'yourpassword'

s = sharepy.connect(URL, username=SHAREPOINT_USER, password=SHAREPOINT_PASSWORD).
r = s.get(URL+FILE_URL)
f = io.BytesIO(r.content)
df = pd.read_csv(f)
like image 198
Andrii Avatar answered Oct 21 '22 07:10

Andrii