Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert DataFrame to xlsx file without saving it?

I am loading in an Excel file as a DataFrame, do some transformations, then I want to save the Excel file to some server (not locally).

Currently, I am able to do a workaround to achieve this:

import pandas as pd
import requests

df = pd.read_excel("file.xlsx")
df = do_some_transformation(df)

# Store DataFrame locally
df.to_excel("outputfile.xlsx")

# re-read locally stored file und upload it
with open("outputfile.xlsx", "rb") as fin:
        requests.put("url/outputfile.xlsx",
                     data=fin.read(),
                     auth=auth,
                     headers={'content-type': 'application/vnd.ms-excel'})

i.e. I am saving the transformed DataFrame locally, to then upload the local copy to the server. Is it possible to convert the df directly into an excel file, without having to store and re-loading it locally? How would I have to modify the requests.put statement?

with the hint of @Aryerez, I tried

df = pd.read_excel("file.xlsx")
df = do_some_transformation(df)

writer = pd.ExcelWriter("file.xlsx", engine='xlsxwriter')
df.to_excel(writer, sheet_name='Sheet1')

requests.put("url/outputfile.xlsx",
                     data=writer,
                     auth=auth,
                     headers={'content-type': 'application/vnd.ms-excel'}),
                     

which results in a TypeError: '_XlsxWriter' object is not iterable.

How can I convert the pandas DataFrame to an Excel-File and pass this to request.put?

like image 523
bk_ Avatar asked Oct 14 '25 15:10

bk_


2 Answers

  • you just need to return the bytes value not the writer itself.

  • and you don't need the actual file to write to it you could just use io.BytesIO() instead of saving it to the disk.

      output = io.BytesIO()
    
      df = pd.read_excel("file.xlsx")
      df = do_some_transformation(df)
    
      writer = pd.ExcelWriter(output, engine='xlsxwriter')
      df.to_excel(writer, sheet_name='Sheet1')
    
      writer.save()
      xlsx_data = output.getvalue()
    
      requests.put("url/outputfile.xlsx",
               data=xlsx_data,
               auth=auth,
               headers={'content-type': 'application/vnd.ms-excel'}),
    
like image 70
Amr Hashem Avatar answered Oct 17 '25 04:10

Amr Hashem


You can do:

writer = pd.ExcelWriter(fileName, engine='xlsxwriter')
df.to_excel(writer, sheetName)

Which will create an Excel object in writer with data from df. It won't save it until you do:

writer.save()
like image 20
Aryerez Avatar answered Oct 17 '25 04:10

Aryerez



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!