您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

Django输出word文件(.doc),仅在内容中显示原始html

Django输出word文件(.doc),仅在内容中显示原始html

除非您有某种方法将响应(这里是HTML假定的)转换为.doc文件,否则您将获得的只是一个包含扩展名为.doc的响应的文本文件。如果您愿意使用.docx文件,那么有个很棒的python库叫做python- docx,您应该研究一下它,它允许您使用lxml库生成格式正确的docx文件

或者,使用模板,例如:

<html>
<head>
<Meta HTTP-EQUIV=""Content-Type"" CONTENT=""text/html; charset=UTF-8"">
<Meta name=ProgId content=Word.Document>
<Meta name=Generator content=""Microsoft Word 9"">
<Meta name=Originator content=""Microsoft Word 9"">
<style>
@page Section1 {size:595.45pt 841.7pt; margin:1.0in 1.25in 1.0in 1.25in;mso-header-margin:.5in;mso-footer-margin:.5in;mso-paper-source:0;}
div.Section1 {page:Section1;}
@page Section2 {size:841.7pt 595.45pt;mso-page-orientation:landscape;margin:1.25in 1.0in 1.25in 1.0in;mso-header-margin:.5in;mso-footer-margin:.5in;mso-paper-source:0;}
div.Section2 {page:Section2;}
</style>
</head>
<body>
<div class=Section2>
'Section1: Portrait, Section2: Landscape

[your text here]

</div>
</body>
</html>

根据此asp.net论坛帖子,application/msword使用UTF-8charset作为mime类型返回时,这应该创建一个有效的.doc文件(因此,请确保字符串均为unicode)。

Go 2022/1/1 18:37:32 有523人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