微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决从 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 举报,一经查实,本站将立刻删除。