我正在創建一個帶有兩個引數的函式:一個檔案串列和一個字典,其中包含與檔案中的關鍵字匹配的鍵名的值,這些值由它們周圍的兩個花括號標識:{{Value}}. 我應該將所有檔案合并到一個檔案中,并使用 Django 模板呈現該檔案。Django 會自動讀取關鍵字并用字典值替換它們。就這么簡單,對吧?我想不會,但我得問問。
有沒有辦法做到這一點?這甚至可能嗎?
澄清一下,我已經將所有檔案合二為一,所以只是渲染它讓我感到困惑。有沒有人經歷過類似的情況?
uj5u.com熱心網友回復:
你可以使用這個庫:https ://docxtpl.readthedocs.io/en/latest/
pip install docxtpl
視圖.py
from django.http import HttpResponse
from docxtpl import DocxTemplate
def render_docx(request):
doc = DocxTemplate("your_docx_template.docx")
# you have to place your_docx_template.docx in the root of your project (same level as manage.py).
context = {
# ...
}
response = HttpResponse(content_type="application/vnd.openxmlformats-officedocument.wordprocessingml.document")
response["Content-Disposition"] = 'filename="your_doc_name.docx"'
doc.render(context)
doc.save(response)
return response
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/515129.html
