在Django项目中将HTML转换/解析为MS Word



我想添加一个选项,将我的HTML与信息转换为MS word文档docx。

我有一个PDF和一些CSS:

<h1>
{{ info.businessName }}<br />
<small style="color: rgb(187, 187, 184); margin: 0px 0px 0px 0px"
>Business Plan
<br />
Prepared {{ info.date_posted|date:"F Y" }}
</small>
</h1>

views.py

def order_word(request, info_id):
info = get_object_or_404(Info, id=info_id)
html = render_to_string('businessplan/word.html', {'info': info})
response = HttpResponse(content_type='application/word')
response['Content-Disposition'] = 'attachment; filename="{}-Business Plan.pdf"'.format(info.businessName)
return response

这是url。py

path('<str:info_id>/word/', order_word, name='order_word'),

这是下载单词的链接:

<a href="{% url 'businessplan:order_word' form.instance.id %}">
<button type="button" class="btn btn-primary">
Download Word
</button>
</a>

我想你指的是Microsoft word。要创建和编辑docx文件,您可以尝试使用"python-docx": https://github.com/python-openxml/python-docx

最新更新