在django中输出doc文件

时间:2021-10-11 13:55:57

I want to output the result in to doc file. I am already getting the result in pdf using

我想将结果输出到doc文件。我已经使用pdf获得了结果

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

I want the alternative code for above in python-docx. Please provide any links which gives the detailed example of python docx. But the same file is getting open in libra office. I dont know how. I used

我想在python-docx中使用上面的替代代码。请提供任何链接,其中给出了python docx的详细示例。但同样的文件在天秤座办公室开放。我不知道怎么做。我用了

report_mail(request,result,email,message)
            response = HttpResponse(result.getvalue(), content_type='application/vnd.openxmlformats-officedocument.wordprocessingml.document')
            response['Content-Disposition'] = 'attachment; filename=file.docx'
            return response

getting output as file.docx also. But its not getting open in ms office professional 2010. I am getting the error "The file can not be open because there are problems with the content details:- The file is corrupt and can not be open"

获得输出为file.docx也。但它没有在ms office professional 2010中开放。我收到错误“文件无法打开,因为内容细节存在问题: - 文件已损坏且无法打开”

and for

并为

content_type='application/vnd.openxmlformats-officedocument.wordprocessingml.document'
response['Content-Disposition'] = 'attachment; filename=file.doc'

getting raw data in file like

在文件中获取原始数据

%PDF-1.4
%“Œ‹ž ReportLab Generated PDF document http://www.reportlab.com
% 'BasicFonts': class PDFDictionary 
1 0 obj
% The standard fonts dictionary
<< /F1 2 0 R
 /F2 3 0 R >>
endobj
% 'F1': class PDFType1Font 
2 0 obj
% Font Helvetica
<< /BaseFont /Helvetica
 /Encoding /WinAnsiEncoding
 /Name /F1
 /Subtype /Type1
 /Type /Font >>
endobj

Please help me out for this problem.

请帮我解决这个问题。

1 个解决方案

#1


0  

Pisa is a tool for converting HTML to PDF. It reads a html source and delivers a pdf file. That is what you get. So far so good.

Pisa是一种将HTML转换为PDF的工具。它读取html源并提供pdf文件。这就是你得到的。到现在为止还挺好。

Changing the file name extension from .pdf to .doc or .docx doesn't change its content. You're lucky that LibreOffice is able to open pdf files. Microsoft Office is not.

将文件扩展名从.pdf更改为.doc或.docx不会更改其内容。你很幸运LibreOffice能够打开pdf文件。 Microsoft Office不是。

Creating a docx with python-docx is not the same as using pisa to get a pdf. Again: pisa converts html to pdf, python-docx doesn't convert html to docx. This requires more steps.

使用python-docx创建docx与使用pisa获取pdf不同。再次:pisa将html转换为pdf,python-docx不会将html转换为docx。这需要更多步骤。

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

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

from docx import Document
[...]

document = Document()

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

You need to add content to your document, step by step.

您需要一步一步地向文档中添加内容。

#1


0  

Pisa is a tool for converting HTML to PDF. It reads a html source and delivers a pdf file. That is what you get. So far so good.

Pisa是一种将HTML转换为PDF的工具。它读取html源并提供pdf文件。这就是你得到的。到现在为止还挺好。

Changing the file name extension from .pdf to .doc or .docx doesn't change its content. You're lucky that LibreOffice is able to open pdf files. Microsoft Office is not.

将文件扩展名从.pdf更改为.doc或.docx不会更改其内容。你很幸运LibreOffice能够打开pdf文件。 Microsoft Office不是。

Creating a docx with python-docx is not the same as using pisa to get a pdf. Again: pisa converts html to pdf, python-docx doesn't convert html to docx. This requires more steps.

使用python-docx创建docx与使用pisa获取pdf不同。再次:pisa将html转换为pdf,python-docx不会将html转换为docx。这需要更多步骤。

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

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

from docx import Document
[...]

document = Document()

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

You need to add content to your document, step by step.

您需要一步一步地向文档中添加内容。