from openpyxl.workbook import Workbook
from django.conf import settings
from datetime import datetime
from openpyxl.styles import Font, Fill


BASE_DIR = settings.BASE_DIR

def export_consumer(users):
    '''
    params: serializer users
    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 users:
        ws1.append([i['name'], i['username'], i['company_name'], i['ID_number1'], i['nickname'], i['workscope_name'], i['role_name'], i['create_time'], i['create_admin_name']])
    filename = 'users' + datetime.now().strftime("%Y%m%d%H%M%S") +'.xlsx'
    path = '/media/export/' + filename
    wb.save((BASE_DIR + path).replace('\\', '/'))
    return path
    
