Hi,
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!
Hi,
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="ir.action.report" id="my_report">
<field name="name">Report</field>
<field name="model">my.model</field>
<field name="report_name">my.model.report</field>
<field name="report">place/of/report/report.txt</field>
<field name="template_extension">txt</field>
<field name="extension">csv</field>
</record>
Then in your place/of/report/report.txt
you add something like:
"header1","header2","header3"
{% for r in records %}
"${r.item1}","${r.item2}","${r.item3}"
{% end %}
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.
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() csv_file.close() 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
fpwrite(data)
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.
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.