Probleem met code SpreadsheetTemplate

Hoi,

ik probeer resultaten van mijn berekening te exporteren naar Excel (d.m.v Download button) maar het lukte mij niet. Ik heb ook een simpele code die op jullie doc. staat getest maar toch niets opgeleverd.

Ik krijg de volgende foutmelding:

Traceback (most recent call last):
  File "viktor_connector/connector.pyx", line 636, in connector.Job.execute
  File "viktor/core.pyx", line 1513, in viktor.core._handle_job
  File "/usr/src/app/app/viktor_code/concrete/krimpwapening/controller.py", line 187, in download_spreadsheet_krimpwap_pdf
    result = self.get_spreadsheet_krimpwapening(params, entity_id)
  File "/usr/src/app/app/viktor_code/concrete/krimpwapening/controller.py", line 170, in get_spreadsheet_krimpwapening
    result = template.render()
  File "viktor/external/spreadsheet.pyx", line 503, in viktor.external.spreadsheet.SpreadsheetTemplate.render
  File "viktor/external/spreadsheet.pyx", line 593, in viktor.external.spreadsheet._call_fill_spreadsheet_addon
  File "/usr/local/lib/python3.7/base64.py", line 80, in b64decode
    s = _bytes_from_decode_data(s)
  File "/usr/local/lib/python3.7/base64.py", line 46, in _bytes_from_decode_data
    "string, not %r" % s.__class__.__name__) from None
TypeError: argument should be a bytes-like object or ASCII string, not 'NoneType'

Graag hoor ik van jullie hoe ik dit probleem kan oplossen.
Grt, Mohamed

Hi!

I’ll answer in English if you don’t mind! The error occurs because the function is working with a NoneType (or an empty variable). Can you show what happens in the self.get_spreadsheet_krimpwapening function? There is probably something wrong with how you set up the template object. If you use the code shown in the docs, try to print file and cells, and check of they look like what you expect.

Ik heb de hele inhoud van controller geüpload. zie bijlage
controller.py (9.9 KB)

Here is the code you copied from the example,

file = BytesIO(b'my_template')
cells = [
     # DirectInputCell('sheet1', 'A', 1, 5),
     # NamedInputCell('named_cell_1', 'text_to_be_placed'),
]
template = SpreadsheetTemplate(file, cells)

Instead of your template, the variable “file” only contains the bytes content for the text “my_template”. You need to replace that with the bytes-content from your spreadsheet. You can do that as follows:

with open(template_path, 'rb') as f:
  template_as_bytes = BytesIO(f.read())

Also your “cells” variable is just an empty list as everything is commented out, but I a assume that just happened with debugging :wink:

Above that, you have another version

cells = [            ...         ]
template_path = Path(__file__).parent / 'krimpwapening_CROW.xlsx'
template = SpreadsheetTemplate.from_path(template_path, cells)

It is not used now, as the one below replaces it before it is rendered. That version looks fine to me, does that give any issues?

I would advise to use the (newer) render_spreadsheet function. It results in cleaner/shorter code. Also you don’t have to convert to BytesIO but can immediately pass the opened binary file handle. An example can be found here:

The SDK reference to the function:

1 Like

Hello, I am trying the same* and I keep getting these kind of errors:

  File "viktor\external\spreadsheet.pyx", line 510, in viktor.external.spreadsheet.render_spreadsheet
  File "viktor\external\spreadsheet.pyx", line 563, in viktor.external.spreadsheet._call_fill_spreadsheet_addon
  File "viktor\core.pyx", line 1184, in viktor.core._post_on_addon_endpoint
viktor.errors.InternalError: An unexpected error occurred
  • the code in my app is the following:
# in parametrization: 
general.download_btn = DownloadButton("Download file", "get_download_result", longpoll=True)

# in controller:
    def get_download_result(self):

        cells = [
            DirectInputCell('sheet1', 'A', 1, 5)
        ]

        template_path = Path(__file__).parent / 'Export_test.xlsx'

        with open(template_path, 'rb') as template:
            filled_spreadsheet = render_spreadsheet(template, cells)

        return DownloadResult(file_content=filled_spreadsheet, file_name='Export_test_INGEVULD.xlsx')

These seem like the steps as described in the docs and in this topic. What should I do to make it work?

Hi Boudewijn,

The code indeed seems to be correct. One thing I cannot check is that the name of your sheet corresponds to the name you are using the DirectInputCell (i.e. sheet1 is present in your Excel workbook). I would expect a similar error if the sheet name isn’t present in your Excel workbook.

Regards,

Raoul

Indeed that was the (obvious) answer: my Excel is in Dutch so it reads ‘Blad1’ not ‘sheet1’. Thanks!

1 Like