27 lines
		
	
	
		
			926 B
		
	
	
	
		
			Python
		
	
	
	
			
		
		
	
	
			27 lines
		
	
	
		
			926 B
		
	
	
	
		
			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):
 | 
						|
    '''
 | 
						|
    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 |