Skip to content

Instantly share code, notes, and snippets.

@nuriozbey
Created September 6, 2022 13:13
Show Gist options
  • Select an option

  • Save nuriozbey/7738ed6ea63a3048536dc9746d63e8c8 to your computer and use it in GitHub Desktop.

Select an option

Save nuriozbey/7738ed6ea63a3048536dc9746d63e8c8 to your computer and use it in GitHub Desktop.
import pandas as pd
from io import BytesIO
"""
# Usage:
df = pd.DataFrame({'num_l': [2, 4, 8, 0],
'num_w': [2, 0, 0, 0],
'num_s': [10, 2, 1, 8]},
index=['falcon9', 'falcon1', 'python', 'anaconda'])
dir = "/teams/DataShare/Shared Documents/General/DummyFolder/"
file_name = "dummy_dataframe.xlsx"
cloud_path = upload_dataframe(ctx, dir, file_name, df)
"""
def upload_dataframe(ctx, dir, file_name, dataframe):
#name = "sample2.xlsx"
bio = BytesIO()
# Pass engine explicitly, as there is no file path to infer from.
writer = pd.ExcelWriter(bio, engine='openpyxl') # you should install (pip install openpyxl)
dataframe.to_excel(writer)
writer.save()
bio.seek(0)
file = ctx.web.get_folder_by_server_relative_url(dir).upload_file(file_name, bio).execute_query()
print("Dataframe has been uploaded to url: {0}".format(file.serverRelativeUrl))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment