0

I have excel saved in sharepoint and I am trying to read it with openpyxl. I have URL path to excel which is below and I have different options I see on internet and none of them worked.

How can I read excel in below path using openpyxl?

https://company.sharepoint.com/:x:/r/sites/global/tasks/ADT/_layouts/15/Doc.aspx?sourcedoc=%7BBFD411FA-303E-4950-961C-6702D81E112B%7D&file=SEPT%20ALL%20ROB.xlsx&action=default&mobileredirect=true

Below is the code I have and I getting HTTP forbidden error

from openpyxl import load_workbook
from io import BytesIO
import urllib


URL = https://company.sharepoint.com/:x:/r/sites/global/tasks/ADT/_layouts/15/Doc.aspx? 
sourcedoc=%7BBFD411FA-303E-4950-961C- 
6702D81E112B%7D&file=SEPT%20ALL%20ROB.xlsx&action=default&mobileredirect=true

def load_workbook_from_url(URL):
  file = urllib.request.urlopen(URL).read()
  return load_workbook(filename=BytesIO(file))


wb = load_workbook_from_url(URL)

sheetlist = wb.get_sheet_names()
print(sheetlist)

Error I get is : raise HTTPError(req.full_url, code, msg, hdrs, fp) urllib.error.HTTPError: HTTP Error 403: Forbidden

4
  • Try defining the URL as a raw string with URL = r"https://path/to/your/file.xlsx". Other than that, i do not know if you can access files on a sharepoint publicly like that, i would need an example, accessible file to investigate further. Commented Dec 6, 2021 at 17:18
  • I tried with raw string and I did not work. If I copy and paste above URL, it will open excel in browser. It is internal website not external. Commented Dec 6, 2021 at 17:33
  • Presumably, you need to login. Commented Dec 6, 2021 at 18:19
  • Maybe this thread or this repository helps Commented Dec 7, 2021 at 8:37

1 Answer 1

0

This worked for me: import shareplum and define the file to be downloaded

from shareplum import Site
from shareplum import Office365
from shareplum.site import Version

def loadShareFile(filename):
    authcookie = Office365('https://{your sharepoint site}.sharepoint.com', username='{usermail}@mail.com', password='{your password}').GetCookies()
    site = Site('https://{your sharepoint site}.sharepoint.com/sites/{your path}', version=Version.v365, authcookie=authcookie)
    
    folder = site.Folder('Documentos Compartidos/{folder}')  #or Shared Documents
    return folder.get_file(filename)  # this return bytes

then get the data with openpyxl:

from openpyxl import load_workbook
from io import BytesIO
import loadShareFile  # import the above method 

wb = load_workbook(BytesIO(loadShareFile(filename='{my file name}.xlsx')))

that's it! now you have you wb object to work with. If someboy nows how to do the other way arround will help me, I'm struggling with that part.

Sign up to request clarification or add additional context in comments.

Comments

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.