python分析excel(column判断写入,复制excel等)

import openpyxl
from datetime import datetime,timedelta
import pandas as pd
import os, sys,re,shutil


tt = datetime.now().timetuple()
today = str(tt.tm_year)+'-'+str(tt.tm_mon)+'-'+str(tt.tm_mday)
print(today)
file_name = pd.read_excel("G:\\python\\szhk\\data-example.xlsx")
new_filename = file_name.to_excel("G:\\python\\szhk\\data-example" + '_'+today+".xlsx",index=False)

workbook = openpyxl.load_workbook("G:\\python\\szhk\\data-example" + '_'+today+".xlsx")
worksheet = workbook.worksheets[0]

print(worksheet)
now = (datetime.now() + timedelta(days=1))
print(now)
year = now.year
month = now.month
day = now.day
area = {"11": "北京", "12": "天津", "13": "河北", "14": "山西", "15": "内蒙古", "21": "辽宁", "22": "吉林", "23": "黑龙江",
            "31": "上海", "32": "江苏", "33": "浙江", "34": "安徽", "35": "福建", "36": "江西", "37": "山东", "41": "河南",
            "42": "湖北", "43": "湖南", "44": "广东", "45": "广西", "46": "海南", "50": "重庆", "51": "四川", "52": "贵州",
            "53": "云南", "54": "西藏", "61": "陕西", "62": "甘肃", "63": "青海", "64": "宁夏", "65": "新疆", "71": "台湾",
            "81": "香港", "82": "澳门", "91": "国外"}
Errors = ['验证通过!', '身份证号码位数不对!', '身份证号码出生日期超出范围或含有非法字符!', '身份证号码校验错误!', '身份证地区非法!']


worksheet.insert_cols(16)
worksheet.insert_cols(17)
worksheet.insert_cols(18)

#获取所有列名 并且判断是否已经存在?
for index,row in enumerate(worksheet.rows):
    if index == 0:
        row[15].value = '归属地'
        row[16].value = '性别'
        row[17].value = '年龄'

    else:
        try:
            province = row[9].value[0:2]
            if province in area.keys():
                row[15].value = area.get(province)
            else:
                row[15].value = Errors[3]
        except ValueError:
            pass

        try:
            birth_year = int(row[9].value[6:10])
            birth_month = int(row[9].value[10:12])
            birth_day = int(row[9].value[12:14])
            if year == birth_year:
                row[17].value = 0

            else:
                if birth_month > month or (birth_month == month and birth_day > day):
                    row[17].value=year - birth_year - 1
                else:
                    row[17].value=year - birth_year
        except ValueError:
            pass

        try:
            if int(row[9].value[16:17]) % 2 == 0:
                row[16].value = '女'
            elif int(row[9].value[16:17]) % 2 == 1:
                row[16].value = '男'
            else:
                row[16].value = 'other'
        except ValueError:
            pass


workbook.save("G:\\python\\szhk\\data-example" + '_' + today + ".xlsx")

 

你可能感兴趣的:(数据可视化,数据分析,python)