Hi,
I have a viktor app which is based on an excelworkheet. I would like the user to be able to download a picture based on the results. For that, another excel file has been created to easily format the picture to be downloaded.
Is it possible to generate a picture based on a range of cells in an excel file which can then be donwloaded/or shown in a view?
Hi @AnneOffermans ,
Welcome to the community! And thanks for reaching out. To better understand, how would you create the picture? There are many ways of using data to create a picture using Python, and there are also ways of extracting your graphs and charts plotted in an Excel spreadsheet to be presented in VIKTOR (and that can be downloaded if wanted).
The tool within VIKTOR has been based on the SpreadsheetCalculation. We have a sheet within the used excel file which visualizes the results based on the inputs from viktor into a good visualization which could be used as “certificate”. This is a different worksheet than the “viktor-output-sheet”. We would like to make a picture/pdf based on the range of cells which have been formatted for the picture/certificate. This picture would then prefferably be downloaded via a button or otherwise shown in a view, from where a screenshot could be taken by the users.
My apologies for not thoroughly understanding. Is it possible for you to share what this image looks like?
The part of the excel file from which I would like to have a picture from looks like the foto. So the cells have been formatted in a certain way, some illustrations have been added as well as some graphs. The resulting picuture would be a screenshot from the selected range in the worksheet.
Hi @AnneOffermans ,
This is clear. Thanks. You could convert this to a PDF by making use of the service we provide to convert Excel to PDFs:
If you do not want to convert the entire Excel to a PDF, then you could make use of the Python package openpyxl
to remove some sheets, and clean up where you please to ensure it looks as intended.
Within the app, I work with the functions SpreadsheetCalculationInput and SpreadsheetCalculation. I have created the following definition to fill the spreadsheet:
def fill_spreadsheet(params) -> vkt.spreadsheet.SpreadsheetCalculation:
inputs=[]
return vkt.spreadsheet.SpreadsheetCalculation.from_path(SPREADSHEET_PATH, inputs=inputs)
And to get the results I use:
def _get_evaluated_spreadsheet_result(self, params, return_file=True): # True of False?
general_inputs = []
for _name in INPUTNAMES:
general_inputs.append(SpreadsheetCalculationInput(_name, eval("params."+_name)))
sheet = SpreadsheetCalculation.from_path(SPREADSHEET_PATH, inputs=general_inputs)
result = sheet.evaluate(include_filled_file=True)
return result
It would be good to get the pdf from a sheet within the same excel file which I use to generate the results. But to do this, I need to hide all the other worksheets, so I need the openpyxl. Would it be possible to use the result from the _get_evaluated_spreadsheet_result() and use openpyxl to hide the worksheets? Or would I need to change the functions as used in Excel in VIKTOR: some tips and tricks (featuring openpyxl)?
I believe you will be able to use the evaluated result rather than doing it as the one link explains. Just be aware of the different formats. You will now have the format SpreadsheetResult
. So to convert this to BytesIO
, which you can use to load into openpyxl
, make sure to take that result, and do the following:
BytesIO(sheet.result.file_content)