从 SharePoint 以编程方式使用 Python 代码下载时 Xlsx 文件已损坏:如何解决?

问题描述

我正在使用以下 Python 代码连接 SharePoint 并下载特定文件。 虽然文件正在下载到我设置的本地文件夹,但我无法打开下载的文件。 尝试打开文件时,我收到一条错误消息,指出文件已损坏或格式不受支持

知道如何解决吗?还是使用更好的解决方案?

**** 代码 ****

 import sys
 import os
 import xlrd
 import pandas
 from datetime import datetime
 import requests
 from office365.runtime.auth.authentication_context import AuthenticationContext
 from office365.sharepoint.client_context import ClientContext
 from office365.sharepoint.files.file import File


 sharePointUrl = 'https://groupdomainname.sharepoint.com'
 username='user@yopmail.com'
 password='passowrd'       
 response=''

 sharepointFilePath="/sites/Investments/Shared%20Documents/Forms/Folders%20For/"
 fileName='File Name.xlsx'

 localPathForDownload="C:\user\download\files\\"


 #1. Authentication : authenticating into your sharepoint site###
 ctx_auth = AuthenticationContext(sharePointUrl)
 if ctx_auth.acquire_token_for_user(username,password) : 
   ctx = ClientContext(sharePointUrl,ctx_auth)
   web = ctx.web
   ctx.load(web)
   ctx.execute_query()
   print('Authenticated into sharepoint as: ',web.properties['Title'])
 else:
   print(ctx_auth.get_last_error())


 #2. Downloading : Finding the file and Downlaoding it to a local folder###
 try : 
    response = File.open_binary(ctx,sharepointFilePath + Filename)
    print ("file " + str(fileName) + " was found")
    with open(localPathForDownload+fileName,"wb") as local_file:
          local_file.write(response.content)

 except Exception as e:
    print(str(sys.exc_info()[0])+" : "+str(e.args[0]))
    sys.exit()

enter image description here

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)