Write a data frame to excel file (.xlsx) template stored in a DataTable on Anvil then download it

I’m looking for a way to write a dataframe to a excel file (.xlsx) template stored in a data table on Anvil and then download that file from the server code or even add the updated excel template back to a row in a data table. I have some thoughts on how to do it but it doesnt seem to work. Is this even possible? Are there better alternatives to accomplishing this?

The dataframe data is being loaded with data analyzed from a file uploader. Below you can see code snippet that I know doesn’t work but I want to make sure that its even possible to do this. Thank you all for your help.

Code Sample:


# paste your code between ``` for row in app_tables.templates.search():
      content = row['Templates']
      content = io.BytesIO(content)

      BlobMedia(content=content.read(), content_type="application/vnd.ms-excel")
      book = load_workbook(table)
      writer = pd.ExcelWriter(content, engine='openpyxl')
      writer.book = book
      writer.sheets = dict((ws.title, ws) for ws in book.worksheets)
    
      report_data.to_excel(writer, 'Adult_Protocol_Data')
      app_tables.templates.add_row(Templates=f)

OR

    for row in app_tables.templates.search():
      content = row['Templates']

      with anvil.media.TempFile(content) as filename:

        book = load_workbook(filename)
        writer = pd.ExcelWriter(f, engine='openpyxl')
        writer.book = book
        writer.sheets = dict((ws.title, ws) for ws in book.worksheets)
      
        report_data.to_excel(writer, 'Adult_Protocol_Data')
        app_tables.templates.add_row(Templates=f)