From 0017d8b7294d452c6f88cef807edb2b920453eac Mon Sep 17 00:00:00 2001 From: caoqianming Date: Mon, 30 Oct 2023 09:50:02 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20base=20=E5=A2=9E=E5=8A=A0docxtpl?= =?UTF-8?q?=E5=AF=BC=E5=87=BA=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/utils/export.py | 17 +++++++++++++++++ requirements.txt | 1 + 2 files changed, 18 insertions(+) diff --git a/apps/utils/export.py b/apps/utils/export.py index 8ace324d..67f8d244 100644 --- a/apps/utils/export.py +++ b/apps/utils/export.py @@ -6,6 +6,23 @@ from datetime import datetime from openpyxl import Workbook, styles from openpyxl.drawing.image import Image from openpyxl.utils import get_column_letter, column_index_from_string +from docxtpl import DocxTemplate + + +def export_docx(template_path: str, context_data: dict, file_name: str): + """ + Word导出 + :param template_path: 模板路径 + :param context_data: 数据 + :param file_name: 保存的文件名 + :return:返回文件路径 + """ + docx = DocxTemplate(settings.BASE_DIR + template_path) + docx.render(context_data) + file_path = f'/media/temp/{file_name}' + save_path = settings.BASE_DIR + file_path + docx.save(save_path) + return file_path def len_byte(value): diff --git a/requirements.txt b/requirements.txt index d437de20..97bb44e3 100755 --- a/requirements.txt +++ b/requirements.txt @@ -31,4 +31,5 @@ openpyxl==3.1.0 cron-descriptor==1.2.35 pymysql==1.0.3 face-recognition==1.3.0 +docxtpl==0.16.7 # deepface==0.0.79