使用的方法
#coding:utf-8
import requests
import string
import time
import hashlib
import json
from PyPDF2.pdf import PdfFileReader, PdfFileWriter, ContentStream
#init
api_url = "http://api.fanyi.baidu.com/api/trans/vip/translate"
my_appid = "你的appid"
cyber = "你的appkey"
## 处理pdf
def getDataUsingPyPdf(filename):
pdf = PdfFileReader(open(filename, "rb"))
content = ""
num = pdf.getNumPages()
for i in range(0, num):
extractedText = pdf.getPage(i).extractText()
content += extractedText + "\n"
return content
def translate(word):
#init salt and final_sign
salt = str(time.time())[:10]
final_sign = str(my_appid)+word+salt+cyber
final_sign = hashlib.md5(final_sign.encode("utf-8")).hexdigest()
#其中form和to可以区别en,zh构造请求参数
paramas = {
'q':word,
'from':'en',
'to':'zh',
'appid':'%s'%my_appid,
'salt':'%s'%salt,
'sign':'%s'%final_sign
}
my_url = api_url+'?appid='+str(my_appid)+'&q='+word+'&from='+'zh'+'&to='+'en'+'&salt='+salt+'&sign='+final_sign
response = requests.get(api_url,params = paramas).content
#content = str(response,encoding = "utf-8")
content = str(response)
print(content)
json_reads = json.loads(content)
return json_reads['trans_result'][0]['dst']+" "
content = getDataUsingPyPdf("test.pdf")
clist = content.split(".")
i=0
while(i<clist.__len__()):
print(translate(clist[i].replace("\n"," ")))
i+=1