How to upload a pandas dataframe directly to SharePoint

Aidan Goldie 0 Reputation points
2024-04-18T17:22:31.5166667+00:00

I am trying to upload a pandas dataframe to SharePoint directly from a python script (don't want to save anything locally)

  • I'm able to create SharePoint directories/folders from the script so I'm sure that my authentication process is working
  • The relative URL is correct as I used this to create directories/folders
  • df1 is the dataframe which has data in it for upload
  • My code runs with no errors however no file is uploaded to SharePoint

Any help with this would be greatly appreciated

My code is as follows ;

site_url = 'https://{Name}.sharepoint.com/sites/{SiteName}'
relative_url = f'Documents/Shared Documents/test'

context = ClientContext(site_url).with_credentials(ClientCredential(sharepoint_clientID, sharepoint_clientSecret))


try:
    target_folder = context.web.get_folder_by_server_relative_url(relative_url)

    buffer = BytesIO() 
    df1.to_csv(buffer, index=False) 
    buffer.seek(0) 
    file_content = buffer.read() 
    file_name = 'test.csv'
    target_folder.upload_file(file_name, file_content).execute_query()

except Exception as e:
        print('Problem uploading to Sharepoint: ', e)

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
9,661 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Shawn Collins 500 Reputation points
    2024-04-18T21:06:46.13+00:00

    Authentication and Context Setup: As stated, it seems you are successfully creating directories, so authentication likely isn't the issue. However, ensure that the client credentials have the necessary permissions to write/upload files to the directory.

    DataFrame to Buffer: This part of your code looks correct. You're writing the DataFrame to a BytesIO stream as a CSV, which is a good approach to avoid local file storage.

    File Upload: This is the critical part. The upload_file method seems to be used correctly. One potential issue could be the way the file content is read or the state of the buffer.

    Error Handling: It's good that you have error handling, but sometimes the message might not be descriptive enough to understand the underlying SharePoint or network issue.

    Let's refine your approach with the following steps:

    Ensure Buffer Readability: After writing to the buffer and before reading, it's good to ensure the buffer's pointer is reset to the start.

    Execution of SharePoint Client Query: You are using execute_query() after uploading the file. This should generally trigger the actual HTTP request to SharePoint. Make sure that this method is correctly executing and not silently failing.

    Verbose Error Output: Modify the exception handling to give more information if possible.

    Permissions Check: Double-check the permissions assigned to the credentials used for file uploads specifically.

    Here's a slightly modified version of your script with some additional debugging information:

    from io import BytesIO from Office365.runtime.auth.client_credential import ClientCredential from Office365.sharepoint.client_context import ClientContext
    
    SharePoint site details
    
    site_url = 'https://{Name}.sharepoint.com/sites/{SiteName}' relative_url = 'Documents/Shared Documents/test' client_id = 'your_client_id'  # Replace with your SharePoint client ID client_secret = 'your_client_secret'  # Replace with your SharePoint client secret
    
    Authenticate and create context
    
    context = ClientContext(site_url).with_credentials(ClientCredential(client_id, client_secret))
    
    try: # Get the target folder target_folder = context.web.get_folder_by_server_relative_url(relative_url) print("Folder found:", target_folder.serverRelativeUrl)
    
    # Prepare the file content
    buffer = BytesIO()
    df1.to_csv(buffer, index=False)
    buffer.seek(0)
    file_content = buffer.getvalue()  # Use getvalue() to read the buffer content
    
    # Upload the file
    file_name = 'test.csv'
    upload_file = target_folder.upload_file(file_name, file_content)
    context.execute_query()  # Make sure to call execute_query to perform the operation
    print("File uploaded successfully")
    except Exception as e: print('Problem uploading to SharePoint:', e)
    

    Key Changes:

    Added print statements to confirm folder retrieval and successful upload.

    Changed buffer.read() to buffer.getvalue() to ensure correct buffer handling.

    Added comments to clarify each block's purpose.

    Make sure to replace 'your_client_id' and 'your_client_secret' with your actual SharePoint client ID and secret. This updated code should provide more insight into where the process might be failing, if it still does.