Python handles one-to-many attendance sheets

Description of the problem: The table structure exported by the unit's weekly attendance system is shown in the figure

 It can be seen from the exported table that it is a one-to-many table, and some teachers correspond to 7 records, and some may only have 6 records.

Finally, we need to get the statistics of each teacher's weekly attendance, as shown in the figure:

 In fact, it is a transposition table of attendance time, but the school has a large number of people, and manual work is cumbersome. I processed it with python, and it was very fast.

Note: The exported table should be sorted by teacher name and saved as .xls.

import xlrd
import xlwt

data = xlrd.open_workbook(r"teacherinfo.xls")  # 打开教师考勤表

table = data.sheets()[0]
# 获取所有行
nrow = table.nrows

name = table.cell_value(1, 1)  # 取第一行老师姓名
flag = True

workbook = xlwt.Workbook(encoding='ascii')
worksheet = workbook.add_sheet("My new Sheet", cell_overwrite_ok=True)

row = 0
col = 1

for i in range(1, nrow):

    if flag:
        worksheet.write(row, 0, name)
        flag = False
    if name == table.cell_value(i, 1):
        value = table.cell_value(i, 4)
        worksheet.write(row, col, value)
        col += 1
    else:
        row += 1  # 姓名不相同,变换行
        col = 1
        name = table.cell_value(i, 1)
        value = table.cell_value(i, 4)
        worksheet.write(row, col, value)
        flag = True
        col += 1
workbook.save("export.xls")  # 导出结果

Algorithm idea: Traverse from the beginning to the bottom, write the same name by column, adjust the rows for different, and then write by column.

Guess you like

Origin blog.csdn.net/chinagaobo/article/details/124643640
Recommended