-2

I want to add the option to convert my HTML with information in to a MS word document docx.

I have the PDF with some 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>

Here is the 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

Here is the URL.py

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

Here is the link to download the word:

                    <a href="{% url 'businessplan:order_word' form.instance.id %}">
                      <button type="button" class="btn btn-primary">
                      Download Word
                    </button>
                    </a>
A_K
  • 731
  • 3
  • 15
  • 40
  • You may refer to this answer: [How to generate word document from html in Django](https://stackoverflow.com/questions/4226095/html-to-doc-converter-in-python/4227062#4227062) – Ahmed Ablak Oct 25 '21 at 03:53

1 Answers1

2

I am assuming you are referring to Microsoft word. for creating and editing docx files you can try using "python-docx": https://github.com/python-openxml/python-docx

Slenderman00
  • 107
  • 7