Sometimes we have to generate excel sheets or csv files or doc files which contain data of our content types.
Generating Excel Sheets in Plone
Let us see, how we can generate an excel sheet which contains names of Documents and their description.
Add a script in Custom Folder with the sample code, shown below.
Sample code to generate an excel sheet:
# Example code:
# Import a standard function, and get the HTML request and response objects.
from Products.PythonScripts.standard import html_quote
request = container.REQUEST
RESPONSE = request.RESPONSE
counter = 0
print "Number|Name"
documents = context.portal_catalog(portal_type='Document')
for document in documents:
counter += 1
doc = document.getObject()
desc = doc.description
dtit = doc.pretty_title_or_id()
print "%d|%s|%s" % (counter,dtit,desc)
RESPONSE.setHeader("Content-type","application/vnd.ms-excel")
RESPONSE.setHeader("Content-disposition","attachment;filename=Documents.xls")
return printed
To get output file as Excel Sheet, We have to set the response header and specify the Content-type andContent-Disposition,as shown below.
RESPONSE.setHeader("Content-type","application/vnd.ms-excel")
RESPONSE.setHeader("Content-disposition","attachment;filename=Documents.csv")
This script will generate a Microsoft Excel Sheet, with name and description of each document.
Generating CSV Files in Plone
To generate a csv file, we have to replace .xls with .csv extension for Content-disposition.
RESPONSE.setHeader("Content-type","application/vnd.ms-excel")
RESPONSE.setHeader("Content-disposition","attachment;filename=Documents.csv")
This will generate a csv file.
Generating MS Word Documents in Plone
To generate a doc file, we have to set Content-type, to application/vnd.ms-word, and set the extension of the Content-disposition, to .doc.
RESPONSE.setHeader("Content-type","application/vnd.ms-word")
RESPONSE.setHeader("Content-disposition","attachment;filename=Documents.doc")
Comments
Post a Comment