python batch will excel content translation, writes

  1. The first is the need to read and write files, you need to get the file path, way to use os.listdir (path) to find a batch file.

  file_path = ‘/home/xx/xx/xx’

  # Ret returns a list

  ret = list_dir = os.listdir(file_path)

  # Through the list to get the required end of the file (only consider obtaining documents, regardless of efficiency)

  for i in ret :

  if i.endswith('xlsx'):

  # Execution logic

  2. rewrite what I call the interface translation

  def baidu_translate(appi, secretKe, content):

  APPID = Rescue

  secretKey = secretKe

  httpClient = None

  myurl = '/api/trans/vip/translate'

  q = content

  fromLang = 'zh' # source language

  After toLang = 'en' # language translation

  salt = random.randint(32768, 65536)

  sign = appid + q + str(salt) + secretKey

  sign = hashlib.md5(sign.encode()).hexdigest()

  myurl = myurl + '?appid=' + appid + '&q=' + urllib.parse.quote(

  q) + '&from=' + fromLang + '&to=' + toLang + '&salt=' + str(

  salt) + '&sign=' + sign

  try:

  httpClient = http.client.HTTPConnection('api.baidu_translation.baidu.com')

  httpClient.request('GET', myurl)

  response = httpClient.getresponse()

  jsonResponse = response.read (). decode ( "utf-8") # returned results obtained, results json format

  js = json.loads (jsonResponse) # result json format conversion dictionary structure

  dst = str (js [ "trans_result"] [0] [ "dst"]) # of getting the translated text results

  print (dst) # print results

  return dst

  except Exception as e:

  print (e)

  finally:

  if httpClient:

  httpClient.close()

  3. Now you need to read excel content, use, xlrd, translation small series using the API borrowed Baidu translation, access to excel content, delivered to the API

  import hashlib

  import http.client

  import json

  import os

  import random

  import time

  import urllib

  import openpyxl

  import xlrd

  # Borrow upper side of the document path operations

  # Appid: translation API provides, you need to sign up for

  # SecretKey: translation API provides, you need to sign up for

  def read_excel(file_path, appid, secretKey):

  list_dir = os.listdir(file_path)

  for i in list_dir:

  if i.endswith('.xlsx'):

  # Splicing get the absolute path

  file_path = file_path + '\\' + i

  rbook = xlrd.open_workbook(filename=file_path)

  rbook.sheets()

  # Get a page of data excel

  sheet1 = rbook.sheet_by_index(0)

  row_num = sheet1.nrows

  for num in range(row_num):

  try: Wuxi gynecological hospital http://www.ytsgfk120.com/

  Reason # small series like this is that I write data values ​​to obtain the specified column,

  # Columns 3 and 4, for example, acquiring data now

  txt1 = sheet1.cell_value(num, 3)

  txt2 = sheet1.cell_value(num, 4)

  To # 2 data can be translated simultaneously

  txt = txt1 + '=' + txt2

  # Ret return translation

  ret = baidu_translate(appid, secretKey, txt)

  # Call interface to prevent errors

  time.sleep(1)

  # The translation results in writing as excel

  write_excel(ret, num, file_path)

  except Exception as e:

  print (e)

  continue

  4. Because the step to call this function writes excel all we need to write a function to complete the operation written.

  def write_excel(ret, num, file_path):

  f_txt = file_path

  book = openpyxl.load_workbook(f_txt)

  sheet1 = book.worksheets[0]

  # In this place is to get a column written

  txtE = 'T' + str (num + 1)

  txtF = 'G' + str(num + 1)

  s_txt = ret.split('=')

  sheet1[txtE] = s_txt[0]

  sheet1[txtF] = s_txt[1]

  book.save(f_txt)

  if __name__ == '__main__':

  appid = 'xxxx'

  secretKey = 'xxxx'

  path = r'xxx'

  read_excel(path, appid, secretKey)


Guess you like

Origin blog.51cto.com/14335413/2441024