【问题标题】:Converting/ Parsing HTML to MS Word in a Django Project在 Django 项目中将 HTML 转换/解析为 MS Word
【发布时间】:2021-12-10 13:21:36
【问题描述】:

我想添加将带有信息的 HTML 转换为 MS Word 文档 docx 的选项。

我有带有一些 CSS 的 PDF:

      <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'),

这里是word的下载链接:

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

【问题讨论】:

标签: python-3.x django django-views


【解决方案1】:

我假设您指的是 Microsoft Word。要创建和编辑 docx 文件,您可以尝试使用“python-docx”:https://github.com/python-openxml/python-docx

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-08
    • 1970-01-01
    • 1970-01-01
    • 2021-06-10
    • 2013-07-24
    • 2012-11-27
    • 2017-07-20
    相关资源
    最近更新 更多