Преобразование/парсинг HTML в MS Word в проекте Django
Я хочу добавить возможность конвертировать мой 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