pyttsx3文本转语音使用指南2(结合word文档)
要将 pyttsx3 与 Word 文件(.docx)结合使用,核心思路是:从 Word 文件中提取文本,然后用 pyttsx3 朗读或保存为音频。以下是具体实现步骤:
1. 安装依赖库
首先需要安装 pyttsx3 和用于读取 Word 文件的 python-docx 库:
pip install pyttsx3 python-docx
2. 从 Word 文件提取文本
使用 python-docx 读取 Word 文件中的所有段落文本:
from docx import Document
def read_word_file(file_path):
doc = Document(file_path)
full_text = []
for paragraph in doc.paragraphs:
full_text.append(paragraph.text)
return '\n'.join(full_text)
# 示例:读取当前目录下的 example.docx
text = read_word_file("example.docx")
print(text)
3. 结合 pyttsx3 朗读或保存音频
将提取的文本传递给 pyttsx3 进行朗读或保存为音频文件:
import pyttsx3
def text_to_speech(text, save_to_file=False, filename="output.mp3"):
engine = pyttsx3.init()
# 设置语音参数(可选)
engine.setProperty('rate', 150) # 语速
engine.setProperty('volume', 0.9) # 音量
# 朗读文本
engine.say(text)
# 保存为文件(可选)
if save_to_file:
engine.save_to_file(text, filename)
# 等待完成
engine.runAndWait()
# 示例:朗读并保存
text = read_word_file("example.docx")
text_to_speech(text, save_to_file=True, filename="output.mp3")
完整代码示例
from docx import Document
import pyttsx3
def read_word_file(file_path):
"""读取 Word 文件内容"""
doc = Document(file_path)
full_text = [para.text for para in doc.paragraphs]
return '\n'.join(full_text)
def text_to_speech(text, save_to_file=False, filename="output.mp3"):
"""文本转语音"""
engine = pyttsx3.init()
engine.setProperty('rate', 150)
engine.setProperty('volume', 0.9)
engine.say(text)
if save_to_file:
engine.save_to_file(text, filename)
print(f"音频已保存至:{filename}")
engine.runAndWait()
if __name__ == "__main__":
# 读取 Word 文件
input_file = "example.docx"
text = read_word_file(input_file)
# 朗读并保存
text_to_speech(text, save_to_file=True)
关键点说明
-
Word 文件处理:
python-docx仅支持.docx格式,不支持旧版.doc。- 若 Word 文件包含表格、图片等复杂内容,需额外处理(示例仅提取段落文本)。
-
语音参数调整:
- 可通过
engine.setProperty()调整语速、音量或切换语音(参考前文说明)。 - 中文支持需确保系统已安装中文语音包(Windows 自带,Linux/macOS 需额外配置)。
- 可通过
-
保存音频文件:
- 保存的格式依赖系统支持(如 Windows 默认保存为
.wav,安装ffmpeg后可支持.mp3)。
- 保存的格式依赖系统支持(如 Windows 默认保存为
-
批量处理:
- 可结合
os模块遍历文件夹下的所有 Word 文件,实现批量转语音。
- 可结合
扩展功能
1. 过滤空白段落
在读取 Word 文件时,过滤空文本:
def read_word_file(file_path):
doc = Document(file_path)
full_text = [para.text.strip() for para in doc.paragraphs if para.text.strip()]
return '\n'.join(full_text)
2. 添加进度提示
在朗读时添加提示音:
def text_to_speech(text):
engine = pyttsx3.init()
engine.say("开始朗读文档内容")
engine.runAndWait()
engine.say(text)
engine.runAndWait()
3. 处理多语言混合
若文档包含中英文混合内容,可尝试切换语音引擎:
# 在初始化后设置中英文语音(需系统支持)
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id) # 假设第二个是英文语音
注意事项
- 文件路径:确保 Word 文件路径正确,避免权限问题。
- 长文本处理:超长文本可能导致内存占用高,建议分段朗读。
- 依赖冲突:某些系统可能需要管理员权限安装语音驱动(如 Linux 的
espeak)。
通过上述方法,你可以轻松将 Word 文档内容转换为语音输出!
浙公网安备 33010602011771号