Home > OS >  How to Download a PDF File from Online SharePoint using python?
How to Download a PDF File from Online SharePoint using python?

Time:08-08

How to Download a PDF File from Online SharePoint using python?

I've tried this code: but whenever I try this it downloads a broken file

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

url = "SharePointFilePath"
username= "Username"
password= "Password"

ctx_auth = AuthenticationContext(url)
ctx_auth.acquire_token_for_user(username, password)   
ctx = ClientContext(url, ctx_auth)
response = File.open_binary(ctx, "SharepointURL")
with open("file.pdf","wb") as local_file:
    local_file.read(response.content)

CodePudding user response:

You need to write, not to read:

with open("file.pdf","wb") as local_file:
    local_file.write(response.content)

CodePudding user response:

Use another flag 'wb' instead of 'rb' and write method instead of read

with open('file.pdf', 'wb') as f:
    f.write(response.content)

Read about flags

  • Related