基于Python第三方插件实现西游记章节标注汉语拼音的方法


Posted in Python onMay 22, 2020

      起因很单纯,就是给我1年级小豆包的女儿标注三国和西游章节的汉语拼音,我女儿每天都朗读 ,结果有很多字不认识,我爱人居然让我给标记不认识的完了手动注音......我勒个去......身为程序员的我怎么能忘记用程序实现呢,特别是咱也会点Python万能语言。哈哈!列举一下使用的技术。

语言:Python3.7

插件:pypinyin0.37.0  和 openpyxl 3.0.3

开发工具:pycharm 社区版

使用openpyxl操作execl的教程多的你无法想。

使用pypinyin将汉字转换成汉语拼音很简单,网络上API一大推。而且简单的不能再简单了,就一句话就实现了。分享点代码:

# 带声调的(默认)
def yinjie(word):
  sentens = "".join(word.split())
  print(sentens)
  s = ''
  # heteronym=True开启多音字
  for i in pypinyin.pinyin(word, heteronym=False):
    s = s + ''.join(i) + " "
  return s

这个就足够汉字转拼音了,不过我要求数据结构就没使用这个方法。我把数据结构说一下。

三层二维数组(这个非常关键):

第一层:单个汉字和汉语拼音构成。

['dì', '第'], ['yī', '一'], ['bǎi', '百'], ['huí', '回']

第二层:按照标题空格分词。

 [['dì', '第'], ['yī', '一'], ['bǎi', '百'], ['huí', '回']], [['jìng', '径'], ['huí', '回'], ['dōng', '东'], ['tǔ', '土']], [['wǔ', '五'], ['shèng', '圣'], ['chéng', '成'], ['zhēn', '真']]

第三层:所有标题的集合。

就是第二层的合计,西游记就是100个章节标题集合。

最开始的成果物。这个不好对应也很难阅读。

基于Python第三方插件实现西游记章节标注汉语拼音的方法

我爱人给了我一个参考事例。如下图:

基于Python第三方插件实现西游记章节标注汉语拼音的方法

咱也不能示弱,咱也是程序员。咱也会万能的Python。

最开始的目标是将文字写入到word中,所以就用了Python-docx。汉语拼音长短不一这个很难对齐。想计算汉语拼音的长度进而计算汉字的位置......这个算法得多复杂,一个排版算法...我不是大神......

这个玩意其实和数学应用题一样,想到了其实一点也不难,就是弄个表格完了让汉语拼音和汉字居中不就得了。想到这个以后其实一点都不难了。使用Python-docx搞了好久有个问题就是竖版的word放不下汉字和汉语拼音。头疼啊。效果如下图:

基于Python第三方插件实现西游记章节标注汉语拼音的方法

唉!难道是思路不对。。。

不用Python-docx了。使用openpyxl来操作execl。第一次成果物。看起来还可以。

基于Python第三方插件实现西游记章节标注汉语拼音的方法

最终的成果物。

基于Python第三方插件实现西游记章节标注汉语拼音的方法

转成PDF的成果物:

基于Python第三方插件实现西游记章节标注汉语拼音的方法

继续鼓捣,最终搞定。。。一共花费了近6个小时,时间有点多。其实主要是数据结构和排版耽误时间,在有就是语法,作为Net出身而后转Java的人来说这个…&...就是并且的意思。然而在Python里面他不就是并且是and啊啊啊啊啊!因为这个我改了N多代码调试了好几次,唉深度无语。真是基础不牢地动山摇啊。别废话了上代码:

