Use pandas to generate CSV from tryton


I would like to know if it is possible to generate a csv file from tryton and export it like a report.

I was working with pandas and proteus, but now trying to generate the same report in tryton, from a menuitem in action.

Thanks in advance!

Just create a report like you do for all the other reports. But use the template_extension as ‘plain text’ and extension as ‘Text CSV’ in your report definition. Something like:

<record model="" id="my_report">
    <field name="name">Report</field>
    <field name="model">my.model</field>
    <field name="report_name"></field>
    <field name="report">place/of/report/report.txt</field>
    <field name="template_extension">txt</field>
    <field name="extension">csv</field>

Then in your place/of/report/report.txt you add something like:

{% for r in records %}
{% end %}
1 Like

There could be issue with escaping and so on.

So I would override the render method to return the data that would have been generated by pandas or the stock csv module from python.

1 Like

Thanks @nicoe, I override render method and works fine for the report!

One question more, if I change the report extension to CSV, it gives me the follow message:

TypeError: a bytes-like object is required, not 'str'

I use to get the data a dict (all_companies)

        df = pd.DataFrame(all_companies)
        filename = '/tmp/horario-tpsp' + '.csv'
        data = df.to_csv(filename)
        csv_file = StringIO()
        df.to_csv(csv_file, encoding='utf-8')
        csv_output = csv_file.getvalue()
        return csv_output

Could you show the full traceback?

Hi, these is the full traceback.

Traceback (most recent call last):
  File "/opt/tpsp/lib/python37/site-packages/trytond/wsgipy", line 73, in dispatch_request
    return endpoint(request, **requestview_args)
  File "/opt/tpsp/lib/python37/site-packages/trytond/protocols/dispatcherpy", line 46, in rpc
    request, database_name, *requestrpc_params)
  File "/opt/tpsp/lib/python37/site-packages/trytond/wsgipy", line 44, in auth_required
    return wrapped(*args, **kwargs)
  File "/opt/tpsp/lib/python37/site-packages/trytond/protocols/wrapperspy", line 122, in wrapper
    return func(request, pool, *args, **kwargs)
  File "/opt/tpsp/lib/python37/site-packages/trytond/protocols/dispatcherpy", line 176, in _dispatch
    result = rpcresult(meth(*c_args, **c_kwargs))
  File "/opt/tpsp/lib/python37/site-packages/trytond/report/reportpy", line 182, in execute
    oext, content = cls_execute(records, data, action_report)
  File "/opt/tpsp/lib/python37/site-packages/trytond/report/reportpy", line 190, in _execute
    return clsconvert(action, clsrender(action, report_context))
  File "/opt/tpsp/lib/python37/site-packages/trytond/report/reportpy", line 312, in convert
TypeError: a bytes-like object is required, not 'str'

Apparently your render method returns str but it should return bytes. You’re using StringIO you should use BytesIO instead.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.