2.9.1 身份证号年龄提取

from openpyxl import load_workbook
from datetime import datetime

def get_time():
    wb = load_workbook('./办公自动化/files/people_info.xlsx')

    sh = wb['Sheet']

    # 当前时间
    now_year = datetime.now().year
    # print(now_year)

    max_column = sh.max_column

    for i, cell in enumerate(sh['C']):
        if i >= 1:   # 第一行是表格标题
            pno = cell.value    # 身份证号码:6位行政区号 4位年  2位月  2位日  4位个人识别码
            year = pno[6:10]
            month = pno[10:12]
            day = pno[12:14]
            # print(i, pno, year, month, day)
            age = now_year - int(year)
            # print(i, age)
            sh.cell(i + 1, max_column + 1).value = age
    tmp_list = []
    for rows in sh[1]:
        tmp_list.append(rows.value)

    if '年龄' in tmp_list:
        print("年龄已生成!重新生成请删除年龄列!")

    else:
       sh.cell(1, max_column + 1).value = '年龄'
       wb.save('./办公自动化/files/people_info.xlsx')

if __name__ == '__main__':
    get_time()