简体   繁体   中英

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

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?

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")
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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM