Django输出word文件(.doc),只显示内容django中的原始html

时间:2014-05-23 13:57:20

标签: python django doc

我以.doc格式导出报告。报告正在正确导出。但是在微软word中的.doc文件中获取原始数据。但是当我在天秤座办公室打开同一个文件时,我得到了正确的内容。 我不知道我在做什么。

view.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='application/vnd.msword')
            response['Content-Disposition'] = 'attachment; filename=file.doc'
            return response  


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

api.py

from xhtml2pdf import pisa
from docx import Document
from py2docx.docx import Docx 
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()
        document.save(result)
        length = result.tell()
        result.seek(0)

        pdf = pisa.pisaDocument(StringIO.StringIO(html.encode("UTF-8")), dest=result, link_callback=fetch_resources )        

        #pdf = Docx(StringIO.StringIO(html.encode("UTF-8")), dest=result, link_callback=fetch_resources )
        if not pdf.err:
            return result
        else:
            return None

export_pdf是api.py文件中的函数。

请帮我解决这个问题。

0 个答案:

没有答案