Skip to content

how can I update and save the change to an excel file in sharepoint after read #618

Open
@yyz20002008

Description

@yyz20002008

After I use code below to read the excel file in sharepoint, how can I write updates to the same excel file

SP_SITE_URL ='https://xxx.sharepoint.com/sites/xxx'
relative_url = "/sites/xxx/Shared Documents/FOLDER" 


# 1. Create a ClientContext object and use the user’s credentials for authentication 
ctx = ClientContext(SP_SITE_URL).with_user_credentials(USERNAME, PASSWORD) 
ClientFolder = ctx.web.get_folder_by_server_relative_path(relative_url)
ctx.load(ClientFolder)
ctx.execute_query()

#if you want to get the files in the folder        
files = ClientFolder.files
print(files)
ctx.load(files)
ctx.execute_query()

newest_file_url = ''
for myfile in files:
   
    if myfile.properties["Name"] == 'xxx.xlsx':
        newest_file_url = myfile.properties["ServerRelativeUrl"]
# Get Excel File by newest_file_url identified above
response= File.open_binary(ctx, newest_file_url)
    # save data to BytesIO stream
bytes_file_obj = io.BytesIO()
bytes_file_obj.write(response.content)
bytes_file_obj.seek(0)
```

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions