import http.client, urllib.parse, json
from xml.etree import ElementTree
import wave
apiKey = "你的密钥"
params = "hello"
headers = {"Ocp-Apim-Subscription-Key": apiKey}
AccessTokenHost = "eastasia.api.cognitive.microsoft.com"
path = "/sts/v1.0/issueToken"
print ("Connect to server to get the Access Token")
conn = http.client.HTTPSConnection(AccessTokenHost)
conn.request("POST", path, params, headers)
response = conn.getresponse()
print(response.status, response.reason)
data = response.read()
conn.close()
accesstoken = data.decode("UTF-8")
print ("Access Token: " + accesstoken)
body = ElementTree.Element('speak', version='1.0')
body.set('{http://www.w3.org/XML/1998/namespace}lang', 'zh-CN')
voice = ElementTree.SubElement(body, 'voice')
voice.set('{http://www.w3.org/XML/1998/namespace}lang', 'zh-CN') //语言
voice.set('{http://www.w3.org/XML/1998/namespace}style', 'lyrical') //语气
voice.set('{http://www.w3.org/XML/1998/namespace}gender', 'Female')
voice.set('name', 'Microsoft Server Speech Text to Speech Voice (zh-CN, XiaoxiaoNeural)') //发音人
voice.text = '这次的事故要严格总结,防止下次再次发生' //要转化的文本内容
headers = {"Content-type": "application/ssml+xml",
"X-Microsoft-OutputFormat": "riff-24khz-16bit-mono-pcm",
"Authorization": "Bearer " + accesstoken,