前言
鉴于英文水平有限,读相关外文学术论文实在是困得要死,就想着能不能通过调用百度翻译API实现固定格式文字的多语种翻译工作,试了一下成功了。下面分享一下其思路与代码。
数据格式如下:
百度翻译api申请网址:https://api.fanyi.baidu.com/
对于个人账户而言,每月翻译字符数低于200万,享免费服务;超过200万字符,按照49元/百万字符支付当月超出部分字符量费用。
1.导入库和数据
import requests
import random
import json
import pandas as pd
from hashlib import md5
from tqdm import tqdm
df = pd.read_excel(r'001.xlsx')
2.设置appid和appkey
# 输入自己申请的id和key
appid = 'xxxxxxxxxx'
appkey = 'xxxxxxxxxxxxxxxx'
3.调用百度翻译api获取翻译后的内容
def baidu_translate(api, key, content):
appid = api
appkey = key # 设置你自己的appid/appkey
from_lang = 'en'
to_lang