python excel 繁体转换

1
2
3
#下载2个文件
https://raw.githubusercontent.com/skydark/nstools/master/zhtools/langconv.py
https://raw.githubusercontent.com/skydark/nstools/master/zhtools/zh_wiki.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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

  

原文: 传送门

 

posted @   qukaige  阅读(600)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
点击右上角即可分享
微信分享提示