56 lines
		
	
	
		
			1.9 KiB
		
	
	
	
		
			Python
		
	
	
	
			
		
		
	
	
			56 lines
		
	
	
		
			1.9 KiB
		
	
	
	
		
			Python
		
	
	
	
| from openpyxl.workbook import Workbook
 | |
| from django.conf import settings
 | |
| from datetime import datetime
 | |
| from openpyxl.styles import Font, Fill
 | |
| import json
 | |
| import os
 | |
| 
 | |
| def export_question(questions:object):
 | |
|     '''
 | |
|     params: serializer questions
 | |
|     return: xlsx path
 | |
|     '''
 | |
|     wb = Workbook()
 | |
|     ws1 = wb.active
 | |
|     ws1.title = '题目表'
 | |
|     ws1.append(['分类','题型', '题干', '选项', '正确答案', '解析'])
 | |
|     row = ws1.row_dimensions[1]
 | |
|     row.font = Font(bold=True)
 | |
|     for i in questions:
 | |
|         ws1.append([i.questioncat.name, i.type, i.name, json.dumps(i.options, ensure_ascii=False), ''.join(sorted(i.right)), i.resolution])
 | |
|     filename = 'questions' + datetime.now().strftime("%Y%m%d%H%M%S") +'.xlsx'
 | |
|     path = '/media/temp/'
 | |
|     full_path = settings.BASE_DIR + '/media/temp/'
 | |
|     if not os.path.exists(full_path):
 | |
|         os.makedirs(full_path)
 | |
|     wb.save(full_path+filename)
 | |
|     return path + filename
 | |
| 
 | |
| 
 | |
| def export_record(records:object):
 | |
|     '''
 | |
|     params: serializer records
 | |
|     return: xlsx path
 | |
|     '''
 | |
|     wb = Workbook()
 | |
|     ws1 = wb.active
 | |
|     ws1.title = '答题记录表'
 | |
|     ws1.append(['类型', '用户', '是否通过', '得分', '总分', '耗时(时分秒)', '答题时间', '部门'])
 | |
|     row = ws1.row_dimensions[1]
 | |
|     row.font = Font(bold=True)
 | |
|     for i in records:
 | |
|         tookformat = get_took_format(i.took)
 | |
|         ws1.append([i.type, i.create_by.name, i.is_pass, i.score, i.total_score, str(tookformat), str(i.start_time), i.belong_dept.name])
 | |
|     filename = 'records' + datetime.now().strftime("%Y%m%d%H%M%S") +'.xlsx'
 | |
|     path = '/media/temp/'
 | |
|     full_path = settings.BASE_DIR + '/media/temp/'
 | |
|     if not os.path.exists(full_path):
 | |
|         os.makedirs(full_path)
 | |
|     wb.save(full_path+filename)
 | |
|     return path + filename
 | |
| 
 | |
| 
 | |
| def get_took_format(took:int):
 | |
|     m, s = divmod(took, 60)
 | |
|     h, m = divmod(m, 60)
 | |
|     return "%02d:%02d:%02d" % (h, m, s) |