码迷,mamicode.com
首页 > 编程语言 > 详细

python excel 繁体转换

时间:2018-05-30 19:24:30      阅读:345      评论:0      收藏:0      [点我收藏+]

标签:raw   dex   mdt   下载   转换   ssi   fir   pre   繁体   

#下载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

  

原文: 传送门

 

python excel 繁体转换

标签:raw   dex   mdt   下载   转换   ssi   fir   pre   繁体   

原文地址:https://www.cnblogs.com/412013cl/p/9112467.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!