百度文档翻译api

# -*- 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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

rogabet-note

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值