如何将.html文件输出到.doc文件django

时间:2014-05-26 13:41:53

标签: python django

我想将.html文件结果输出到.doc文件。 我试图使用python-docx但无法弄明白。我是django的新手。

这是我的api.py

from docx import Document
from xhtml2pdf import pisa 
from cStringIO import StringIO
def export_pdf(request,id):
        report = Report.objects.get(id=id)
        document = Document()

        options1 = ReportPropertyOption.objects.filter(report=report,is_active=True)   
        locations = []  
        out_string = ""    
        map = None



        for option in options1:  
            option.property = get_property_name(option.property)        
            option.exterior_images = ReportExteriorImages.objects.filter(report = option)  
            option.interior_images = ReportInteriorImages.objects.filter(report = option)
            option.floorplan_images = ReportFloorPlanImages.objects.filter(report = option)
            option.fitouts =    ReportFitOut.objects.filter(propertyoption = option)   
            if (option.gps_longitude):

                locations.append("&markers=color:red|label:S|"+""+str(option.gps_longitude)+","+str(option.gps_latidtude)+"")
        for loc in locations:
            out_string+=loc

        if locations:
            map = "http://maps.google.com/maps/api/staticmap?center=Bangalore&zoom=12&size=512x512&maptype=roadmap"+out_string+"&sensor=true"              
        #map = "http://maps.google.com/maps/api/staticmap?zoom=12&size=400x400&maptype=roadmap&sensor=false&center=\\"
        html  = render_to_string('report/export.html', { 'pagesize' : 'A4', }, context_instance=RequestContext(request,{'options1':options1,'meta':report.meta,'map':map}))

        #result = StringIO.StringIO()
        result = StringIO()
        result1=document.save(result)
        length = result.tell()
        result.seek(0)

        #doc = document(StringIO.StringIO(html.encode("UTF-8")), dest=result, link_callback=fetch_resources )        
        return result

这是我的views.py文件

def export(request,id):
        result = export_pdf(request,id)
        report = Report.objects.get(id=id)
        report.finalised = True       
        report.transaction.state= TransactionState.objects.get(state="Finalized")
        report.transaction.save()
        report.save()
        email = report.transaction.manager.email
        message="" 
        if result:   
            report_mail(request,result,email,message) 
            response = HttpResponse(result.getvalue(), content_type='vnd.openxmlformats-officedocument.wordprocessingml.document')
            response['Content-Disposition'] = 'attachment; filename="my_file.docx"'
            response['Content-Length'] = result.tell()
            return response

        return HttpResponse('Some error occured here! %s')

创建空白docx。如何将.html文件添加到Document? 所以请帮我在.docx文件中获得结果输出。

2 个答案:

答案 0 :(得分:0)

您的document对象中没有添加任何内容,因此您将获得空的docx

根据python-docx文档,您需要使用document对象,如下所示:

from docx import Document
[...]

document = Document()

document.add_heading('Document Title', 0)
[...]
document.save('demo.docx')

答案 1 :(得分:0)

我偶然发现了这一点,事实证明它比我想象的容易。

请记住,response django对象等同于file对象。所以:

def export_word(request):
    """ Download a word docx file """
    response = HttpResponse(mimetype='text/docx')
    response['Content-Disposition'] = 'attachment; filename=download.docx'

    document = Document()
    document.add_heading('Document Title', 0)
    p = document.add_paragraph('A plain paragraph having some text')

    document.save(response)

    return response