GetLexicon - Amazon Polly
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅中国的 Amazon Web Services 服务入门



下的 Python 代码使用Amazon SDK for Python (Boto)以检索存储在Amazon区域。该示例接受词典名称作为命令行参数,并仅获取该词典,打印出词典在本地保存的 tmp 路径。

以下代码示例使用存储在AmazonSDK 配置文件。有关创建配置文件的信息,请参阅 步骤 3.1:设置 Amazon Command Line Interface (Amazon CLI)

有关此操作的更多信息,请参阅 GetLexicon API 参考。

from argparse import ArgumentParser from os import path from tempfile import gettempdir from boto3 import Session from botocore.exceptions import BotoCoreError, ClientError # Define and parse the command line arguments cli = ArgumentParser(description="GetLexicon example") cli.add_argument("name", type=str, metavar="LEXICON_NAME") arguments = cli.parse_args() # Create a client using the credentials and region defined in the adminuser # section of the Amazon credentials and configuration files session = Session(profile_name="adminuser") polly = session.client("polly") print(u"Fetching {0}...".format( try: # Fetch lexicon by name response = polly.get_lexicon( except (BotoCoreError, ClientError) as error: # The service returned an error, exit gracefully cli.error(error) # Get the lexicon data from the response lexicon = response.get("Lexicon", {}) # Access the lexicon's content if "Content" in lexicon: output = path.join(gettempdir(), u"%s.pls" % print(u"Saving to %s..." % output) try: # Save the lexicon contents to a local file with open(output, "w") as pls_file: pls_file.write(lexicon["Content"]) except IOError as error: # Could not write to file, exit gracefully cli.error(error) else: # The response didn't contain lexicon data, exit gracefully cli.error("Could not fetch lexicons contents") print("Done.")