首页 > 解决方案 > 我似乎无法让 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")

标签: python-3.xgoogle-cloud-platform

解决方案


我可以通过降级库来解决这个错误:
pip3 install "google-cloud-texttospeech<2.0.0"


推荐阅读