How to download a XLS from Teams/SharePoint using Python

sfmapper 1 Reputation point
2021-01-14T19:36:19.1+00:00

Hello,
I am attempting to download a xls file from our Teams shared files. I have gotten it to work most of the way but am having two issues.
The first, the resulting downloaded xls is always corrupted and is only 1kb in size.
The second, I cant figure out how to dictate the directory where the xls is copied to. It currently copies it to the same location as the .py file.
Any help would be appreciated.

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

site_url = "https://[our co].sharepoint.com/teams/[our teams channel]"
library = "/Shared Documents/"
filename = "test.xlsx"
pathToFile = library + filename

app_principal = {
'client_id': '[our client id]',
'client_secret': '[our client secret]',
}

ctx_auth = AuthenticationContext(site_url)
ctx_auth.acquire_token_for_app(client_id=app_principal['client_id'], client_secret=app_principal['client_secret'])
ctx = ClientContext(site_url, ctx_auth)
web = ctx.web
ctx.load(web)
ctx.execute_query()

fileToRetrieve = File.open_binary(ctx, pathToFile)
output = open(filename, 'wb').write(fileToRetrieve.content)

Microsoft Teams Development
Microsoft Teams Development
Microsoft Teams: A Microsoft customizable chat-based workspace.Development: The process of researching, productizing, and refining new or existing technologies.
2,993 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Jagadeesh-MSFT 331 Reputation points
    2021-01-19T12:28:35.963+00:00

    Re: How to download a XLS from Teams/SharePoint using Python

    All the files in Teams are saved in Share point. All those files can be downloaded using SharePoint API. The second option can be using the Graph API.

    0 comments No comments