溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

怎么使用python實現(xiàn)翻譯word表格小程序

發(fā)布時間:2021-03-23 12:35:58 來源:億速云 閱讀:183 作者:小新 欄目:開發(fā)技術(shù)

這篇文章給大家分享的是有關怎么使用python實現(xiàn)翻譯word表格小程序的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

腳本詳情

import re
import docx
import time
import pandas as pd
from selenium import webdriver
from selenium.webdriver.chrome.options import Options # 導入chrome選項
from selenium.webdriver.common.keys import Keys
from os import system
'''
seleium爬蟲效率很低但勝在不用考慮反爬問題,由于想加快翻譯速度并實現(xiàn)轉(zhuǎn)換為exe文件后可在其他無python環(huán)境中運行,
添加excel表格充當數(shù)據(jù)庫,excel文件中,一列命名漏洞英文列表,一列命名漏洞翻譯列表,由于使用seleeium需在python目
錄下添加對應瀏覽器driver,由于我使用的是chrome所以需下載chromedriver。
'''
def mydoc(doc,table,huan,expath):
 table_contents = []
 table_content_trans = []
 for i in range(0,len(table.rows)):#設定i值極限 行
 data = pd.DataFrame(pd.read_excel(expath))
 datalist_d = data['漏洞英文列表']
 datalist_t = data['漏洞翻譯列表']
 i_text = table.cell(i,0).text#表格內(nèi)i行j列單元格內(nèi)容賦值給i_text
 zhPattern = re.compile(u'[\u4e00-\u9fa5]+') # 中文字符范圍
 szPattern = re.compile(u'[0-9]') # 數(shù)字范圍
 # spPattern = re.compile(u'[/]+')
 contents = u'{}'.format(i_text) # 表格內(nèi)單元格文本
 # search整個字符串內(nèi)查找模式匹配,找到第一個匹配然后返回一個包含匹配信息的對象,無則NONE
 # match匹配字符串第一位,開頭位置是否匹配,匹配成功才會返回結(jié)果,否則返回None
 #'[^?\\/]'返回指定標點符號
 match_zh = zhPattern.search(contents)
 match_sz = szPattern.match(contents)
 if match_zh or match_sz:
 pass

 else:
 if len(datalist_d) != 0:
 flag_excel = False
 for j in range(len(datalist_d)):
  if datalist_d[j] == i_text:
  table.cell(i,0).text = str(datalist_t[j])
  flag_excel = True
  break
 if flag_excel == False:
  print('漏洞庫中未搜索到...')
  table_contents.append(i_text) # 表格內(nèi)內(nèi)容
  trans_result = myspider(i_text) # 翻譯表格內(nèi)容
  print('翻譯中...')
  if huan == 1:
  trans_result_n = trans_result.replace("\n", "") # 內(nèi)容去除換行
  table.cell(i, 0).text = trans_result_n # 替換表格內(nèi)容
  table_content_trans.append(trans_result_n) # 翻譯和排版后內(nèi)容加入表格
  data_t = pd.Series({"漏洞英文列表": i_text,"漏洞翻譯列表": trans_result_n}, name='漏洞庫') # 添加數(shù)據(jù)
  data_add_t = data.append(data_t) # 添加數(shù)據(jù)
  data_add_t.to_excel(expath, index=False) # 存入excel中
  print('存入漏洞庫...')
  else:
  table.cell(i, 0).text = trans_result # 替換表格內(nèi)容
  table_content_trans.append(trans_result) # 翻譯和排版后內(nèi)容加入表格
  data_t = pd.Series({"漏洞英文列表": i_text,"漏洞翻譯列表": trans_result}, name='漏洞庫') # 添加數(shù)據(jù)
  data_add_t = data.append(data_t) # 添加數(shù)據(jù)
  data_add_t.to_excel(expath, index=False) # 存入excel中
  print('存入漏洞庫...')

 else:
 print('漏洞庫為空')
 table_contents.append(i_text) # 表格內(nèi)內(nèi)容
 trans_result = myspider(i_text) # 翻譯表格內(nèi)容
 print('翻譯中...')
 if huan == 1:
  trans_result_n = trans_result.replace("\n", "") # 內(nèi)容去除換行
  table.cell(i, 0).text = trans_result_n # 替換表格內(nèi)容
  table_content_trans.append(trans_result_n) # 翻譯和排版后內(nèi)容加入表格
  data_t = pd.Series({"漏洞英文列表": i_text,"漏洞翻譯列表": trans_result_n}, name='漏洞庫') # 添加數(shù)據(jù)
  data_add_t = data.append(data_t) # 添加數(shù)據(jù)
  data_add_t.to_excel(expath, index=False) # 存入excel中
  print('存入漏洞庫...')
 else:
  table.cell(i, 0).text = trans_result # 替換表格內(nèi)容
  table_content_trans.append(trans_result) # 翻譯和排版后內(nèi)容加入表格
  data_t = pd.Series({"漏洞英文列表": i_text,"漏洞翻譯列表": trans_result}, name='漏洞庫') # 添加數(shù)據(jù)
  data_add_t = data.append(data_t) # 添加數(shù)據(jù)
  data_add_t.to_excel(expath, index=False) # 存入excel中
  print('存入漏洞庫...')

 #判斷列表中是否都是空字符串
 flag = False
 for i in table_contents:
 if i.strip() != '':
 flag = True
 # 空列表或者列表中都是空字符串不翻譯
 if len(table_contents) == 0 or flag == False:
 return print("此表格無需翻譯或漏洞庫中已存儲")
 else:
 print('表格待翻譯內(nèi)容:',table_contents)
 print('表格翻譯后內(nèi)容:',table_content_trans)

