import shutil
import os
import time
import openpyxl

from django.contrib import messages
from THumano.models import Personales
from TransitoApp.dbs import BASE_DIR
from celery import shared_task

def reporte_task():
    print('Iniciando tarea del reporte..!')
    lista = []
    path = os.path.join(BASE_DIR, 'siith.xlsx')
    path1= os.path.join(BASE_DIR,'static/siith.xlsx')
    try:
        os.remove(path1)
    except Exception as error:
        print(error)
    time.sleep(3)
    shutil.copy(path, path1)
    path = path1
    libro = openpyxl.load_workbook(path)
    sheet = libro.get_sheet_by_name('personal')

    personales = Personales.objects.all().order_by('usuario__last_name')
    row = 5
    for p in personales:
        print('creando para: ',p.usuario.username)
        # datos personales
        a = sheet.cell(row=row, column=1)
        a.value = str.upper(p.usuario.first_name + " " + p.usuario.last_name)
        b = sheet.cell(row=row, column=2)
        b.value = p.usuario.username
        c = sheet.cell(row=row, column=3)
        c.value = p.l_nacimiento
        d = sheet.cell(row=row, column=4)
        d.value = p.f_nacimiento
        e = sheet.cell(row=row, column=5)
        e.value = p.edad
        f = sheet.cell(row=row, column=6)
        f.value = str.upper(p.genero)
        g = sheet.cell(row=row, column=7)
        g.value = str.upper(p.etnia)
        h = sheet.cell(row=row, column=8)
        h.value = str.upper(p.e_civil)
        i = sheet.cell(row=row, column=9)
        i.value = str.upper(p.nacionalidad)
        # licencias
        j = sheet.cell(row=row, column=10)
        j.value = "SI" if p.usuario.licencias_set.last() else "NO"
        k = sheet.cell(row=row, column=11)
        k.value = p.usuario.licencias_set.last().tipo if p.usuario.licencias_set.last() else "-"
        l = sheet.cell(row=row, column=12)
        l.value = p.usuario.licencias_set.last().f_caducidad if p.usuario.licencias_set.last() else "-"
        m = sheet.cell(row=row, column=13)
        m.value = "VIGENTE" if p.usuario.licencias_set.last() else "NO VIGENTE"
        # datos de contacto
        n = sheet.cell(row=row, column=14)
        n.value = p.telefono
        o = sheet.cell(row=row, column=15)
        o.value = p.celular
        p_ = sheet.cell(row=row, column=16)
        p_.value = str.upper(p.correo_p)
        q = sheet.cell(row=row, column=17)
        q.value = str.upper(p.correo_i)
        # contacto de emergencia
        r = sheet.cell(row=row, column=18)
        r.value = str.upper(p.nombre_contacto) if p.nombre_contacto else "-"
        s = sheet.cell(row=row, column=19)
        s.value = str.upper(p.parentesco) if p.parentesco else "-"
        t = sheet.cell(row=row, column=20)
        t.value = p.cedula_contacto
        u = sheet.cell(row=row, column=21)
        u.value = p.convencional_contacto
        v = sheet.cell(row=row, column=22)
        v.value = p.celular_contacto
        w = sheet.cell(row=row, column=23)
        w.value = str.upper(p.direccion) if p.direccion else "-"
        # instruccion formal:
        x = sheet.cell(row=row, column=24)
        x.value = "SI" if p.titulo else "NO"
        y = sheet.cell(row=row, column=25)
        y.value = str.upper(p.titulo) if p.titulo else "-"
        z = sheet.cell(row=row, column=26)
        z.value = str.upper(p.pais) if p.titulo else "-"
        aa = sheet.cell(row=row, column=27)
        aa.value = str.upper(p.area) if p.titulo else "-"
        ab = sheet.cell(row=row, column=28)
        ab.value = str.upper(p.institucion_educativa) if p.titulo else "-"
        ac = sheet.cell(row=row, column=29)
        ac.value = p.numero_periodos if p.titulo else "-"
        ad = sheet.cell(row=row, column=30)
        ad.value = str.upper(p.tipo_periodo) if p.titulo else "-"
        ae = sheet.cell(row=row, column=31)
        ae.value = str.upper(p.egresado) if p.titulo else "-"
        af = sheet.cell(row=row, column=32)
        af.value = str.upper(p.numero_registro) if p.titulo else "-"
        # del domicilio
        ag = sheet.cell(row=row, column=33)
        ag.value = str.upper(p.lugar)
        ah = sheet.cell(row=row, column=34)
        ah.value = str.upper(p.provincia)
        ai = sheet.cell(row=row, column=35)
        ai.value = str.upper(p.canton)
        aj = sheet.cell(row=row, column=36)
        aj.value = str.upper(p.parroquia)
        ak = sheet.cell(row=row, column=37)
        ak.value = str.upper(p.barrio)
        al = sheet.cell(row=row, column=38)
        al.value = str.upper(p.calle_principal)
        am = sheet.cell(row=row, column=39)
        am.value = str.upper(p.calle_secundaria)
        an = sheet.cell(row=row, column=40)
        an.value = p.numero_casa
        ao = sheet.cell(row=row, column=41)
        ao.value = str.upper(p.referencia)
        # informacion de salud
        ap = sheet.cell(row=row, column=42)
        ap.value = p.tipo_sangre
        aq = sheet.cell(row=row, column=43)
        aq.value = str.upper(p.discapacidad) if p.discapacidad else "NO"
        ar = sheet.cell(row=row, column=44)
        ar.value = str.upper(p.tipo_discapacidad) if p.tipo_discapacidad else "NINGUNA"
        as_ = sheet.cell(row=row, column=45)
        as_.value = p.porcentaje_discapacidad
        at = sheet.cell(row=row, column=46)
        at.value = p.carnet_CONADIS if p.carnet_CONADIS else "NO TIENE"
        au = sheet.cell(row=row, column=47)
        au.value = p.sustituto if p.sustituto else "NO TIENE"
        av = sheet.cell(row=row, column=48)
        av.value = p.fecha_declaracion
        aw = sheet.cell(row=row, column=49)
        aw.value = p.nombres_familiar if p.nombres_familiar else "NO TIENE"
        ax = sheet.cell(row=row, column=50)
        ax.value = p.telefono_sustituto if p.telefono_sustituto else "NO TIENE"
        # datos del contrato o nombramiento:
        ba = sheet.cell(row=row, column=53)
        ba.value = p.ingreso_institucion
        bb = sheet.cell(row=row, column=54)
        bb.value = p.tiempo_servicio
        bc = sheet.cell(row=row, column=55)
        bc.value = p.ultima_fecha_entrada_servicio_publico
        bd = sheet.cell(row=row, column=56)
        bd.value = str(p.ultimo_tiempo_servicio_anios) + " / " + str(p.ultimo_tiempo_servicio_meses)
        be = sheet.cell(row=row, column=57)
        be.value = p.finalizacion_contrato
        bf = sheet.cell(row=row, column=58)
        bf.value = "ACTIVO" if p.estado else "NO ACTIVO"
        # informacion para pagos:
        bg = sheet.cell(row=row, column=59)
        bg.value = "SI" if p.acumulacion else "NO"
        bh = sheet.cell(row=row, column=60)
        bh.value = str(p.cuenta_bancaria)
        bi = sheet.cell(row=row, column=61)
        bi.value = str.upper(p.banco) if p.banco else "-"
        bj = sheet.cell(row=row, column=62)
        bj.value = str.upper(p.tipo_cuenta) if p.tipo_cuenta else "-"
        # informacion del contrato:
        bk = sheet.cell(row=row, column=63)
        bk.value = p.solicitud
        bl = sheet.cell(row=row, column=64)
        bl.value = p.fecha_entrega
        bm = sheet.cell(row=row, column=65)
        bm.value = p.informe_tecnico
        bn = sheet.cell(row=row, column=66)
        bn.value = p.completado
        bo = sheet.cell(row=row, column=67)
        bo.value = p.firma
        bp = sheet.cell(row=row, column=68)
        bp.value = p.servidor
        bq = sheet.cell(row=row, column=69)
        bq.value = p.finanzas
        br = sheet.cell(row=row, column=70)
        br.value = p.archivo
        bs = sheet.cell(row=row, column=71)
        bs.value = p.numero_meses
        bt = sheet.cell(row=row, column=72)
        bt.value = p.remuneracion
        libro.save(path)
        # condiciones de vivienda:110
        try:
            df = sheet.cell(row=row, column=110)
            df.value = "SI" if p.usuario.condicionesvida_set.first().agua else "NO"
            dg = sheet.cell(row=row, column=111)
            dg.value = "SI" if p.usuario.condicionesvida_set.first().alumbrado_vivienda else "NO"
            dh = sheet.cell(row=row, column=112)
            dh.value = "SI" if p.usuario.condicionesvida_set.first().alumbrado_publico else "NO"
            di = sheet.cell(row=row, column=113)
            di.value = "SI" if p.usuario.condicionesvida_set.first().alcantarillado else "NO"
            dj = sheet.cell(row=row, column=114)
            dj.value = "BUENO" if p.usuario.condicionesvida_set.first().calles else "MALO"
            dk = sheet.cell(row=row, column=115)
            dk.value = "SI" if p.usuario.condicionesvida_set.first().basura else "NO"
            dl = sheet.cell(row=row, column=116)
            dl.value = "SI" if p.usuario.condicionesvida_set.first().iglesia else "NO"
            dm = sheet.cell(row=row, column=117)
            dm.value = "SI" if p.usuario.condicionesvida_set.first().hospital else "NO"
            dn = sheet.cell(row=row, column=118)
            dn.value = "SI" if p.usuario.condicionesvida_set.first().centro_medico else "NO"
            do = sheet.cell(row=row, column=119)
            do.value = "SI" if p.usuario.condicionesvida_set.first().subcentro else "NO"
            dp = sheet.cell(row=row, column=120)
            dp.value = "SI" if p.usuario.condicionesvida_set.first().escuela else "NO"
            dq = sheet.cell(row=row, column=121)
            dq.value = "SI" if p.usuario.condicionesvida_set.first().colegio else "NO"
            dr = sheet.cell(row=row, column=122)
            dr.value = "SI" if p.usuario.condicionesvida_set.first().mercado else "NO"
            ds = sheet.cell(row=row, column=123)
            ds.value = "SI" if p.usuario.condicionesvida_set.first().telefono_convencional else "NO"
            dt = sheet.cell(row=row, column=124)
            dt.value = "SI" if p.usuario.condicionesvida_set.first().telefono_celular else "NO"
            du = sheet.cell(row=row, column=125)
            du.value = "SI" if p.usuario.condicionesvida_set.first().internet else "NO"
            dv = sheet.cell(row=row, column=126)
            dv.value = "SI" if p.usuario.condicionesvida_set.first().transporte else "No"
            dw = sheet.cell(row=row, column=127)
            dw.value = "SI" if p.usuario.condicionesvida_set.first().seguridad else "NO"
            dx = sheet.cell(row=row, column=128)
            dx.value = "SI" if p.usuario.condicionesvida_set.first().estado_calles else "NO"
            dy = sheet.cell(row=row, column=129)
            dy.value = "SI" if p.usuario.condicionesvida_set.first().aceras else "NO"
            dz = sheet.cell(row=row, column=130)
            dz.value = "SI" if p.usuario.condicionesvida_set.first().bordillos else "NO"
            ea = sheet.cell(row=row, column=131)
            ea.value = "SI" if p.usuario.condicionesvida_set.first().colector_aguas_lluvias else "NO"
            eb = sheet.cell(row=row, column=132)
            eb.value = str.upper(p.usuario.condicionesvida_set.first().otros)
            ec = sheet.cell(row=row, column=133)
            ec.value = "SI" if p.usuario.condicionesvida_set.first().tenencia_vivienda else "NO"
            ed = sheet.cell(row=row, column=134)
            ed.value = str.upper(p.usuario.condicionesvida_set.first().tipo_vivienda)

            ee = sheet.cell(row=row, column=135)
            ee.value = str.upper(p.usuario.condicionesvida_set.first().tipo_familia)
            ef = sheet.cell(row=row, column=136)
            ef.value = "-"
            eg = sheet.cell(row=row, column=137)
            eg.value = "SI" if p.usuario.condicionesvida_set.first().dormitorios else "NO"
            eh = sheet.cell(row=row, column=138)
            eh.value = "SI" if p.usuario.condicionesvida_set.first().sala else "NO"
            ei = sheet.cell(row=row, column=139)
            ei.value = "SI" if p.usuario.condicionesvida_set.first().comedor else "NO"
            ej = sheet.cell(row=row, column=140)
            ej.value = "SI" if p.usuario.condicionesvida_set.first().sala_comedor else "NO"
            ej = sheet.cell(row=row, column=141)
            ej.value = "SI" if p.usuario.condicionesvida_set.first().cocina else "NO"
            ej = sheet.cell(row=row, column=142)
            ej.value = "SI" if p.usuario.condicionesvida_set.first().banios else "NO"
            ej = sheet.cell(row=row, column=143)
            ej.value = "SI" if p.usuario.condicionesvida_set.first().patio else "NO"
            ej = sheet.cell(row=row, column=144)
            ej.value = "SI" if p.usuario.condicionesvida_set.first().garaje else "NO"
            ej = sheet.cell(row=row, column=145)
            ej.value =str.upper( p.usuario.condicionesvida_set.first().otros_c)
            ej = sheet.cell(row=row, column=146)
            ej.value = "SI" if p.usuario.condicionesvida_set.first().potable else "NO"
            ej = sheet.cell(row=row, column=147)
            ej.value = "SI" if p.usuario.condicionesvida_set.first().hervida else "No"
            ej = sheet.cell(row=row, column=148)
            ej.value = "SI" if p.usuario.condicionesvida_set.first().tratada else "NO"
            ej = sheet.cell(row=row, column=149)
            ej.value = "SI" if p.usuario.condicionesvida_set.first().cisterna else "NO"
            ej = sheet.cell(row=row, column=150)
            ej.value = "SI" if p.usuario.condicionesvida_set.first().pozo else "NO"
            ej = sheet.cell(row=row, column=151)
            ej.value = "SI" if p.usuario.condicionesvida_set.first().tanqueros else "NO"
            ej = sheet.cell(row=row, column=152)
            ej.value =str.upper( p.usuario.condicionesvida_set.first().otro_a)
            ej = sheet.cell(row=row, column=153)
            ej.value = "SI" if p.usuario.condicionesvida_set.first().alimentacion else "NO"
            ej = sheet.cell(row=row, column=154)
            ej.value = "SI" if p.usuario.condicionesvida_set.first().recolector_basura else "NO"
            ej = sheet.cell(row=row, column=155)
            ej.value = "SI" if p.usuario.condicionesvida_set.first().quma_basura else "NO"
            ej = sheet.cell(row=row, column=156)
            ej.value =str.upper(p.usuario.condicionesvida_set.first().otro_b)
            libro.save(path)
        except Exception as error:
            print("error", error, "condiciones de vivienda")
            libro.save(path)
        # gastos:
        try:
            fa = sheet.cell(row=row, column=157)
            fa.value = p.usuario.informacionmonetaria_set.first().vivienda
            fb = sheet.cell(row=row, column=158)
            fb.value =p.usuario.informacionmonetaria_set.first().manutencion
            fc = sheet.cell(row=row, column=159)
            fc.value =p.usuario.informacionmonetaria_set.first().educacion
            fd = sheet.cell(row=row, column=160)
            fd.value =p.usuario.informacionmonetaria_set.first().medicina
            fe = sheet.cell(row=row, column=161)
            fe.value =p.usuario.informacionmonetaria_set.first().alimentacion
            ff = sheet.cell(row=row, column=162)
            ff.value =p.usuario.informacionmonetaria_set.first().vestuario
            fg = sheet.cell(row=row, column=163)
            fg.value =p.usuario.informacionmonetaria_set.first().extraordinario
            fh = sheet.cell(row=row, column=164)
            fh.value =p.usuario.informacionmonetaria_set.first().total
            fi = sheet.cell(row=row, column=165)
            fi.value =p.usuario.informacionmonetaria_set.first().ahorro
            libro.save(path)
        except Exception as error:
            print(error, "error condidionces monetarias")
            libro.save(path)
        #apoyo economico: 177
        try:
            fu = sheet.cell(row=row, column=177)
            fu.value ="SI" if p.usuario.apoyoeconomico_set.first().padres else "NO"
            fv = sheet.cell(row=row, column=178)
            fv.value ="SI" if p.usuario.apoyoeconomico_set.first().hijo else "NO"
            fw = sheet.cell(row=row, column=179)
            fw.value ="SI" if p.usuario.apoyoeconomico_set.first().conyuge else "NO"
            fx = sheet.cell(row=row, column=180)
            fx.value ="SI" if p.usuario.apoyoeconomico_set.first().otro else "NO"
            libro.save(path)
        except Exception as error:
            print("apoyo economico", error)
            libro.save(path)
