#下载2个文件 https://raw.githubusercontent.com/skydark/nstools/master/zhtools/langconv.py https://raw.githubusercontent.com/skydark/nstools/master/zhtools/zh_wiki.py
import xlrd
from sqlalchemy import create_engine, MetaData
from sqlalchemy.ext.automap import automap_base
from sqlalchemy.orm import Session
from langconv import *
def readExcel(path):
workBook = xlrd.open_workbook(path)
names = workBook.sheet_names()
print(‘excel的所有表名‘, names)
# 根据索引获取当前表
firstSheet = workBook.sheet_by_index(0) # 获取第一张表
print(‘当前的sheet名称: %s‘ % firstSheet.name)
print(‘当前表的总行数: %s‘ % firstSheet.nrows)
print(‘当前表占用的列数: %s‘ % firstSheet.ncols)
# 获取第一行的value
# firstRow = firstSheet.row_values(0)
# print(‘表第一行(列名称): %s‘ % firstRow)
for num in range(firstSheet.nrows):
if num > 0:
rows = firstSheet.row_values(num)
for index, value in enumerate(rows):
if index == 1:
if str(value) and len(str(value)) == 8:
yield str(value), rows[2]
continue
ret = readExcel("C:/Users/kaige/Desktop/四元玉鉴/新商品备案-税则号/照陸方稅號(2018對照表 配合臺方稅則調整).xls")
# 繁体转简体
def Traditional2Simplified(sentence):
‘‘‘
将sentence中的繁体字转为简体字
‘‘‘
sentence = Converter(‘zh-hans‘).convert(sentence)
return sentence
# 数据连接引擎
engine = create_engine(‘mssql+pymssql://username:password/dbname?charset=utf8‘, echo=False)
# orm 映射实体类
metadata_r = MetaData()
metadata_r.reflect(engine, only=[‘Base_CustTax_copy1_2‘])
Base_r = automap_base(metadata=metadata_r)
Base_r.prepare()
Base_CustTax_copy1_2 = Base_r.classes.Base_CustTax_copy1_2
# 获取session实例
session = Session(engine)
for code, name in ret:
# 添加对象
base = Base_CustTax_copy1_2(Cust_Code=code,Cmdt_Desc=Traditional2Simplified(name))
session.add(base)
# 提交
session.commit()
# 繁体转简体
def Simplified2Traditional(sentence):
‘‘‘
将sentence中的简体字转为繁体字
‘‘‘
sentence = Converter(‘zh-hant‘).convert(sentence)
return sentence
原文:https://www.cnblogs.com/412013cl/p/9112467.html