def myspider(text):
 # 設置chrome瀏覽器無頭模式
 chrome_options = Options()
 chrome_options.add_argument('--headless')
 driver = webdriver.Chrome(chrome_options=chrome_options)
 # driver.fullscreen_window() #全屏
 driver.maximize_window() # 屏幕最大化
 # 打開有道翻譯頁面
 driver.get("http://fanyi.youdao.com/")
 time.sleep(0.5)
 # 獲取頁面名為inputOriginal的id標簽的文本內(nèi)容
 inputwd = driver.find_element_by_id("inputOriginal") # 搜索輸入文本框的id屬性值 .text #id="wrapper"的所有文本
 but = driver.find_element_by_id('transMachine') # 搜索提交按鈕//*[@id="transMachine"]
 outputwd = driver.find_element_by_xpath('//*[@id="transTarget"]') # 翻譯后文本框
 inputwd.clear() # 清除文本框里的內(nèi)容
 # outputwd.clear() # 清除文本框里的內(nèi)容
 inputwd.send_keys(text) # 輸入翻譯內(nèi)容
 but.send_keys(Keys.RETURN) # 輸入回車鍵 but.click() #點擊按鈕s
 time.sleep(0.5)
 result = outputwd.text
 # 關閉瀏覽器
 driver.quit()
 return result

def mymain():
 # urlname = input('輸入路徑:')
 docname = input('輸入文件全名:')
 huan = int(input('翻譯內(nèi)容是否需刪除換行(1.是2.否):'))
 # urlname_t = urlname.replace('\\','\\\\')
 # print('轉(zhuǎn)義后路徑:',f'{urlname}//{docname}')
 path = f'.\\{docname}' #文件路徑
 expath = '.\\漏洞庫.xlsx'
 doc = docx.Document(path)
 tables = doc.tables # 獲取文件中的表格集
 e1 = time.time()
 print(f'共{len(tables)}個表格')
 n = 1
 try:
 for i in range(0,len(tables)):
 table = tables[i]
 mydoc(doc,table,huan,expath)
 print(f'\n剩余{len(tables)-n}個表格待翻譯')
 time.sleep(0.3)
 n += 1
 doc.save(f".\\trans{docname}")
 except Exception as e:
 print('報錯:',e)
 e2 = time.time()
 print('耗時:',float(e2 - e1))
 print('轉(zhuǎn)換完畢')
 system('pause')

mymain()

感謝各位的閱讀!關于“怎么使用python實現(xiàn)翻譯word表格小程序”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI