Python-Docx Split Between Controller and View

171 views
Skip to first unread message

David Wellsandt

unread,
Jan 5, 2015, 12:36:58 PM1/5/15
to web...@googlegroups.com
I'm going crazy here to implement a reporting system for .docx files. I read several examples of streaming a file (like CSV outputs and such), but they were all from the controller. Since there will be several report formats for my application, I wanted to move all the reports to the views. I SWEAR I had it working and then fouled it up somehow.

Here's what works.

controllers/sale.py
@auth.requires_login()
def test5():
   
from docx import Document
   
from docx.shared import Inches
   
import StringIO
    output
= StringIO.StringIO()
    document
= Document()
    sales
= db(db.sale.property==db.property.id).select()
    document
.add_heading('Sales', 0)
    response
.headers['Content-Type']='application/vnd.openxmlformats-officedocument.wordprocessingml.document'
    response
.headers['Content-Disposition'] = "attachment; filename=Comparable Sales.docx"
    response
.write(output.getvalue(), escape=False)
    document
.save(output)
   
return output.getvalue()

It produces a .docx file. No data yet, but just a file with the heading caption that reads, "Sales."



Then, I tried splitting stuff up a bit.

controllers/sale.py
@auth.requires_login()
def test4():
    response
.view = 'default/test2.html'
   
return locals()

views/default/test2.html
{{
from docx import Document
from docx.shared import Inches
import StringIO
output
= StringIO.StringIO()
document
= Document()
sales
= db(db.sale.property==db.property.id).select()
document
.add_heading('Sales', 0)
response
.headers['Content-Type']='application/vnd.openxmlformats-officedocument.wordprocessingml.document'
response
.headers['Content-Disposition'] = "attachment; filename=Comparable Sales.docx"
document
.save(output)
response
.write(output.getvalue(), escape=False)
}}

With the split-up deal, it downloads fine. When I open it, Word alerts me first, "The file Comparable Sales.docx cannot be opened because there are problems with the contents."

I click OK and get:

"Word found unreadable content in Comparable Sales.docx. Do you want to recover the contents of this document?"

I click OK and it opens with the correct content.

I've tried several variations, such as moving the sales query to the controller, removing the Content-Disposition header, etc. No luck.

I've tried comparing the underlying document.xml files, but I don't notice any notable differences. I've validated and checked for errors.

So, clearly I'm very close if it's actually getting the content, but what have I done that keeps making it corrupt?

Massimo Di Pierro

unread,
Jan 6, 2015, 12:33:02 PM1/6/15
to web...@googlegroups.com
Just looking at your test5

document is unused (why do you need this object)?
output is a StringIO but you never write anything in it.
You call both response.write(output.getvalue()) and return output.getvalue(). I think you only want the latter. 
I think this: "attachment; filename=Comparable Sales.docx"
should be
"attachment; filename='Comparable Sales.docx'"

What is the desired workflow? Is the docx in the database or is it to be created? How?
Reply all
Reply to author
Forward
0 new messages