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

时间:2021-10-11 13:56:03

I want to output the .html file result to the .doc file. I tried to use python-docx but not able to figure it out. I am new to django.

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

here s my api.py

这是我的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

Here is my views.py file

这是我的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')

Getting blank docx created. How to add .html file to Document? So please help me to get result output in .docx file.

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

2 个解决方案

#1


0  

There is nothing you add to your document object, therefore you get an empty docx back.

您没有向文档对象添加任何内容,因此您将获得一个空的docx。

According to the python-docx documentation you need to work with the document object like this:

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

from docx import Document
[...]

document = Document()

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

#2


0  

I stumbled into this too and as it turns out it's way easier than I thought.

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

Just keep in mind that the response django object is equivalent to a file object. So:

请记住,响应django对象等同于文件对象。所以:

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

#1


0  

There is nothing you add to your document object, therefore you get an empty docx back.

您没有向文档对象添加任何内容,因此您将获得一个空的docx。

According to the python-docx documentation you need to work with the document object like this:

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

from docx import Document
[...]

document = Document()

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

#2


0  

I stumbled into this too and as it turns out it's way easier than I thought.

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

Just keep in mind that the response django object is equivalent to a file object. So:

请记住,响应django对象等同于文件对象。所以:

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