1

I am trying to read the excel file that I stored in Sharepoint. But I wasn't able to. I have referred to most of the existing articles but that didn't solve my issue.

Code I referenced From: How to read SharePoint Online (Office365) Excel files in Python with Work or School Account?

Read xlsx stored on sharepoint location with openpyxl in python?

But no luck.

import io
import json
import pandas as pd
from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.runtime.auth.user_credential import UserCredential
from office365.runtime.http.request_options import RequestOptions
from office365.sharepoint.client_context import ClientContext
from office365.sharepoint.files.file import File
from io import BytesIO

username = '##########################'
password = '##########################'
site_url = "#######################################################"

ctx = ClientContext(site_url).with_credentials(UserCredential(username, password))
request = RequestOptions("{0}/_api/web/".format(site_url))
response = ctx.execute_request_direct(request)
json_data = json.loads(response.content)

Error I am getting : enter image description here

freak7
  • 99
  • 12
  • 1
    I tried to do this a while ago and just ended up "Syncing" the sharepoint folder with my machine locally, its much easier to read and write to this way. – Boskosnitch May 07 '21 at 04:51

1 Answers1

0

I hope this works for you. Once you download the file you can read it as per its respective file format.

from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.sharepoint.client_context import ClientContext
from office365.sharepoint.files.file import File

ctx_auth = AuthenticationContext(url)
ctx_auth.acquire_token_for_user(username, password)   
ctx = ClientContext(url, ctx_auth)
response = File.open_binary(ctx, "/Shared Documents/User Guide.docx")
with open("./User Guide.docx", "wb") as local_file:
    local_file.write(response.content)