pyttsx3无法设置指定语言:如何修正代码实现标准普通话发音?
I totally get how frustrating that mismatch is—you picked the voice labeled "Mandarin" in pyttsx3, but it doesn’t sound anything like the proper Mandarin you get with espeak -vzh. The root issue here is that the voice ID returned by voices[-2].id (marked as 'Mandarin') might not map to the same espeak Chinese voice you’re using in the terminal.
Here’s the straightforward fix:
Instead of relying on the voice object pulled from getProperty('voices'), directly specify espeak’s official Mandarin voice ID in your code. espeak uses zh (or zh-CN for region-specific precision) for standard Mandarin, and pyttsx3 accepts this in the format espeak:<language-code>.
Corrected Code:
import pyttsx3 engine = pyttsx3.init() # Explicitly target espeak's Mandarin voice engine.setProperty("voice", "espeak:zh") engine.say('你好你好你好你好') engine.runAndWait()
Why this works:
When you run espeak -vzh in the terminal, you’re directly calling espeak’s built-in Mandarin voice. But pyttsx3’s voice list might include other "Mandarin" voices (like system-level TTS options) that aren’t the espeak one you want. By using espeak:zh as the voice ID, you’re forcing pyttsx3 to use the exact same voice as your terminal command.
If espeak:zh doesn’t do the trick, try espeak:zh-CN instead—some systems use the region-specific code for more accurate voice targeting.
内容的提问来源于stack exchange,提问作者showkey

