安装包:
pip install pyttsx3
import pyttsx3
# 读取 OCR 结果
with open("ocr_result.txt", "r", encoding="utf-8") as f:
text = f.read().strip()
# 确保文本不为空
if text:
engine = pyttsx3.init()
engine.setProperty('rate', 150) # 语速
engine.setProperty('voice', 'zh') # 选择中文语音(可能需要调整)
engine.save_to_file(text, "output.wav")
engine.runAndWait()
print("🎵 语音已保存为 output.wav")
else:
print("⚠️ ocr_result.txt 为空,无法生成音频")