Upload a Pandas DataFrame as Excel file directly to Google Drive using PyDrive Upload a Pandas DataFrame as Excel file directly to Google Drive using PyDrive pandas pandas

Upload a Pandas DataFrame as Excel file directly to Google Drive using PyDrive


It is possible to get pandas to write an Excel file directly to a string like:

Code:

wb = Workbook()writer = pd.ExcelWriter('', engine='openpyxl')writer.book = wbdf.to_excel(writer)file_as_string = save_virtual_workbook(wb)

Full Code:

Here is the above code combined with your example. Please note that this part is untested.

import pandas as pdfrom pydrive.auth import GoogleAuthfrom pydrive.drive import GoogleDrivegauth = GoogleAuth()gauth.LoadCredentialsFile(mycreds)drive = GoogleDrive(gauth)df = pd.DataFrame({'a': [1, 2], 'b': [2, 3]})from openpyxl.workbook import Workbookfrom openpyxl.writer.excel import save_virtual_workbookwb = Workbook()writer = pd.ExcelWriter('', engine='openpyxl')writer.book = wbdf.to_excel(writer)f = drive.CreateFile({'id': '0B_6_uVX9biFuX0FJWFkt'}) #test.xlsx filef.SetContentString(save_virtual_workbook(wb))f.Upload()