#informacion social 181:
        try:
            fy = sheet.cell(row=row, column=181)
            fy.value ="SI" if p.usuario.informacionsocial_set.first().servidor else "NO"
            fz = sheet.cell(row=row, column=182)
            fz.value ="SI" if p.usuario.informacionsocial_set.first().conyuge else "NO"
            ga = sheet.cell(row=row, column=183)
            ga.value ="SI" if p.usuario.informacionsocial_set.first().familiares else "NO"
            gb = sheet.cell(row=row, column=184)
            gb.value ="SI" if p.usuario.informacionsocial_set.first().guarderias else "NO"
            gc = sheet.cell(row=row, column=185)
            gc.value ="SI" if p.usuario.informacionsocial_set.first().empleada_domestica else "NO"
            gd = sheet.cell(row=row, column=186)
            gd.value ="SI" if p.usuario.informacionsocial_set.first().vecinos else "NO"
            ge = sheet.cell(row=row, column=187)
            ge.value ="SI" if p.usuario.informacionsocial_set.first().otros else "NO"
            gf = sheet.cell(row=row, column=188)
            gf.value ="SI" if p.usuario.informacionsocial_set.first().leer else "NO"
            gg = sheet.cell(row=row, column=189)
            gg.value ="SI" if p.usuario.informacionsocial_set.first().escuchar_musica else "NO"
            gh = sheet.cell(row=row, column=190)
            gh.value ="SI" if p.usuario.informacionsocial_set.first().deportes else "NO"
            gi = sheet.cell(row=row, column=191)
            gi.value ="SI" if p.usuario.informacionsocial_set.first().reuniones else "NO"
            gj = sheet.cell(row=row, column=192)
            gj.value ="SI" if p.usuario.informacionsocial_set.first().quehaceres else "NO"
            gk = sheet.cell(row=row, column=193)
            gk.value ="SI" if p.usuario.informacionsocial_set.first().trabajos_extras else "NO"
            gl = sheet.cell(row=row, column=194)
            gl.value ="SI" if p.usuario.informacionsocial_set.first().otro else "NO"
            libro.save(path)
        except Exception as error:
            print("error informacion social", error)
            libro.save(path)
        # capacitacion: 73
        row1=row
        try:
            for capacitacion in p.usuario.capacitacion_set.all():
                bu = sheet.cell(row=row1, column=73)
                bu.value = str.upper(capacitacion.evento)
                bv = sheet.cell(row=row1, column=74)
                bv.value = str.upper(capacitacion.tipo)
                bw = sheet.cell(row=row1, column=75)
                bw.value = capacitacion.duracion
                bx = sheet.cell(row=row1, column=76)
                bx.value = str.upper(capacitacion.auspiciante)
                by = sheet.cell(row=row1, column=77)
                by.value = str.upper(capacitacion.tipo_certificado)
                bz = sheet.cell(row=row1, column=78)
                bz.value = str.upper(capacitacion.certificado_por)
                ca = sheet.cell(row=row1, column=79)
                ca.value = capacitacion.fecha_inicio
                cb = sheet.cell(row=row1, column=80)
                cb.value = capacitacion.fecha_finalizacion
                cc = sheet.cell(row=row1, column=81)
                cc.value = str.upper(capacitacion.pais)
                row1 += 1
                lista.append(row1)
                libro.save(path)
        except Exception as error:
            print("error en capacitacion", error)
            libro.save(path)
            row1 += 1
        row2 = row
        try:
            for tray in p.usuario.trayectorialaboral_set.all():
                #trayectoria laboral:82
                cd = sheet.cell(row=row2, column=82)
                cd.value = str.upper(tray.tipo_insititucion)
                ce = sheet.cell(row=row2, column=83)
                ce.value =tray.fecha_ingreso
                cf = sheet.cell(row=row2, column=84)
                cf.value =str.upper(tray.nombre_insitutucion)
                cg = sheet.cell(row=row2, column=85)
                cg.value =str.upper(tray.unidad_administrativa)
                ch = sheet.cell(row=row2, column=86)
                ch.value =str.upper(tray.puesto)
                ci = sheet.cell(row=row2, column=87)
                ci.value =tray.fecha_salida
                cj = sheet.cell(row=row2, column=88)
                cj.value =str.upper(tray.motivo_ingreso)
                ck = sheet.cell(row=row2, column=89)
                ck.value =str.upper(tray.motivo_salida)
                libro.save(path)
                row2+=1
                lista.append(row2)
        except Exception as error:
            print(error, "trayectoria laboral error")
            libro.save(path)
            row2 += 1
        #estructura familiar: 90
        row3=row
        try:
            for estructura in p.usuario.vivienda_set.all():
                cl = sheet.cell(row=row3, column=90)
                cl.value = str.upper(estructura.tipo_relacion)
                cm = sheet.cell(row=row3, column=91)
                cm.value =str.upper(estructura.tipo_documento)
                cn = sheet.cell(row=row3, column=92)
                cn.value =estructura.numero_documento
                co = sheet.cell(row=row3, column=93)
                co.value =str.upper(estructura.nombres)
                cp = sheet.cell(row=row3, column=94)
                cp.value =estructura.fecha_nacimiento
                cq = sheet.cell(row=row3, column=95)
                cq.value =estructura.edad
                cr = sheet.cell(row=row3, column=96)
                cr.value =str.upper(estructura.estado_civil)
                cs = sheet.cell(row=row3, column=97)
                cs.value =str.upper(estructura.instruccion_formal)
                ct = sheet.cell(row=row3, column=98)
                ct.value =str.upper(estructura.profesion)
                cu = sheet.cell(row=row3, column=99)
                cu.value =str.upper(estructura.institucion_labora)
                cv = sheet.cell(row=row3, column=100)
                cv.value =str.upper(estructura.tipo_institucion)
                cw = sheet.cell(row=row3, column=101)
                cw.value =estructura.ingreso_mensual
                cx = sheet.cell(row=row3, column=102)
                cx.value ="SI" if estructura.discapacidad else "NO"
                cy = sheet.cell(row=row3, column=103)
                cy.value =str.upper(estructura.tipo_discapacidad)
                cz = sheet.cell(row=row3, column=104)
                cz.value =estructura.porcentaje_discapacidad
                da = sheet.cell(row=row3, column=105)
                da.value =estructura.carnet_conadis
                db = sheet.cell(row=row3, column=106)
                db.value =str.upper(estructura.enfermedad)
                dc = sheet.cell(row=row3, column=107)
                dc.value =str.upper(estructura.tipo_enfermedad)
                dd = sheet.cell(row=row3, column=108)
                dd.value =str.upper(estructura.tipo_sangre)
                de = sheet.cell(row=row3, column=109)
                de.value ="SI" if estructura.vive_con_usted else "NO"
                libro.save(path)
                row3+=1
                lista.append(row3)
        except Exception as error:
            print("error estructura familiar", error)
            libro.save(path)
            row3 += 1
        #enfermedades: 166
        row4 = row
        try:
            for enfermedad in p.usuario.enfermedadesaccidentes_set.all():
                fj = sheet.cell(row=row4, column=166)
                fj.value ="SI" if enfermedad.enfermedades else "NO"
                fk = sheet.cell(row=row4, column=167)
                fk.value =str.upper(enfermedad.enfermedades_hereditarias)
                fl = sheet.cell(row=row4, column=168)
                fl.value ="SI" if enfermedad.accidente else "NO"
                fm = sheet.cell(row=row4, column=169)
                fm.value =str.upper(enfermedad.tipo_accidente)
                fn = sheet.cell(row=row4, column=170)
                fn.value =str.upper(enfermedad.consecuencia)
                fo = sheet.cell(row=row4, column=171)
                fo.value ="SI" if enfermedad.hospital_publico else "NO"
                fp = sheet.cell(row=row4, column=172)
                fp.value ="SI" if enfermedad.hospital_IESS else "NO"
                fq = sheet.cell(row=row4, column=173)
                fq.value ="SI" if enfermedad.clinica else "NO"
                fr = sheet.cell(row=row4, column=174)
                fr.value ="SI" if enfermedad.centro_salud else "NO"
                fs = sheet.cell(row=row4, column=175)
                fs.value ="SI" if enfermedad.medico_particular else "NO"
                ft = sheet.cell(row=row4, column=176)
                ft.value ="SI" if enfermedad.otro else "NO"
                libro.save(path)
                row4 += 1
                lista.append(row4)
        except Exception as error:
            libro.save(path)
            row4 += 1
            print("error en enfermedades_ accidentes", error)
        #enfermedades_base:
        row5=row
        try:
            for e in p.usuario.enfermedades_set.all():
                ay = sheet.cell(row=row5, column=51)
                ay.value = "SI" if e.catastrofica else "NO"
                az = sheet.cell(row=row5, column=52)
                az.value = str.upper(e.enfermedad)
                libro.save(path)
                row5 += 1
                lista.append(row5)
        except Exception as error:
            print("error, enfermedades del usuario", error)
            libro.save(path)
            row5 += 1

        from openpyxl.styles import Font
        libro.font = Font(size=20)
        libro.save(path)
        row=max(lista)
        #messages.add_message(request,messages.INFO,"EL REPORTE SE GENERO EXITOSAMENTE..!")
        print('reporte finalizado')

