40 lines
		
	
	
		
			1.8 KiB
		
	
	
	
		
			Python
		
	
	
	
			
		
		
	
	
			40 lines
		
	
	
		
			1.8 KiB
		
	
	
	
		
			Python
		
	
	
	
| from docxtpl import DocxTemplate, InlineImage
 | |
| from apps.qm.models import TestRecord
 | |
| from apps.qm.serializers import TestRecordDetailSerializer
 | |
| from server.settings import BASE_DIR
 | |
| from utils.tools import ranstr
 | |
| from docx.shared import Mm
 | |
| from apps.mtm.models import RecordFormField
 | |
| 
 | |
| 
 | |
| 
 | |
| def exprot_test_record(tr:TestRecord, tm:str):
 | |
|     data = TestRecordDetailSerializer(instance=tr).data
 | |
|     doc = DocxTemplate(BASE_DIR + tm)
 | |
|     edata = {}
 | |
|     edata['formName'] = data['form_']['name']
 | |
|     edata['glassSpec'] = data['wproduct_']['material_']['specification'] if data['wproduct_'] else ''
 | |
|     edata['glassNum'] =  data['wproduct_']['number'] if data['wproduct_'] else ''
 | |
|     edata['testDate'] = data['update_time'][0:11] if data['update_time'] else data['create_time'][0:11]
 | |
|     tester_s = data['update_by_']['signature'] if data['update_by_'] else data['create_by_']['signature']
 | |
|     if tester_s: # 签名
 | |
|         edata['tester'] = InlineImage(doc, BASE_DIR + tester_s, height=Mm(10))
 | |
|     
 | |
|     for i in data['record_data']:
 | |
|         if i['field_type'] == str(RecordFormField.FIELD_DRAW):
 | |
|             edata[i['field_key']] = {
 | |
|                 's': InlineImage(doc, BASE_DIR + i['field_value'], width=Mm(40)),
 | |
|                 'm': InlineImage(doc, BASE_DIR + i['field_value'], width=Mm(60)),
 | |
|                 'l': InlineImage(doc, BASE_DIR + i['field_value'], width=Mm(80)),
 | |
|             }
 | |
|         else:
 | |
|             edata[i['field_key']] = i['origin_value'] if 'origin_value' in i else ''
 | |
|             if i['field_value'] not in ['', None]:
 | |
|                 edata[i['field_key']] = i['field_value']
 | |
|     # 开始生成word
 | |
|     doc.render(edata)
 | |
|     filename = edata['formName'] + '_' + ranstr(6)
 | |
|     path = '/media/export/' + filename + '.docx'
 | |
|     filepath = BASE_DIR + path
 | |
|     doc.save(filepath)
 | |
|     return path |