#下载2个文件https://raw.githubusercontent.com/skydark/nstools/master/zhtools/langconv.py https:// raw.githubusercontent.com/skydark/nstools/master/zhtools/zh_wiki.py
Import xlrdfrom sqlalchemy import create_engine, metadatafrom sqlalchemy.ext.automap import Automap_basefrom Sqlalchemy.orm Import sessionfrom langconv import *def readexcel (path): WorkBook = xlrd.open_workbook (path) names = Workbook.sheet_names () print (' All table name of Excel ', names) # Get current table by index Firstsheet = workbook.sheet_by_index (0) # Get first table Print (' Current sheet name:%s '% firstsheet.name) print (' Total rows of the current table:%s '% firstsheet.nrows) print (' Number of columns occupied by current table:%s '% Firstshee T.ncols) # Gets the first row of value # firstrow = firstsheet.row_values (0) # print (' Table first row (column name):%s '% firstrow) for num in RA Nge (firstsheet.nrows): if num > 0:rows = firstsheet.row_values (num) for index, value in E Numerate (rows): If index = = 1:if str (value) and Len (str (value)) = = 8: Yield str (value), rows[2] Continueret = Readexcel ("c:/users/kaige/desktop/$ four Yu Yu/new product record-tariff code/mainland tax (2018 to adjust the table with Taiwan tax). xls ") # Traditional to simplified def TRaditional2simplified (sentence): "The traditional characters in sentence to simplified" "sentence = Converter (' Zh-hans '). Convert (sentence) Return sentence# data connection engine = Create_engine (' Mssql+pymssql://username:password/dbname?charset=utf8 ', Echo=False) # ORM Mapping entity class 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# Get session Instance session = Session (engine) for code, name in RET: # Add Object base = Base_custtax_copy1_2 (Cust_code=code,cmdt_de Sc=traditional2simplified (name)) Session.add (base) # Submit Session.commit () # Traditional to Simplified def simplified2traditional (sentence): "Convert the simplified characters in sentence to traditional characters" sentence = Converter (' zh-hant '). Convert (sentence) return sentence
Original: Portal
Python Excel traditional Conversion