Do you have a content type that you’d like to be able to offer as Comma Separated Value (CSV)/Excel download? Python can create both formats pretty easily (using either the standard csv library, or xlwt) and you can write a view for your content type that will create the file and return it to the user as a file download.
At Plone.org, Martin Aspeli covers this process as a part of the five.grok documentation. I’d already created my product without using five.grok, and I didn’t figure this one use case was enough reason to switch. So here’s what I came up with:
First, register the view in browser/configure.zcml
... <browser:view for="my.product.interfaces.IMyContentType" name="csv_view" class=".mycontenttypeview.MyContentTypeCsvView" permission="zope.Public" /> ...
I added the view to browser/myContentType.py under the default view.
... class MyContentTypeCsvView(BrowserView): """ Download the content type as a CSV file. """ def __call__(self): """ Build a CSV from the content type. """ out = StringIO() writer = csv.writer(out) ... # make the CSV file ... filename = "%s.csv" % context.id self.request.response.setHeader('Content-Type', 'text/csv') self.request.response.setHeader('Content-Disposition', 'attachment; filename="%s"' % filename) return out.getvalue()
Now you should be able to link to http://mysite.com/mycontenttype/csv_view and initiate a file download.