# WordDocumentPinYinByPython **Repository Path**: zaiecunmin/word-document-pin-yin-by-python ## Basic Information - **Project Name**: WordDocumentPinYinByPython - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 0 - **Created**: 2025-11-01 - **Last Updated**: 2026-01-25 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README @[TOC](Word文档注音) # 项目地址 [GitCode:WordDocumentPinYinByPython](https://gitcode.com/m0_70451198/WordDocumentPinYinByPython) [Gitee:WordDocumentPinYinByPython](https://gitee.com/zaiecunmin/word-document-pin-yin-by-python) # 项目预期 ## 项目目的 WPS 拼音指南30字以上收费[^1] Microsoft Word 拼音指南只支持30字[^1] 希望实现用Python实现Word文档完整的注音 以下是在WPS中的预览效果[^2] [^1]: 记录于2025/11/1,后可能变更 [^2]:MicrodsoftWord的实现效果![MW](https://i-blog.csdnimg.cn/direct/df5b7a4279a547a589ea8bb62ff632b3.png) ## 目标文件(png) ![目标文件](https://i-blog.csdnimg.cn/direct/3f8862ebf69a4e1f936829a62d052f01.png) ## 源文件(png) ![源文件](https://i-blog.csdnimg.cn/direct/c17310d425ec42f7a48d0c99dabfb7e0.png) # 实现路径 ## 仿照目标 WPS 拼音指南实现案例(固)[^3](仿照目标) ```xml EQ \* jc0 \* "Font:汉仪润圆-75W" \* hps14 \o \ad(\s \up 13(ɡù),固) ``` MicrosoftWord 拼音指南实现案例(秦) ```xml EQ \* jc2 \* "Font:汉仪润圆-75W" \* hps14 \o\ad(\s\up 13( qín ),秦) ``` [^3]:Word文档取出XML的方法:另存为.zip文件,文中所给为word/document.xml ## 仿照成果[^4] 核心代码如下 ```python def add_pinyin_to_char(self, run, char,pinyin,pinyinfont='SimSun'): """为单个汉字添加注音""" if not pinyin: run.text = char return # 创建一个新的字段(field) fld_char_begin = OxmlElement('w:fldChar') fld_char_begin.set(qn('w:fldCharType'), 'begin') instr_text = OxmlElement('w:instrText') # 设置EQ域代码,用于显示注音 # 格式: EQ \* jc0 \* "Font:SimSun" \* hps14 \o \ad(\s \up 13(拼音),汉字) instr_text.text = f' EQ \* jc0 \* "Font:{pinyinfont}" \* hps14 \\o \\ad(\\s \\up 13({pinyin}),{char})' fld_char_separate = OxmlElement('w:fldChar') fld_char_separate.set(qn('w:fldCharType'), 'separate') fld_char_end = OxmlElement('w:fldChar') fld_char_end.set(qn('w:fldCharType'), 'end') # 清除原run的文本 run.text = '' # 将字段添加到run中 run._r.append(fld_char_begin) run._r.append(instr_text) run._r.append(fld_char_separate) run._r.append(fld_char_end) ``` [^4]:使用了TREA的VsCode插件辅助完成 # 使用方法 ## 使用库 os,re,python-docx,pypinyin,tkinter ## 代码文件 ### 1.word_pinyin.py 包含了实现目标的最少代码 调用方法如下 ```python if __name__ == "__main__": wp = WordPinyin() # 创建示例文档 sample_file = "[你的word文件名].docx" # 为示例文档添加注音 output_file = wp.process_document(sample_file[, output_file]) #可添加output_file为输出文件地址,默认文件为[你的word文件名]_with_pinyin.docx print("操作完成!") ``` ### 2.main.py 使用tkinter设计UI,增加拼音预览,多音字修改,段落修改等功能 #### 第一步:打开原始文件(最好不包含拼音) ![打开文件](https://i-blog.csdnimg.cn/direct/b247338972e847c2a95f76f37c3f03cf.png) 效果如下图: ![文件预览](https://i-blog.csdnimg.cn/direct/44f5abdafb5949d1926f2fb0de2a0d79.png) #### 第二步:预览拼音,生成所有拼音 ![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/96236a9efa3e4a40bef2d6d7ffb652a7.png) 效果如下图: ![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/dc19cf0ecc3c440f8447a8e69a1b75a1.png) #### 第三步:修改错误的拼音 ![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/aa2b66fd13ec4c7d91b0b7614128e06f.png) 效果如下:可能处理时间有点长,请耐心等待![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/042414594ffc454c842087a1f541be98.png) #### 第四步:(可不做)修改文档字体和字号[^5] 选择需要修改的段落,在右侧进行修改 ![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/f4cdfde0de144ae5b0151e138b2360d1.png) [^5]:该功能完全为TREA生成,作者并未测试,请谨慎使用。 #### 第五步:保存结果 点击处理文件,把新docx文件保存到预期位置 ![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/473b2d50716b48cc81a3eecb101ffe5e.png) ### requirements.txt 下载运行库 # 作者结语 如果这个项目对您有帮助,那么请在gitee和Gitcode上star一下吧。