python批量將excel內(nèi)容進行翻譯寫入功能
由于小編初來乍到,有很多地方不是很到位,還請見諒,但是很實用的哦!
1.首先是需要進行文件的讀寫操作,需要獲取文件路徑,方式使用os.listdir(路徑)進行批量查找文件。
file_path = ‘/home/xx/xx/xx'
# ret 返回一個列表
ret = list_dir = os.listdir(file_path)
# 遍歷列表,獲取需要的結(jié)尾文件(只考慮獲取文件,不考慮執(zhí)行效率)
for i in ret :
if i.endswith('xlsx'):
# 執(zhí)行的邏輯
2.改寫一下我調(diào)用的翻譯接口
def baidu_translate(appi, secretKe, content):
appid = appi
secretKey = secretKe
httpClient = None
myurl = '/api/trans/vip/translate'
q = content
fromLang = 'zh' # 源語言
toLang = 'en' # 翻譯后的語言
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") # 獲得返回的結(jié)果,結(jié)果為json格式
js = json.loads(jsonResponse) # 將json格式的結(jié)果轉(zhuǎn)換字典結(jié)構(gòu)
dst = str(js["trans_result"][0]["dst"]) # 取得翻譯后的文本結(jié)果
print(dst) # 打印結(jié)果
return dst
except Exception as e:
print(e)
finally:
if httpClient:
httpClient.close()
3.現(xiàn)在需要進行讀取excel的內(nèi)容,使用方法,xlrd,小編使用的翻譯是借用的百度翻譯的API,獲取excel內(nèi)容,傳遞給API
import hashlib
import http.client
import json
import os
import random
import time
import urllib
import openpyxl
import xlrd
# 借用上邊所述的文件路徑操作
# appid :翻譯API提供,需要注冊獲取
# secretKey :翻譯API提供,需要注冊獲取
def read_excel(file_path, appid, secretKey):
list_dir = os.listdir(file_path)
for i in list_dir:
if i.endswith('.xlsx'):
# 拼接獲取絕對路徑
file_path = file_path + '\\' + i
rbook = xlrd.open_workbook(filename=file_path)
rbook.sheets()
# 獲取excel某頁數(shù)據(jù)
sheet1 = rbook.sheet_by_index(0)
row_num = sheet1.nrows
for num in range(row_num):
try:
# 小編這樣寫的原因是我值獲取指定列的數(shù)據(jù),
# 例如現(xiàn)在獲取第3,4列數(shù)據(jù)
txt1 = sheet1.cell_value(num, 3)
txt2 = sheet1.cell_value(num, 4)
# 為了2列數(shù)據(jù)可以同時進行翻譯
txt = txt1 + '=' + txt2
# ret返回翻譯結(jié)果
ret = baidu_translate(appid, secretKey, txt)
# 防止調(diào)用接口出錯
time.sleep(1)
# 將翻譯結(jié)果在寫如excel
write_excel(ret, num, file_path)
except Exception as e:
print(e)
continue
4.因為上一步調(diào)用了這個寫入excel的函數(shù),所有我們需要寫一個函數(shù)來完成寫入的操作。
def write_excel(ret, num, file_path):
f_txt = file_path
book = openpyxl.load_workbook(f_txt)
sheet1 = book.worksheets[0]
# 在這個地方是獲取某列寫入
txtE = 'F' + 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)
總結(jié)
以上所述是小編給大家介紹的python批量將excel內(nèi)容進行翻譯寫入功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!
相關(guān)文章
Django+vue+vscode前后端分離搭建的實現(xiàn)
本文以一個非常簡單的demo為例,介紹了利用django+drf+vue的前后端分離開發(fā)模式,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2023-08-08
正確理解python中的關(guān)鍵字“with”與上下文管理器
這篇文章主要介紹了關(guān)于python中關(guān)鍵字"with"和上下文管理器的相關(guān)資料,文中介紹的非常詳細,相信對大家學(xué)習(xí)或者使用python具有一定的參考價值,需要的朋友們下面來一起看看吧。2017-04-04
Python實現(xiàn)字典按key或者value進行排序操作示例【sorted】
這篇文章主要介紹了Python實現(xiàn)字典按key或者value進行排序操作,結(jié)合實例形式分析了Python針對字典按照key或者value進行排序的相關(guān)操作技巧,需要的朋友可以參考下2019-05-05
深度定制Python的Flask框架開發(fā)環(huán)境的一些技巧總結(jié)
現(xiàn)在越來越多的人使用virtualenv虛擬環(huán)境部署Python項目,包括針對框架的實例文件夾與版本控制布置,這里我們就來整理關(guān)于深度定制Python的Flask框架開發(fā)環(huán)境的一些技巧總結(jié)2016-07-07
Python argparse中的action=store_true用法小結(jié)
這篇文章主要介紹了Python argparse中的action=store_true用法小結(jié),本文結(jié)合示例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-02-02

