Excel file corrupted after upload to Sharepoint with Python

I am trying to upload an Excel file from a local folder to Sharepoint using the Requests module in Python. The file will appear in my Shared Documents folder, where I want it to be, but when I try to open the file, I get a message that says "The book you selected cannot be opened. Unsupported file format, or it may be damaged. You Want to try opening this file in Excel? "

I researched this problem, and many forums that I found said that the common problem is that somewhere there is a space before the ^ character, but for the moment I’m just trying to load an absolutely empty file with absolutely nothing in it .

Here is my code:

files = {'file': ('Test.xlsx', open('Test.xls', 'rb'), 'application/vnd.ms-excel', {'Expires': '0'})}
myFile = requests.put('http://linkToMySharepoint/Test.xlsx', files=files, auth=auth)

What am I doing wrong? Any help or understanding would be greatly appreciated!

+4
source share
2 answers

Problem: The problem is that Sharepoint is expecting binary data from your request. But you are sending some batches of data. When you make a request, the python request library automatically adds to the header

Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryNTIFeyYj307EY4HV 

As an example, try sending a .txt file with the text "fcvsdcvsd". When you upload this file to your Sharepoint site, you will receive another file. This is what I got in the Sharepoint file repository when I downloaded this file:

------WebKitFormBoundaryCGYE31vAG1tfvbzV
Content-Disposition: form-data; name="fileUpload1"; filename="testtest.txt"
Content-Type: text/plain

fcvsdcvsd
------WebKitFormBoundaryCGYE31vAG1tfvbzV-- 

In your case, these lines are also added to your file. When Excel tries to open this file, it finds these lines and stops reading because the file is damaged.

: , .

   headers = {"accept": "application/json;odata=verbose",
               "X-RequestDigest": token,
               "content-type": "application/x-www-urlencoded; charset=UTF-8"}

    with open(local_path, "rb") as read_file:
        content = read_file.read() 

    r = requests.post(url + "getFolderByServerRelativeUrl('" + location + "')/files/add(overwrite=true,url='" + filename + "')",
                      data=content, auth=auth, headers=headers)

, ( "" ) ( " " ), "application/x-www-urlencoded; charset = UTF-8"

txt ,

fcvsdcvsd

URL- - URL-, Sharepoint (?). - , "X-RequestDigest": , Sharepoint.

+1

, , . , , MIME . . PUT. :

import requests
import os
import sys
from requests_ntlm import HttpNtlmAuth

filename = 'test.xlsx'


session = requests.Session()
session.auth = HttpNtlmAuth('YOURDOMAIN\\youraccount','yourpass', session)

file = open(filename, 'rb')
bytes = bytearray(file.read())
resp = requests.put('http://sharepoint.company.com/exampleurl/Archive%20%20old%20stuff/' + filename, data=bytes, auth=session.auth)

print(resp.status_code)
0

Source: https://habr.com/ru/post/1544011/


All Articles