1

Exactly as the title says, I have this code

from shareplum import Site
from shareplum import Office365
from shareplum.site import Version

authcookie = Office365('https://mysite.sharepoint.com/', username='username', password='password').GetCookies()
site = Site('https://mysite.sharepoint.com/sites/mysite/', version=Version.v2016, authcookie=authcookie)
folder = site.Folder('Shared Documents/Beta Testing')
file = folder.get_file('practice.xlsx')
with open("practice.xlsx", "wb") as fh:
    fh.write(file)
print('---')
folder.upload_file('xlsx', 'practice.xlsx')

Currently it downloads the file just fine which is fantastic, however I do not know how to reverse what I did with opening and downloading the file. Basically I need to be able to upload the file with the exact same name as the one I downloaded in the exact same format (in this case xlsx) as to overwrite the one in the sharepoint with the updated document.

neeko_vo
  • 11
  • 1
  • 3

2 Answers2

2

Your post indicates that you want to modify the file so you will need some file handling for the downloaded file once it is saved after modification. Once the file modification has been done you need to open the file in 'rb' and then read that to a variable which will be the content when calling folder_obj.upload_file(content, name).

#this is your step to modify the file.
with open("practice.xlsx", "wb") as fh:
    #file modification stuff... pyxlsx?
    fh.write(file)

#open the file and read it into a variable as binary
with open("practice.xlsx", "rb") as file_obj:
    file_as_string = file_obj.read()

#upload the file including the file name and the variable (file_as_string)
folder.upload_file(file_as_string, 'practice.xlsx')

This has been working for me. If you want to change the name of the file to include a version, delete the old file by calling folder.delete_file("practice.xlsx").

0

Can you try the below and see if it works?

    with open("practice.xlsx", "wb") as fh:
       file_content =  fh.write(file)
    folder.upload_file(file_content,'practice.xlsx')
Rajalakshmi
  • 541
  • 5
  • 21