Excel汉字转拼音无声调

python汉字转拼音&创建Excel

python 时间格式化

    print(str(datetime.datetime.now().strftime("%Y-%m-%d %X")))
    print(str(datetime.datetime.now().strftime("%Y-%m-%d %H%M%S")))

List和元组添加元素

listDa=[]
listDa.append(a) #将a 添加到List集合里
dict={}
dict.update(dict2) # 将 dict2 的内容添加到 dict 中

创建一个Excel 并往sheet里写入文件

def createExcel(index):
    mywb = openpyxl.Workbook()
    title = 'my_second_sheet' + str(index)
    # index 表示sheet页,从0开始,title 表示sheet页名称
    # 创建时,默认会有一个Sheet页
    mywb.create_sheet(index=index, title=title)
    # logger.info(mywb.get_sheet_names())
    mysheet = mywb[title]
    mysheet['F6'] = 'Writing new Value!'
    mywb.save('./source_file/' + str(datetime.datetime.now().strftime("%Y-%m-%d-%H%M%S")) + '.xlsx')

汉字转拼音


# 将汉字转为拼音
import openpyxl
import pypinyin
from loguru import logger

def pinyin(word):
    s = ''
    # 不带声调的 style=pypinyin.NORMAL
    for i in pypinyin.pinyin(word, style=pypinyin.NORMAL):
        s += ''.join(i)
    return s


# 保存Excel
def saveExcel(code, num):
    data = openpyxl.load_workbook("./source_file/123.xlsx")  # 打开电影.xlsx文件读取数据
    table = data["Sheet1"]
    table["A" + str(num)] = code
    data.close()
    data.save("./source_file/123.xlsx")


if __name__ == '__main__':
    workbook = openpyxl.load_workbook("./source_file/123.xlsx")
    # 获取当前活动的sheet页对象
    sheet = workbook.active
    # 获取指定的sheet页对象
    sheet2 = workbook["Sheet1"]
    num = 1
    while True:
        num += 1
        identifier = sheet2._get_cell(num, 2).value  # 标识、code
        if identifier is None:
            break
        code = pinyin(identifier)
        saveExcel(code, num)
    logger.info('拼音转换结束===================')

惜秦皇汉武,略输文采;唐宗宋祖,稍逊风骚。 一代天骄,成吉思汗,只识弯弓射大雕。 俱往矣,数风流人物,还看今朝

posted @ 2021-10-22 23:30  darling331  阅读(96)  评论(1)  编辑  收藏  举报

EXCEL汉字转拼音 WORD转换法及用VBA宏自建函数

发布者: 匿名 2017年11月20日 围观次 点赞:3

将中文姓名直接转为中文拼音,这里有两个方法提供,一个是用WORD来做中转转换,一个是用VBA宏来转换


一、WORD法

Excel汉字转拼音无声调


二、VBA宏


Function PinYin(TXT As Variant, Delimiter As String, Tpy As Byte, Optional FirU As Boolean = False) As String
    'Delimiter,隔离符号;
    'Tpy定义返回格式:
        '1,带用数字表示的声调
        '2,无声调
        '3,仅首字母
        '4,带传统声调
    'FirU,各单词首字母是否大写,默认小写
        
    Dim N As Integer
    Dim ASCID As Long
    Dim Y As Byte
    Dim M_Txt As String
    Dim M_PY As String
    Dim MI_PY As String
    
    On Error Resume Next
    
    TXT = Trim(TXT)
    If TXT = "" Then
        PinYin = ""
        Exit Function
    End If
    
    If PY_DB(72, 94) <> "ā/á/ǎ/à" And Tpy = 4 Then
        Call DealVal_2
    ElseIf PY_DB(72, 94) <> "a1" And Tpy < 4 Then
        Call DealVal_1
    End If
    
    For i = 1 To Len(Trim(TXT))
        M_Txt = Mid(Trim(TXT), i, 1)
        If M_Txt = "" Then
            MI_PY = ""
        Else
            ASCID = Asc(M_Txt)
            For N = 1 To UBound(PY_Index)
                If PY_Index(N) < ASCID Then Exit For
            Next N
        
            PYDB_Index = PY_Index(N - 1) - ASCID
        
            If PYDB_Index < 0 Or PYDB_Index > 93 Then
                M_PY = M_Txt
                Y = 1
            Else
                M_PY = PY_DB(N - 1, PYDB_Index + 1)
            End If
        End If
    
        Select Case Tpy
            Case 1
                MI_PY = M_PY
            Case 2
                MI_PY = IIf(M_PY = M_Txt, M_PY, Mid(M_PY, 1, Len(M_PY) - 1))
            Case 3
                MI_PY = Left(M_PY, 1)
            Case 4
                MI_PY = M_PY
        End Select
     
        PinYin = PinYin & IIf(M_PY = M_Txt, MI_PY, IIf(Y = 1, Delimiter & MI_PY & Delimiter, IIf(i = Len(Trim(TXT)), MI_PY, MI_PY & Delimiter)))
        Y = IIf(Y = 1, IIf(M_PY = M_Txt, 1, 0), 0)
    Next i
    If FirU Then PinYin = Application.WorksheetFunction.Proper(PinYin)
End Function




函数的声明:Function PinYin(TXT As Variant, Delimiter As String, Tpy As Byte) As String,

这里第三个参数 Tpy是定义返回格式,主要有以下几种:

1,带用数字表示的声调

2,无声调

3,首字母

4,首字母并大写

5,带传统声调


假设 TXT="my name is: 中国 1."

用 PinYin(TXT," ",5)调用,将得到:my name is:  zhōng/zhòng guó  1.

用 PinYin(TXT," ",4,1)调用,将得到:My Name Is:  Zhōng/Zhòng Guó  1.


顶(3)

踩(1)

EXCEL 转换 vba 宏 汉字 拼音 【搜索相关内容】[打印] [关闭]

上一篇:MAX/MIN+IF数组求取多重条件最大值最小值

下一篇:EXCEL使用VBA SQL读取ACCESS数据库内容

您可能还会对下面的文章感兴趣:

相关文章

  • 1 Excel工作表保护密码忘记的解决办法 工作表保护密码破解方法
  • 2 EXCEL多种舍入函数的应用
  • 3 VBA宏合并当前工作簿下的所有工作表
  • 4 多工作簿合并成一个工作簿 多个EXCEL文件合并成一个EXCEL
  • 5 EXCEL中以万元为单位显示金额 单元格格式设置方法
  • 6 EXCEL制作到期提醒

最新评论