# -*- coding: utf-8 -*-
import random,os
import json,time
import base64,http.client
import requests,urllib.parse
import shutil,datetime,hashlib
import requests
# appid_word = "26700"
# APIKey_word='qmvg400hG7k'
# secretKey_word = "VXjOm00n4D"
file='zhongwen.docx'
filetype=file.split('.')[-1]
filefrontname=file.split('.')[-2]
print(filetype)
with open(file, 'rb') as f:
wordcontent = base64.b64encode(f.read())
wordcontent = bytes.decode(wordcontent)
f.close()
time.sleep(3)
host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=qm00G7k&client_secret=VXj001114D'
response_token = requests.get(host)
token = response_token.json()['access_token']
print(token)
url = 'https://aip.baidubce.com/rpc/2.0/mt/v2/doc-translation/create?access_token=' + token
# Build request
headers = {'Content-Type': 'application/json;charset=utf-8'}
# headers = {'Content-Type': 'application/x-www-form-urlencoded'}
payload = { 'from': "zh",
'to': "en",
"domain": "general",
"input": {"content":wordcontent, "format": filetype,"filename": file,},
"output": {"filename_prefix": "new%s"%filefrontname, "formats": [filetype],}
}
# pip install yfinance==0.1.62 -i https://pypi.douban.com/simple/
r = requests.post(url=url, json=payload, headers=headers)
print(r.text)
result_id = r.json()['result']['id']
print(result_id)
time.sleep(1)
host2 = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=qmv00G7k&client_secret=VX00vy114D'
response_token2 = requests.get(host2)
token2 = response_token2.json()['access_token']
print(token)
url2 = 'https://aip.baidubce.com/rpc/2.0/mt/v2/doc-translation/query?access_token=' + token2
# q = '输入query'; # example: hello
# Build request
headers2 = {'Content-Type': 'application/json;charset=utf-8'}
payload2 = {
"id": result_id
}
# Send request
r2 = requests.post(url2, params=payload2, headers=headers2)
result2 = r2.json()['result']['data']
print(result2)
runingpb()
time.sleep(5)
r3 = requests.post(url2, params=payload2, headers=headers2)
result3 = r3.json()['result']['data']['status']
print(result3)
while not result3 == 'Succeeded':
time.sleep(5)
r3 = requests.post(url2, params=payload2, headers=headers2)
result3 = r3.json()['result']['data']['status']
print(result3)
result4 = r3.json()['result']['data']['output']['files'][0]['url']
print(result4)
import webbrowser
play_url=result4
webbrowser.open(play_url)
2022719链接:https://pan.baidu.com/s/1_Sm_IUdG_8hnwBrcKM1HRg?pwd=0719