import os from docx import Document from docxtpl import DocxTemplate def process_doc(software_name, software_type, version_number, create_time, executors, cases_info): tpl = DocxTemplate("static/report_template/测试报告模版-详情部分.docx") content = { 'software_name': software_name, 'software_type': software_type, 'version_number': version_number, 'create_time': create_time, 'executors': executors } tpl.render(content) tpl.save('static/report-tmp/测试报告详情.docx') for i, key in enumerate(cases_info): tpl = DocxTemplate("static/report_template/测试报告模版-表格部分.docx") content = cases_info[key] content['i'] = i+1 tpl.render(content) tpl.save('static/report-tmp/测试报告表格-{}.docx'.format(i)) document = Document('static/report-tmp/测试报告详情.docx') os.remove('static/report-tmp/测试报告详情.docx') files = os.listdir('static/report-tmp') if '.DS_Store' in files: files.remove('.DS_Store') for i in range(len(files)): sub_doc = Document('static/report-tmp/测试报告表格-{}.docx'.format(i)) for element in sub_doc.element.body: document.element.body.append(element) os.remove('static/report-tmp/测试报告表格-{}.docx'.format(i)) return document