util_doc.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import os
  2. from docx import Document
  3. from docxtpl import DocxTemplate
  4. def process_doc(software_name, software_type, version_number, create_time, executors, cases_info):
  5. tpl = DocxTemplate("static/report_template/测试报告模版-详情部分.docx")
  6. content = {
  7. 'software_name': software_name,
  8. 'software_type': software_type,
  9. 'version_number': version_number,
  10. 'create_time': create_time,
  11. 'executors': executors
  12. }
  13. tpl.render(content)
  14. tpl.save('static/report-tmp/测试报告详情.docx')
  15. for i, key in enumerate(cases_info):
  16. tpl = DocxTemplate("static/report_template/测试报告模版-表格部分.docx")
  17. content = cases_info[key]
  18. content['i'] = i+1
  19. tpl.render(content)
  20. tpl.save('static/report-tmp/测试报告表格-{}.docx'.format(i))
  21. document = Document('static/report-tmp/测试报告详情.docx')
  22. os.remove('static/report-tmp/测试报告详情.docx')
  23. files = os.listdir('static/report-tmp')
  24. if '.DS_Store' in files:
  25. files.remove('.DS_Store')
  26. for i in range(len(files)):
  27. sub_doc = Document('static/report-tmp/测试报告表格-{}.docx'.format(i))
  28. for element in sub_doc.element.body:
  29. document.element.body.append(element)
  30. os.remove('static/report-tmp/测试报告表格-{}.docx'.format(i))
  31. return document