import pypinyin
from openpyxl import Workbook
from openpyxl.drawing.text import Font
from openpyxl.styles import Font, colors, Alignment
from pulgin.Tools import Tools
class HanZhiAddPinYin:
  def __init__(self):
    pass
  def signWord(self,word):
    pinyicontent = pypinyin.pinyin(word, heteronym=False)
    word_pinyin = [pinyicontent[0][0], word]
    return word_pinyin
  def sentences(self,keyWords):
    listsentense = []
    for duanyu in keyWords.split():
      print(duanyu)
      duanyu_list = []
      for word in duanyu:
        duanyu_list.append(self.signWord(word))
      listsentense.append(duanyu_list)
    print(
      listsentense
    )
    return listsentense
  def articles(self,txt_file_path):
    article = []
    encoding = Tools.get_file_encoding(txt_file_path)
    f = open(txt_file_path, "r", encoding=encoding, errors='ignore') # 返回一个文件对象
    line = f.readline().strip() # 调用文件的 readline()方法
    index = 1
    while line:
      article.append(self.sentences(line))
      line = f.readline()
      index = index + 1
    f.close()
    return article
  def builder_execl(self,word_title, save_path, article):
    """
    构建execl文件
    :param word_title: sheet页面的名词
    :param save_path: execl保存路径
    :param article: 文章内容集合
    :return:
    """
    wb = Workbook()
    ws = wb.active
    ws.title = word_title
    ws.sheet_properties.tabColor = "1072BA" # 设置背景
    xl_sheet = wb.get_sheet_by_name(word_title)
    execl_cell_width = 4.6
    for sentences in article:
      column_index = 1
      # sentences 2行数据
      # 获取行数
      pinyin_row = xl_sheet.max_row + 1 # 拼音所在的行
      hanzi_row = pinyin_row + 1 # 汉字所在的行
      sentences_index = 0
      for duanyu in sentences: # ['dì', '第'], ['yī', '一'], ['huí', '回']
        for sign_word in duanyu: # ['dì', '第']
          # region 设置样式
          # 设置样式
          execl_cell_font = Font(name='华文楷体', size=12, italic=False, color=colors.BLACK, bold=True)
          execl_pinyin_row = xl_sheet.cell(row=pinyin_row, column=column_index)
          execl_hanzi_row = xl_sheet.cell(row=hanzi_row, column=column_index)
          execl_pinyin_row.alignment = Alignment(horizontal='center', vertical='center')
          execl_hanzi_row.alignment = Alignment(horizontal='center', vertical='center')
          execl_pinyin_row.font = execl_cell_font
          execl_hanzi_row.font = execl_cell_font
          xl_sheet.column_dimensions['A'].width = 3
          xl_sheet.column_dimensions['B'].width = 3
          xl_sheet.column_dimensions['C'].width = 3
          xl_sheet.column_dimensions['D'].width = execl_cell_width
          xl_sheet.column_dimensions['E'].width = execl_cell_width
          xl_sheet.column_dimensions['F'].width = execl_cell_width
          xl_sheet.column_dimensions['H'].width = execl_cell_width
          xl_sheet.column_dimensions['I'].width = execl_cell_width
          xl_sheet.column_dimensions['G'].width = execl_cell_width
          xl_sheet.column_dimensions['J'].width = execl_cell_width
          xl_sheet.column_dimensions['K'].width = execl_cell_width
          xl_sheet.column_dimensions['L'].width = execl_cell_width
          xl_sheet.column_dimensions['M'].width = execl_cell_width
          xl_sheet.column_dimensions['N'].width = execl_cell_width
          xl_sheet.column_dimensions['O'].width = execl_cell_width
          xl_sheet.column_dimensions['P'].width = execl_cell_width
          xl_sheet.column_dimensions['Q'].width = execl_cell_width
          xl_sheet.column_dimensions['R'].width = execl_cell_width
          xl_sheet.column_dimensions['S'].width = execl_cell_width
          xl_sheet.column_dimensions['T'].width = execl_cell_width
          xl_sheet.column_dimensions['U'].width = execl_cell_width
          xl_sheet.column_dimensions['V'].width = execl_cell_width
          xl_sheet.column_dimensions['W'].width = execl_cell_width
          # endregion
          xl_sheet.cell(row=pinyin_row, column=column_index, value=sign_word[0])
          xl_sheet.cell(row=hanzi_row, column=column_index, value=sign_word[1]) # 0 第一百回 1 径回东土 2 五圣成真
          # print(sentences_index)
          # print(len(duanyu) + len(sentences[0]))
          # print(column_index)
          if sentences_index == 1 and len(duanyu) + len(sentences[0]) == column_index:#坑人的and
            xl_sheet.cell(row=pinyin_row, column=column_index + 1, value=",")
            xl_sheet.cell(row=hanzi_row, column=column_index + 1, value=",")
            column_index = column_index + 1 # 遇到断句多增加一列向后
          column_index = column_index + 1 # 列向后
        sentences_index = sentences_index + 1 # 三个短语计数器
    wb.save(save_path)

