Home > database >  python - google colab: create excel file with different sheets from several data frames
python - google colab: create excel file with different sheets from several data frames

Time:09-14

I've built a dashboard for my team that presents them with several different tables on google colab, and I'm trying to create a download button that once clicked, the tables will be joined into the same file under different sheets. I tried using the following format:

with pd.ExcelWriter(file_name) as writer:
   df1.to_excel(writer, sheet_name = 'df1')
   df2.to_excel(writer, sheet_name = 'df2')
   df3.to_excel(writer, sheet_name = 'df3')

I couldn't find a way to save the file to my downloads folder. Tried using writer.save() inside the with statement, but it downloads it to the drive folder where the script is. Is there another way to do this?

My goal is to create a file, which has several sheets, and let the users download it to their computer.

edit: Is there a python script that works on Google colab that shows me my username?

CodePudding user response:

If you are a windows user and your download is on HD C:then:

change your_user to your username, and this will problably run just fine!

with pd.ExcelWriter('C:/Users/your_user/Downloads/filename.xlsx') as writer:
     df1.to_excel(writer, sheet_name="df1")
     df2.to_excel(writer, sheet_name="df2")
     df3.to_excel(writer, sheet_name="df3")

CodePudding user response:

from google.colab import data_table, file 
with pd.ExcelWriter('filename.xlsx') as writer:
     df1.to_excel(writer, sheet_name="df1")
     df2.to_excel(writer, sheet_name="df2")
     df3.to_excel(writer, sheet_name="df3")
     files.download('filename.xlsx')

does the trick. it also writes the file to Google drive, where the colab notebook is, so i just need to find a way to delete it from there.

  • Related