Plotly in excel

Hi support,
My app produces a few values and a plotly graph as a result. I would like to transfer these result to an excel file, so that the results can be downloaded.
I have set up a template excel file with all the values added, but I don’t know how to add the result of the plotly graph to this file.
How can I best do this?

Hi Grianne,

What information exactly are you trying to insert into your excel file?
And what method are you currently using to interact with your excel template?

What I’m trying to insert into my excel file is the graph produced by plotly. It is now shown in the PlotlyView, and I would like to add this to the excel file.
I currently use this function:
def create_spreadsheet_file_content(self, params, entity_id, entity_name) → bytes:
template_path = …(path here)
cells = [
InputCellRange(…etc)
]
template = SpreadsheetTemplate.from_path(template_path, cells)
result = template.render()
return result.file_content

Thanks for the reply, very clear what you are trying to do.
Sadly at this moment it is not possible to paste full images into an excel sheet.
A possibility you could have a look at would be to use an Excel worker and use macros in your sheet.

But my recommendation in this case would be to have an empty plot ready in your template file in which the data is entered from the app using the InputCellRange that you are already using.

Hi Grianne,

A colleague just pointed me to a third option: openpyxl.

This is a little more involved than using the standard excel functionalities built into the sdk but does provide you the option to insert jpg or png images into your excel sheet.
An example of how that could work would be:

def add_image(spreadsheet_template_render: bytes, image: BytesIO, cell_anchor: str = 'G1') -> bytes:
   """Takes a rendered spreadsheet and adds an image at the specified anchor point""" 
   im = Image(image) 
   im.anchor = cell_anchor 
   wb = load_workbook(BytesIO(spreadsheet_template_render)) 
   ws = wb.active 
   ws.add_image(im) 
   xlsx_file = BytesIO() 
   wb.save(xlsx_file) 
   return xlsx_file.getvalue()

Let me know if that helps!