基于Python第三方插件實(shí)現(xiàn)西游記章節(jié)標(biāo)注漢語(yǔ)拼音的方法
起因很單純,就是給我1年級(jí)小豆包的女兒標(biāo)注三國(guó)和西游章節(jié)的漢語(yǔ)拼音,我女兒每天都朗讀 ,結(jié)果有很多字不認(rèn)識(shí),我愛(ài)人居然讓我給標(biāo)記不認(rèn)識(shí)的完了手動(dòng)注音......我勒個(gè)去......身為程序員的我怎么能忘記用程序?qū)崿F(xiàn)呢,特別是咱也會(huì)點(diǎn)Python萬(wàn)能語(yǔ)言。哈哈!列舉一下使用的技術(shù)。
語(yǔ)言:Python3.7
插件:pypinyin0.37.0 和 openpyxl 3.0.3
開(kāi)發(fā)工具:pycharm 社區(qū)版
使用openpyxl操作execl的教程多的你無(wú)法想。
使用pypinyin將漢字轉(zhuǎn)換成漢語(yǔ)拼音很簡(jiǎn)單,網(wǎng)絡(luò)上API一大推。而且簡(jiǎn)單的不能再簡(jiǎn)單了,就一句話(huà)就實(shí)現(xiàn)了。分享點(diǎn)代碼:
# 帶聲調(diào)的(默認(rèn)) def yinjie(word): sentens = "".join(word.split()) print(sentens) s = '' # heteronym=True開(kāi)啟多音字 for i in pypinyin.pinyin(word, heteronym=False): s = s + ''.join(i) + " " return s
這個(gè)就足夠漢字轉(zhuǎn)拼音了,不過(guò)我要求數(shù)據(jù)結(jié)構(gòu)就沒(méi)使用這個(gè)方法。我把數(shù)據(jù)結(jié)構(gòu)說(shuō)一下。
三層二維數(shù)組(這個(gè)非常關(guān)鍵):
第一層:?jiǎn)蝹€(gè)漢字和漢語(yǔ)拼音構(gòu)成。
['dì', '第'], ['yī', '一'], ['bǎi', '百'], ['huí', '回']
第二層:按照標(biāo)題空格分詞。
[['dì', '第'], ['yī', '一'], ['bǎi', '百'], ['huí', '回']], [['jìng', '徑'], ['huí', '回'], ['dōng', '東'], ['tǔ', '土']], [['wǔ', '五'], ['shèng', '圣'], ['chéng', '成'], ['zhēn', '真']]
第三層:所有標(biāo)題的集合。
就是第二層的合計(jì),西游記就是100個(gè)章節(jié)標(biāo)題集合。
最開(kāi)始的成果物。這個(gè)不好對(duì)應(yīng)也很難閱讀。
我愛(ài)人給了我一個(gè)參考事例。如下圖:
咱也不能示弱,咱也是程序員。咱也會(huì)萬(wàn)能的Python。
最開(kāi)始的目標(biāo)是將文字寫(xiě)入到word中,所以就用了Python-docx。漢語(yǔ)拼音長(zhǎng)短不一這個(gè)很難對(duì)齊。想計(jì)算漢語(yǔ)拼音的長(zhǎng)度進(jìn)而計(jì)算漢字的位置......這個(gè)算法得多復(fù)雜,一個(gè)排版算法...我不是大神......
這個(gè)玩意其實(shí)和數(shù)學(xué)應(yīng)用題一樣,想到了其實(shí)一點(diǎn)也不難,就是弄個(gè)表格完了讓漢語(yǔ)拼音和漢字居中不就得了。想到這個(gè)以后其實(shí)一點(diǎn)都不難了。使用Python-docx搞了好久有個(gè)問(wèn)題就是豎版的word放不下漢字和漢語(yǔ)拼音。頭疼啊。效果如下圖:
唉!難道是思路不對(duì)。。。
不用Python-docx了。使用openpyxl來(lái)操作execl。第一次成果物。看起來(lái)還可以。
最終的成果物。
轉(zhuǎn)成PDF的成果物:
繼續(xù)鼓搗,最終搞定。。。一共花費(fèi)了近6個(gè)小時(shí),時(shí)間有點(diǎn)多。其實(shí)主要是數(shù)據(jù)結(jié)構(gòu)和排版耽誤時(shí)間,在有就是語(yǔ)法,作為Net出身而后轉(zhuǎn)Java的人來(lái)說(shuō)這個(gè)…&...就是并且的意思。然而在Python里面他不就是并且是and啊啊啊啊?。∫?yàn)檫@個(gè)我改了N多代碼調(diào)試了好幾次,唉深度無(wú)語(yǔ)。真是基礎(chǔ)不牢地動(dòng)山搖啊。別廢話(huà)了上代碼:
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') # 返回一個(gè)文件對(duì)象 line = f.readline().strip() # 調(diào)用文件的 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): """ 構(gòu)建execl文件 :param word_title: sheet頁(yè)面的名詞 :param save_path: execl保存路徑 :param article: 文章內(nèi)容集合 :return: """ wb = Workbook() ws = wb.active ws.title = word_title ws.sheet_properties.tabColor = "1072BA" # 設(shè)置背景 xl_sheet = wb.get_sheet_by_name(word_title) execl_cell_width = 4.6 for sentences in article: column_index = 1 # sentences 2行數(shù)據(jù) # 獲取行數(shù) 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 設(shè)置樣式 # 設(shè)置樣式 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 # 三個(gè)短語(yǔ)計(jì)數(shù)器 wb.save(save_path)
總結(jié)
到此這篇關(guān)于基于Python第三方插件實(shí)現(xiàn)西游記章節(jié)標(biāo)注漢語(yǔ)拼音的方法的文章就介紹到這了,更多相關(guān)python第三方插件標(biāo)拼音內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Python中Pyenv virtualenv插件的使用
- python搶購(gòu)軟件/插件/腳本附完整源碼
- Python常用擴(kuò)展插件使用教程解析
- Python使用Chrome插件實(shí)現(xiàn)爬蟲(chóng)過(guò)程圖解
- Python插件機(jī)制實(shí)現(xiàn)詳解
- 詳解PyCharm安裝MicroPython插件的教程
- Python實(shí)現(xiàn)E-Mail收集插件實(shí)例教程
- Python實(shí)現(xiàn)SQL注入檢測(cè)插件實(shí)例代碼
- Python 帶你快速上手 Apache APISIX 插件開(kāi)發(fā)
相關(guān)文章
python 實(shí)現(xiàn)網(wǎng)上商城,轉(zhuǎn)賬,存取款等功能的信用卡系統(tǒng)
本篇文章主要介紹 基于python 實(shí)現(xiàn)信用卡系統(tǒng),附有代碼實(shí)例,對(duì)于用python 開(kāi)發(fā)網(wǎng)絡(luò)上傳系統(tǒng)具有參考價(jià)值,有需要的朋友可以看下2016-07-07python matplotlib畫(huà)圖實(shí)例代碼分享
這篇文章主要介紹了python matplotlib畫(huà)圖實(shí)例代碼分享,具有一定借鑒價(jià)值,需要的朋友可以參考下2017-12-12Python采集C站熱榜數(shù)據(jù)實(shí)戰(zhàn)示例
這篇文章主要為大家介紹了Python采集C站熱榜數(shù)據(jù)實(shí)戰(zhàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05Python webdriver.Chrome()的使用解讀
這篇文章主要介紹了Python webdriver.Chrome()的使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02Python Requests模擬登錄實(shí)現(xiàn)圖書(shū)館座位自動(dòng)預(yù)約
這篇文章主要為大家詳細(xì)介紹了Python Requests的模擬登錄,Python實(shí)現(xiàn)圖書(shū)館座位自動(dòng)預(yù)約,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-04-04