Django output word files(.doc),only show raw html in the contents

后端 未结 1 1150
春和景丽
春和景丽 2021-01-01 08:16

I am writing a web app using Django 1.4.I want one of my view to output mirosoft word docs using the follwoing codes:

response = HttpResponse(view_data, cont         


        
1条回答
  •  南方客
    南方客 (楼主)
    2021-01-01 08:43

    Unless you have some method of converting your response (here HTML I assume) to a .doc file, all you will get is a text file containing your response with the extension .doc. If you are willing to go for .docx files there is a wonderful python library called python-docx you should look in to that allows you to generate well formed docx files using the lxml library.

    Alternatively, use a template such as:

    
    
    
    
    
    
    
    
    
    
    'Section1: Portrait, Section2: Landscape [your text here]

    This should, according to this asp.net forum post make a valid .doc file when returned as mime type application/msword using UTF-8 charset (so make sure strings are all unicode).

    0 讨论(0)
提交回复
热议问题