python-3.x - 我似乎无法让 google.cloud.texttospeech 工作
问题描述
我使用 Python 3.8,我复制粘贴了这段代码作为测试。
from google.cloud import texttospeech
# Instantiates a client
client = texttospeech.TextToSpeechClient()
# Set the text input to be synthesized
synthesis_input = texttospeech.SynthesisInput(text="Hello, World!")
# Build the voice request, select the language code ("en-US") and the ssml
# voice gender ("neutral")
voice = texttospeech.VoiceSelectionParams(
language_code="en-US", ssml_gender=texttospeech.SsmlVoiceGender.NEUTRAL
)
# Select the type of audio file you want returned
audio_config = texttospeech.AudioConfig(
audio_encoding=texttospeech.AudioEncoding.MP3
)
# Perform the text-to-speech request on the text input with the selected
# voice parameters and audio file type
response = client.synthesize_speech(
input=synthesis_input, voice=voice, audio_config=audio_config
)
# The response's audio_content is binary.
with open("output.mp3", "wb") as out:
# Write the response to the output file.
out.write(response.audio_content)
print('Audio content written to file "output.mp3"')
这是谷歌显示的代码,可以在这里看到:GOOGLE LINK
现在我的问题是我收到了这个错误
PS C:\Users\User\Desktop> & C:/Users/User/AppData/Local/Programs/Python/Python38/python.exe "c:/Users/User/Desktop/from google.cloud import texttospeech.py"
Traceback (most recent call last):
File "c:/Users/User/Desktop/from google.cloud import texttospeech.py", line 7, in <module>
synthesis_input = texttospeech.types.SynthesisInput(text="Hello, World!")
AttributeError: module 'google.cloud.texttospeech' has no attribute 'types'
PS C:\Users\User\Desktop>
我尝试更改它以在代码中添加凭据,但问题仍然存在。这是我更改的行:
client = texttospeech.TextToSpeechClient(credentials="VoiceAutomated-239f1c05600c.json")
解决方案
我可以通过降级库来解决这个错误:
pip3 install "google-cloud-texttospeech<2.0.0"
推荐阅读
- rx-swift - 在publishedSubject 上使用.startWith 和在publishedSubject 上使用.onNext 之间的区别
- json - Amazon 策略生成器 - 策略包含无效资源
- ios - Switch View Controller From TableViewCell Class in Swift
- python - 如何从另一个 pod 或 Kubernetes 实体创建/删除 pod
- python - 如何删除非ASCII字符?
- php - Laravel 5.6 更改电子邮件地址重置登录限制
- r - 按另一个因子中的类别计算因子变量中每个类别的比率
- c - C - 文件输入/输出重定向和 EOF
- excel - 按选中的项目排序列表视图
- angular - Contextual Information When Using an Angular Form Array