|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- encoding: utf-8 -*- |
| 3 | +""" |
| 4 | +Topic: 将航信excel表数据转换为MySQL中的数据 |
| 5 | +Desc : |
| 6 | +""" |
| 7 | +import sys |
| 8 | +import copy |
| 9 | +from openpyxl import Workbook |
| 10 | +from openpyxl import load_workbook |
| 11 | + |
| 12 | +import logging |
| 13 | +import datetime |
| 14 | +import mysql.connector |
| 15 | +from mysql.connector import errorcode |
| 16 | + |
| 17 | +sql_create1 = """ |
| 18 | + CREATE TABLE t_enterprise ( |
| 19 | + id BIGINT PRIMARY KEY AUTO_INCREMENT COMMENT '主键ID', |
| 20 | + name VARCHAR(300) NOT NULL COMMENT '企业名称', |
| 21 | + tax_code VARCHAR(60) NOT NULL COMMENT '税号', |
| 22 | + region_id BIGINT COMMENT '区域ID', |
| 23 | + customer_type INTEGER COMMENT '客户类型', |
| 24 | + enterprise_type INTEGER COMMENT '企业类型', |
| 25 | + address VARCHAR(200) COMMENT '详细地址', |
| 26 | + postcode VARCHAR(10) COMMENT '邮编', |
| 27 | + tel VARCHAR(30) COMMENT '联系电话', |
| 28 | + contact VARCHAR(10) COMMENT '联系人', |
| 29 | + fax VARCHAR(30) COMMENT '传真', |
| 30 | + mobile VARCHAR(30) COMMENT '手机号', |
| 31 | + created_time DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间', |
| 32 | + updated_time DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间' |
| 33 | + ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COMMENT '企业表'; |
| 34 | +""" |
| 35 | +sql_create2 = """ |
| 36 | + CREATE TABLE t_region ( |
| 37 | + id BIGINT PRIMARY KEY AUTO_INCREMENT COMMENT '主键ID', |
| 38 | + region_code VARCHAR(16) COMMENT '邮编', |
| 39 | + regian_name VARCHAR(20) COMMENT '区域名', |
| 40 | + note VARCHAR(120) COMMENT '备注', |
| 41 | + parent_id BIGINT COMMENT '父级ID', |
| 42 | + created_time DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间', |
| 43 | + updated_time DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间' |
| 44 | + ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COMMENT '区域表'; |
| 45 | +""" |
| 46 | +sql_insert_enterprise = """ |
| 47 | + INSERT INTO t_enterprise |
| 48 | + (id,name,tax_code,region_id,customer_type,enterprise_type) |
| 49 | + VALUES (%s, %s, %s, %s, %s, %s); |
| 50 | +""" |
| 51 | +sql_update_enterprise = """ |
| 52 | + UPDATE t_enterprise |
| 53 | + SET |
| 54 | + address=%s, |
| 55 | + postcode=%s, |
| 56 | + tel=%s, |
| 57 | + contact=%s, |
| 58 | + fax=%s, |
| 59 | + mobile=%s |
| 60 | + WHERE id=%s |
| 61 | +""" |
| 62 | +sql_insert_region = """ |
| 63 | + INSERT INTO t_region |
| 64 | + (id,region_code,regian_name,note,parent_id) |
| 65 | + VALUES (%s, %s, %s, %s, %s); |
| 66 | +""" |
| 67 | +logging.basicConfig(level=logging.INFO, |
| 68 | + format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s', |
| 69 | + datefmt='%Y-%m-%d %H:%M:%S') |
| 70 | +_log = logging.getLogger('app.' + __name__) |
| 71 | + |
| 72 | + |
| 73 | +def _log_info(msgpre): |
| 74 | + _log.info('{}---{}'.format(msgpre, datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))) |
| 75 | + |
| 76 | + |
| 77 | +def _connect(): |
| 78 | + config = { |
| 79 | + 'user': 'root', |
| 80 | + 'password': 'mysql', |
| 81 | + 'host': '192.168.203.95', |
| 82 | + 'database': 'hangxin', |
| 83 | + 'raise_on_warnings': True, |
| 84 | + } |
| 85 | + cnx = None |
| 86 | + try: |
| 87 | + cnx = mysql.connector.connect(**config) |
| 88 | + except mysql.connector.Error as err: |
| 89 | + if err.errno == errorcode.ER_ACCESS_DENIED_ERROR: |
| 90 | + print("Something is wrong with your user name or password") |
| 91 | + elif err.errno == errorcode.ER_BAD_DB_ERROR: |
| 92 | + print("Database does not exist") |
| 93 | + else: |
| 94 | + print(err) |
| 95 | + if cnx: |
| 96 | + cnx.close() |
| 97 | + return cnx |
| 98 | + |
| 99 | + |
| 100 | +def _init_table(): |
| 101 | + conn_ = _connect() |
| 102 | + cursor = conn_.cursor() |
| 103 | + cursor.execute(sql_create1) |
| 104 | + cursor.execute(sql_create2) |
| 105 | + cursor.close() |
| 106 | + conn_.commit() |
| 107 | + conn_.close() |
| 108 | + |
| 109 | + |
| 110 | +def parse_sheet(wb, sheet_name, column_num, log_msg): |
| 111 | + ws = wb[sheet_name] # ws is now an IterableWorksheet |
| 112 | + result_list = [] |
| 113 | + for row in ws.rows: |
| 114 | + row_data = [] |
| 115 | + for i, cell in enumerate(row): |
| 116 | + if i >= column_num: |
| 117 | + break |
| 118 | + row_data.append(cell.value) |
| 119 | + result_list.append(row_data[:]) |
| 120 | + _log_info(log_msg) |
| 121 | + return result_list |
| 122 | + |
| 123 | + |
| 124 | +def xlsx_to_table(xlsx_name): |
| 125 | + conn_ = _connect() |
| 126 | + _log_info('Excel文件解析start') |
| 127 | + wb = load_workbook(xlsx_name, read_only=True) |
| 128 | + # 先收集企业资料表 |
| 129 | + list1 = parse_sheet(wb, 'customer', 6, 'customer表解析end') |
| 130 | + data1 = [(v[0], v[1], v[2], v[3], v[4], v[5]) for v in list1[1:]] |
| 131 | + # 收集地址和联系人表 |
| 132 | + list2 = parse_sheet(wb, 'addr', 10, 'addr表解析end') |
| 133 | + data2 = [(v[2], v[4], v[5], v[6], v[7], v[8], v[0]) for v in list2[1:]] |
| 134 | + # 收集区域表 |
| 135 | + list3 = parse_sheet(wb, 'region', 5, 'region表解析end') |
| 136 | + data3 = [(v[0], v[1], v[2], v[3], v[4]) for v in list3[1:]] |
| 137 | + _log_info('Excel文件解析end') |
| 138 | + |
| 139 | + cursor = conn_.cursor() |
| 140 | + |
| 141 | + _log_info('插入企业资料start') |
| 142 | + for i, d1 in enumerate(data1): |
| 143 | + cursor.execute(sql_insert_enterprise, d1) |
| 144 | + if i % 50 == 0: |
| 145 | + conn_.commit() |
| 146 | + conn_.commit() |
| 147 | + _log_info('插入企业资料end') |
| 148 | + |
| 149 | + _log_info('更新企业联系信息start') |
| 150 | + for i, d2 in enumerate(data2): |
| 151 | + cursor.execute(sql_update_enterprise, d2) |
| 152 | + if i % 50 == 0: |
| 153 | + conn_.commit() |
| 154 | + conn_.commit() |
| 155 | + _log_info('插入企业资料表end') |
| 156 | + |
| 157 | + _log_info('插入区域信息start') |
| 158 | + for i, d3 in enumerate(data3): |
| 159 | + cursor.execute(sql_insert_region, d3) |
| 160 | + if i % 50 == 0: |
| 161 | + conn_.commit() |
| 162 | + conn_.commit() |
| 163 | + _log_info('插入区域信息end') |
| 164 | + |
| 165 | + _log_info('数据库更新end') |
| 166 | + |
| 167 | + cursor.close() |
| 168 | + conn_.close() |
| 169 | + |
| 170 | + |
| 171 | +if __name__ == '__main__': |
| 172 | + excel = r'D:\download\20150505\gdc.xlsx' |
| 173 | + _init_table() |
| 174 | + conn = _connect() |
| 175 | + xlsx_to_table(excel) |
| 176 | + pass |
0 commit comments