1

In the documentation for pd.ExcelWriter we see the following code snippet:

You can store Excel file in RAM:

import io
df = pd.DataFrame([["ABC", "XYZ"]], columns=["Foo", "Bar"])
buffer = io.BytesIO()
with pd.ExcelWriter(buffer) as writer:
     df.to_excel(writer)

My question is that how can we access the excel back. I wanted to have the b64 coded version of the same excel without saving the file in the system that is why I am thinking of saving it in my RAM. Can someone please help on this?

Thanks for your time

Solution: Was able to access the file using buffer.getvalue().

1 Answers1

1

In the snippet you provided, the Excel file has been written to the buffer the same way as if it would have been stored on disk.

Therefore you can read it back in a similar way as if you were reading from file:

pd.read_excel(buffer.getvalue())

More on how BytesIO behave:

sophros
  • 14,672
  • 11
  • 46
  • 75
  • Thanks for your help. I tried this but it is not working. Getting a Value Error. ValueError: File is not a recognized excel file. Any other way in which we can access it ? – Anurag Saraf Jul 21 '21 at 06:16
  • 1
    Hi, was able to access the data using buffer.getvalue() method. Thanks for your help sophros – Anurag Saraf Jul 21 '21 at 06:22