打印pyttsx3的voices列表
时间: 2024-05-06 11:16:41 浏览: 254
可以使用以下代码来打印pyttsx3的voices列表:
```python
import pyttsx3
engine = pyttsx3.init()
voices = engine.getProperty('voices')
for voice in voices:
print("Voice:")
print(" - ID: %s" % voice.id)
print(" - Name: %s" % voice.name)
print(" - Languages: %s" % voice.languages)
print(" - Gender: %s" % voice.gender)
print(" - Age: %s" % voice.age)
```
这将打印出每个可用语音的ID、名称、支持的语言、性别和年龄。
相关问题
pyttsx3怎么添加语音列表呢
要添加语音列表,需要使用 `pyttsx3` 模块中的 `engine.setProperty()` 方法来设置语音属性。具体步骤如下:
1. 创建 `pyttsx3` 引擎对象:
```python
import pyttsx3
engine = pyttsx3.init()
```
2. 使用 `engine.setProperty()` 方法设置语音属性:
```python
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[index].id)
```
其中,`voices` 是一个包含系统支持的所有语音的列表,`index` 是你要使用的语音在列表中的索引,可以通过遍历 `voices` 列表找到你想要的语音。
3. 使用 `engine.say()` 方法将内容添加到语音列表中:
```python
engine.say('Hello, World!')
engine.say('How are you today?')
```
4. 使用 `engine.runAndWait()` 方法执行语音列表中的内容:
```python
engine.runAndWait()
```
完整的代码示例:
```python
import pyttsx3
engine = pyttsx3.init()
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id)
engine.say('Hello, World!')
engine.say('How are you today?')
engine.runAndWait()
```
运行上面的代码,你就可以听到使用第二个语音发出的问候语了。
pyttsx3 男声
### 使用 pyttsx3 设置男性声音进行文本转语音
为了设置 `pyttsx3` 的发音人为男性,可以调整引擎中的语音属性。通过遍历可用的声音并选择合适的性别,能够指定特定的发声方式。
```python
import pyttsx3
engine = pyttsx3.init()
# 获取所有可用的声音
voices = engine.getProperty('voices')
for voice in voices:
if "male" in voice.name.lower():
engine.setProperty('voice', voice.id)
text_to_speak = "这是一个测试语句,使用男性声音朗读。"
engine.say(text_to_speak)
engine.runAndWait()
```
这段代码初始化了 `pyttsx3` 引擎,并获取当前系统上所有的声音配置[^1]。接着循环遍历这些声音选项,查找名称中含有“male”的声音,并将其设为默认发音人[^3]。最后,传递一段文本给 `say()` 方法执行朗读操作,并调用 `runAndWait()` 来等待播放完成[^2]。
值得注意的是,在不同的操作系统平台上可能有不同的内置音色可供选择;因此,“male”关键字的具体匹配可能会有所变化。如果上述方法未能成功切换至男性声音,则建议打印出所有可用的声音列表以查看具体的名字字符串:
```python
print([(v.name, v.gender) for v in voices])
```
这可以帮助确认哪些声音是适合的选择[^4]。
阅读全文
相关推荐
















