Login

Create OpenOffice documents

Author:
king
Posted:
February 25, 2007
Language:
Python
Version:
Pre .96
Tags:
print odf office
Score:
4 (after 4 ratings)

This view will enable you to generate OpenOffice documents from templates written in OpenOffice 2.x

Just make sure that there is no OO tag in between your code (no change in formatting etc.). content.xml is a valid XML file, so you can do some preprocessing using xml.dom.minidom. I would also recommend caching (just save the zip file without content.xml and content.xml on its own).

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import zipfile
import StringIO

from django import http
from django.template import Context, Template


def print_odf( request, odf_path, context ):
    """
      odf_path - path to the template document
      context - dictionary to create context from
    """
    c = Context()
    for key, value in context.items():
        if callable( value ):
            c[key] = value()
        else:
            c[key] = value
       
    # default mimetype
    mimetype = 'application/vnd.oasis.opendocument.text'
    # ODF is just a zipfile
    input = zipfile.ZipFile( template_name, "r" )
    # we cannot write directly to HttpResponse, so use StringIO
    text = StringIO.StringIO()
    # output document
    output = zipfile.ZipFile( text, "a" )
       
    # go through the files in source
    for zi in input.filelist:
        out = input.read( zi.filename )
        # waut for the only interesting file
        if zi.filename == 'content.xml':
            # un-escape the quotes (in filters etc.)
            t = Template( out.replace( '"', '"' ) ) 
            # render the document
            out = t.render( c ) 
        elif zi.filename == 'mimetype':
            # mimetype is stored within the ODF
            mimetype = out 
        output.writestr( zi.filename, out )

    # close and finish
    output.close()
    return http.HttpResponse( content=text.getvalue(), mimetype=mimetype )

More like this

  1. save file on couchdb with couchdbkit by magik_cypress 3 years, 1 month ago
  2. Send large files through Django, and how to generate Zip files by jcrocholl 7 years, 8 months ago
  3. A form field for valdating PDF and Microsoft Word document by jimmylam 6 years, 5 months ago
  4. Auto-documenting Django Models with Sphinx by Voightkampff 3 years, 8 months ago
  5. Make hyperlinks for labels of raw_id_fields (jQuery) by ramen 5 years, 3 months ago

Comments

wiz (on February 27, 2007):

HttpResponse can take file-likes directly: [HTML_REMOVED][HTML_REMOVED] return http.HttpResponse(text) [HTML_REMOVED][HTML_REMOVED]

#

king (on February 27, 2007):

thanks, I updated the snippet

#

akaihola (on February 6, 2008):

Line 23 should probably use "odf_path" instead of "template_name".

#

satels (on June 14, 2011):

See https://github.com/NetAngels/django-webodt

#

Please login first before commenting.