Solved

File creation and upload

  • 29 August 2023
  • 3 replies
  • 55 views

Badge +2

How do I store a dataframe as an XLS in CDF files? I have to perform this within CDF functions. 

 

icon

Best answer by Ivar Stangeby 31 August 2023, 12:42

View original

3 replies

Hello Eashwar!

 

You would have to deploy some Python code to do this - note I haven’t tested this, but it should serve as a starting point for you.  This requires the library `openpyxl` to be able to call `df.to_excel`.

import pandas as pd
import io

def get_data_frame():
df = pd.DataFrame({
'A': [1, 2, 3],
'B': [4, 5, 6],
'C': ['p', 'q', 'r']
})

return df

def handle(client, data):

# do some set up to fetch the data-frame
dataframe = get_data_frame()

# then you can convert the dataframe to xls-format.
# to avoid storing directly to a file, we can use io.BytesIO()
output_buffer = io.BytesIO()
df.to_excel(output, sheet_name='my_sheet', index=False)


# the FilesAPI accepts BytesIO as input in `client.files.create`
client.files.upload_bytes(output_buffer, name="my_file", ...)

I hope this helps! Let me know if you need further assistance. 

Userlevel 3

Hi @eashwar11, was the above helpful?

Badge +2

Yes @Carin Meems . Thanks @Ivar Stangeby 

Reply