Upload files to Sharepoint in a specific folder using Python

Asked

Viewed 204 times

0

I am uploading file from my machine to Sharepoint using python with lib Office-REST-Python-Client, but I can only upload the file to the Sharepoint root and not to a certain folder. Code I’m using for upload:

import os

from settings import settings

from office365.runtime.auth.user_credential import UserCredential
from office365.sharepoint.client_context import ClientContext

ctx = ClientContext.connect_with_credentials(settings['url'],
                                             UserCredential(settings['user_credentials']['username'],
                                                            settings['user_credentials']['password']))

path = "../../../tests/data/SharePoint User Guide.docx"
with open(path, 'rb') as content_file:
    file_content = content_file.read()

list_title = "Documents"
target_folder = ctx.web.lists.get_by_title(list_title).rootFolder
name = os.path.basename(path)
target_file = target_folder.upload_file(name, file_content)
ctx.execute_query()
print("File url: {0}".format(target_file.serverRelativeUrl))

With this code above, I can upload and the files appear at the root of Sharepoint, which in this case I call 'Documents'. Then I made the following modification to go up in a certain folder, in case in a call 'Area':

list_title = "Documents/Area"

However, in this case I am no longer successful, it returns that this Area path does not exist. How can I upload to a specific folder?

Directory in my Sharepoint:

Diretório no sharepoint

  • 1

    See help: https://stackoverflow.com/questions/61603107/upload-a-file-to-a-sharepoint-folder-using-python

  • 1

    @Paulomarques I came to see this example, but he uses another library, he uses shareplum

  • What is the value of target_folder and of name?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.