最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2020年02月27日 08:37:20   作者:回憶125  
這篇文章主要為大家詳細介紹了python翻譯word表格小程序,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

背景

原是弱電集成的設(shè)計員,糾結(jié)很久后參加了python培訓機構(gòu)轉(zhuǎn)職后的一員小白,由于一次工作中需要翻譯一份近100頁word表格,純手工翻譯大概三個小時,為了解決這種重復(fù)又耗時的勞動,并重溫python相關(guān)知識所以制作了該小程序。

腳本詳情

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目
錄下添加對應(yīng)瀏覽器driver,由于我使用的是chrome所以需下載chromedriver。
'''
def mydoc(doc,table,huan,expath):
 table_contents = []
 table_content_trans = []
 for i in range(0,len(table.rows)):#設(shè)定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):
 # 設(shè)置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
 # 關(guān)閉瀏覽器
 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()

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Python如何讀寫JSON格式數(shù)據(jù)

    Python如何讀寫JSON格式數(shù)據(jù)

    這篇文章主要介紹了Python如何讀寫JSON格式數(shù)據(jù),JSON文件的讀寫應(yīng)算成Python基礎(chǔ)知識的內(nèi)容,在編寫Nonebot插件時,常常會操作JSON類型的數(shù)據(jù),需要的朋友可以參考下
    2023-04-04
  • pytorch學習教程之自定義數(shù)據(jù)集

    pytorch學習教程之自定義數(shù)據(jù)集

    這篇文章主要給大家介紹了關(guān)于pytorch學習教程之自定義數(shù)據(jù)集的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-11-11
  • python3 pandas 讀取MySQL數(shù)據(jù)和插入的實例

    python3 pandas 讀取MySQL數(shù)據(jù)和插入的實例

    下面小編就為大家分享一篇python3 pandas 讀取MySQL數(shù)據(jù)和插入的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-04-04
  • Python合并字符串的3種方法

    Python合并字符串的3種方法

    這篇文章主要介紹了Python合并字符串的3種方法,本文講解了使用+=操作符、使用%操作符、使用String的' '.join()方法3種方法,需要的朋友可以參考下
    2015-05-05
  • Python的生成器函數(shù)詳解

    Python的生成器函數(shù)詳解

    這篇文章主要介紹了Python的生成器函數(shù),具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-02-02
  • Python實現(xiàn)在PDF中插入單圖像水印和平鋪圖像水印

    Python實現(xiàn)在PDF中插入單圖像水印和平鋪圖像水印

    這篇文章主要為大家詳細介紹了如何使用Python實現(xiàn)在PDF中插入單圖像水印和平鋪圖像水印,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下
    2024-04-04
  • Python常用內(nèi)置函數(shù)和關(guān)鍵字使用詳解

    Python常用內(nèi)置函數(shù)和關(guān)鍵字使用詳解

    在Python中有許許多多的內(nèi)置函數(shù)和關(guān)鍵字,它們是我們?nèi)粘V薪?jīng)常可以使用的到的一些基礎(chǔ)的工具,可以方便我們的工作。本文將詳細講解他們的使用方法,需要的可以參考一下
    2022-05-05
  • pandas 使用insert插入一列

    pandas 使用insert插入一列

    這篇文章主要介紹了pandas 使用insert插入一列的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-03-03
  • Docker如何部署Python項目的實現(xiàn)詳解

    Docker如何部署Python項目的實現(xiàn)詳解

    這篇文章主要介紹了Docker如何部署Python項目的實現(xiàn)詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-10-10
  • Python OpenCV調(diào)用攝像頭檢測人臉并截圖

    Python OpenCV調(diào)用攝像頭檢測人臉并截圖

    這篇文章主要為大家詳細介紹了Python OpenCV調(diào)用攝像頭檢測人臉并截圖,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-07-07

最新評論

全州县| 荥阳市| 祥云县| 安丘市| 常熟市| 驻马店市| 依兰县| 灵宝市| 南召县| 鄂尔多斯市| 邹城市| 正镶白旗| 茌平县| 永济市| 新余市| 全椒县| 和政县| 永修县| 韶山市| 侯马市| 临沭县| 左云县| 三亚市| 舟山市| 渑池县| 宾阳县| 肇源县| 靖西县| 武冈市| 密云县| 万山特区| 来宾市| 长顺县| 高青县| 德钦县| 宜兰县| 昌都县| 阳朔县| 香格里拉县| 邵武市| 六枝特区|