总结

到此这篇关于基于Python第三方插件实现西游记章节标注汉语拼音的方法的文章就介绍到这了,更多相关python第三方插件标拼音内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
Python 第一步 hello world
Sep 25 Python
python操作日期和时间的方法
Mar 11 Python
Python中实现对list做减法操作介绍
Jan 09 Python
Python抽象类的新写法
Jun 18 Python
Python自动登录126邮箱的方法
Jul 10 Python
Python中用psycopg2模块操作PostgreSQL方法
Nov 28 Python
python爬取网页内容转换为PDF文件
Jul 28 Python
解决python3运行selenium下HTMLTestRunner报错的问题
Dec 27 Python
Python二叉树的镜像转换实现方法示例
Mar 06 Python
Python django框架输入汉字,数字,字符生成二维码实现详解
Sep 24 Python
简单了解Python字典copy与赋值的区别
Sep 16 Python
用sleep间隔进行python反爬虫的实例讲解
Nov 30 Python
Tensorflow tf.tile()的用法实例分析
May 22 #Python
python代码实现将列表中重复元素之间的内容全部滤除
May 22 #Python
Tensorflow实现将标签变为one-hot形式
May 22 #Python
Python selenium爬取微博数据代码实例
May 22 #Python
python实现文法左递归的消除方法
May 22 #Python
使用Django搭建网站实现商品分页功能
May 22 #Python
Tensorflow卷积实现原理+手写python代码实现卷积教程
May 22 #Python
You might like
php将会员数据导入到ucenter的代码
2010/07/18 PHP
破解.net程序(dll文件)编译和反编译方法
2013/01/31 PHP
PHP中一些可以替代正则表达式函数的字符串操作函数
2014/11/17 PHP
PHP常用工具类大全附全部代码下载
2015/12/07 PHP
thinkPHP中配置的读取与C方法详解
2016/12/05 PHP
一个实用的php验证码类
2017/07/06 PHP
php设计模式之策略模式应用案例详解
2019/06/17 PHP
调试Node.JS的辅助工具(NodeWatcher)
2012/01/04 Javascript
教你使用javascript简单写一个页面模板引擎
2015/05/05 Javascript
全面解析Bootstrap表单使用方法(表单按钮)
2015/11/24 Javascript
flag和jq on 的绑定多个对象和方法(必看)
2017/02/27 Javascript
Angular.JS中的指令引用template与指令当做属性详解
2017/03/30 Javascript
vue调用高德地图实例代码
2017/04/28 Javascript
微信小程序实现订单倒计时
2020/11/01 Javascript
vue + axios get下载文件功能
2019/09/25 Javascript
如何利用JavaScript编写更好的条件语句详解
2020/08/10 Javascript
js代码编写无缝轮播图
2020/09/13 Javascript
微信小程序调用后台service教程详解
2020/11/06 Javascript
[01:14]英雄,所敬略同——2018完美盛典宣传视频4K
2018/12/05 DOTA
使用Python脚本生成随机IP的简单方法
2015/07/30 Python
Python中函数参数匹配模型详解
2019/06/09 Python
解决python 文本过滤和清理问题
2019/08/28 Python
python 调试冷知识(小结)
2019/11/11 Python
Python动态导入模块和反射机制详解
2020/02/18 Python
Python如何测试stdout输出
2020/08/10 Python
美国渔具店:FishUSA
2019/08/07 全球购物
招商业务员岗位职责
2013/12/16 职场文书
施工资料员岗位职责
2014/01/06 职场文书
质检部经理岗位职责
2014/02/19 职场文书
三八妇女节活动主持词
2014/03/17 职场文书
婚前协议书范本
2014/04/15 职场文书
竞选副班长演讲稿
2014/04/24 职场文书
2015年普法依法治理工作总结
2015/05/26 职场文书
军训后的感想
2015/08/07 职场文书
教师教育心得体会
2016/01/19 职场文书
2016保送生自荐信范文
2016/01/29 职场文书