Escenario
El domingo pasado entró en vigor en toda España la obligatoriedad para las empresas de registrar diariamente la jornada de trabajo de sus empleados. Para llevar el control, basta con mantener una hoja de cálculo en la que el trabajador refleje las horas de entrada y de salida, que ha de estar disponible en caso de una inspección de trabajo.Problema
Por poco que sea, lleva tiempo mantener la hoja de cálculo, se nos puede olvidar y, es probable que, en muchos casos, siempre tengamos que rellenar los mismos datos.Solución
¡Automatización! Para esta ocasión vamos a usar python, y el módulo xlwt para generar hojas de cálculo Excel:
1 |
$ pip3 install xlwt |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
COMPANY_WORKERS = ['Freya', 'Aesir', 'Balder', 'Delling'] DESTINATION_FOLDER = '/tmp' locale.setlocale(locale.LC_ALL, '') now = datetime.datetime.now() def run(): for worker in COMPANY_WORKERS: workbook = Workbook() # add_sheet is used to create sheet. sheet = workbook.add_sheet('Hoja 1') sheet.write(0, 1, 'NOMBRE EMPRESA: GAMCO S.L.') sheet.write(3, 4, '{0} {1}'.format(get_month_name(now.month), now.year)) sheet.write(4, 0, 'NOMBRE TRABAJADOR: ' + worker ) sheet.write(5, 0, 'DIA') sheet.write(5, 1, 'HORA ENTRADA') sheet.write(5, 2, 'HORA SALIDA') sheet.write(5, 3, 'TOTAL HORAS') sheet.write(5, 4, 'FIRMA DEL TRABAJADOR') dayrow = 7 num_days = calendar.monthrange(now.year, now.month)[1] days = [datetime.date(now.year, now.month, day) for day in range(1, num_days + 1)] for day in days: sheet.write(dayrow, 0, str(day.day)) sheet.write(dayrow, 1, '09:00') sheet.write(dayrow, 2, '17:00') sheet.write(dayrow, 3, '8') dayrow = dayrow + 1 workbook.save(DESTINATION_FOLDER + '/' + '{0}_{1}_{2}.xls'.format(worker, now.year, now.month)) def get_month_name(month_no): return locale.nl_langinfo(locale.__dict__["MON_" + str(month_no)]) if __name__ == '__main__': run() |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
COMPANY_WORKERS = ['Freya', 'Aesir', 'Balder', 'Delling'] DESTINATION_FOLDER = '/tmp' locale.setlocale(locale.LC_ALL, '') now = datetime.datetime.now() def run(): for worker in COMPANY_WORKERS: workbook = Workbook() # add_sheet is used to create sheet. sheet = workbook.add_sheet('Hoja 1') sheet.write(0, 1, 'NOMBRE EMPRESA: GAMCO S.L.') sheet.write(3, 4, '{0} {1}'.format(get_month_name(now.month), now.year)) sheet.write(4, 0, 'NOMBRE TRABAJADOR: ' + worker ) sheet.write(5, 0, 'DIA') sheet.write(5, 1, 'HORA ENTRADA') sheet.write(5, 2, 'HORA SALIDA') sheet.write(5, 3, 'TOTAL HORAS') sheet.write(5, 4, 'FIRMA DEL TRABAJADOR') dayrow = 7 day_of_month = datetime.datetime(year=now.year, month=now.month, day=1) actual_month = now.month num_days = calendar.monthrange(now.year, now.month)[1] while day_of_month.month == actual_month: if not is_weekend(day_of_month): minutes = randint(0, 59) sheet.write(dayrow, 0, str(day_of_month.day)) sheet.write(dayrow, 1, '{:02d}:{:02d}'.format(worker['start_hour'], minutes)) sheet.write(dayrow, 2, '{:02d}:{:02d}'.format(worker['exit_hour'], minutes)) sheet.write(dayrow, 3, '8') dayrow = dayrow + 1 day_of_month = day_of_month + datetime.timedelta(days=1) workbook.save(DESTINATION_FOLDER + '/' + '{0}_{1}_{2}.xls'.format(worker, now.year, now.month)) def is_weekend(date): return date.weekday() >= 5 def get_month_name(month_no): return locale.nl_langinfo(locale.__dict__["MON_" + str(month_no)]) if __name__ == '__main__': run() |