欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python實(shí)現(xiàn)對(duì)word文檔添加密碼去除密碼的示例代碼

 更新時(shí)間:2020年12月29日 14:56:57   作者:1want0  
這篇文章主要介紹了Python實(shí)現(xiàn)對(duì)word文檔添加密碼去除密碼,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

代碼實(shí)現(xiàn)如下:

import win32com.client,os,time
 
def word_encryption(path, password):
  # 若加密保存.docx時(shí),覆蓋原文件,則無(wú)法成功添加密碼。但是保存為另一個(gè)文件名,則可以添加密碼。
  # 因此將A存為B,刪A,再將B改為A。
  dirname, tempname = os.path.split(path)
  path_temp = os.path.join(dirname, tempname)
  while os.path.exists(path_temp):
    tempname = f'{len(tempname)}' + tempname
    path_temp = os.path.join(dirname, tempname)
  def encryption(fp, pt, pw):
    word_app = win32com.client.Dispatch('Word.Application')
    word_app.Visible = 0
    word_app.DisplayAlerts = 0
    doc = word_app.Documents.Open(fp, False, False, False, '')
    doc.SaveAs2(pt, None, False, pw)
    doc.Close()
    word_app.Quit()
 
  encryption(path, path_temp, password)
  os.remove(path) # 刪除原文件
  os.rename(path_temp, path) # 改臨時(shí)文件名稱(chēng)為原文件名稱(chēng)
  time.sleep(0.5) # 不要?jiǎng)h除,不要?jiǎng)h除
def word_decryption(path, password):
  # 若加密保存.docx時(shí),覆蓋原文件,則無(wú)法成功添加密碼。但是保存為另一個(gè)文件名,則可以添加密碼。
  # 因此將A存為B,刪A,再將B改為A。
  dirname, tempname = os.path.split(path)
  path_temp = os.path.join(dirname, tempname)
  while os.path.exists(path_temp):
    tempname = f'{len(tempname)}' + tempname
    path_temp = os.path.join(dirname, tempname)
  def decryption(fp, pt, pw):
    word_app = win32com.client.Dispatch('Word.Application')
    word_app.Visible = 0
    word_app.DisplayAlerts = 0
    doc = word_app.Documents.Open(fp, False, False, False, key)
    doc.SaveAs2(pt, None, False, pw)
    doc.Close()
    word_app.Quit()
 
  decryption(path, path_temp, password)
  os.remove(path) # 刪除原文件
  os.rename(path_temp, path) # 改臨時(shí)文件名稱(chēng)為原文件名稱(chēng)
  time.sleep(0.5) # 不用刪除
 
def elistdir(path):
  for file in os.listdir(path):
    file_path = os.path.join(path, file)
    if os.path.isdir(file_path) and file_path==path:#排除子路徑
      elistdir(file_path)
      #print(file_path)
    elif os.path.splitext(file_path)[1]=='.docx':
      #list_name.append(file_path)
      
      if file_path != '':
        print(file_path)
        try:
          word_encryption(file_path, key)
        except:
          pass
 
def dlistdir(path):
  for file in os.listdir(path):
    file_path = os.path.join(path, file)
    if os.path.isdir(file_path) and file_path==path:#排除子路徑
      dlistdir(file_path)
      #print(file_path)
    elif os.path.splitext(file_path)[1]=='.docx':
      #list_name.append(file_path)
      
      if file_path != '':
        print(file_path)
        try:
          word_decryption(file_path, '')
        except:
          pass
 
if __name__ == '__main__':
  key='12345'                 #加密解密密匙
  filedir=r"C:\Users\Administrator\Desktop"# 指定路徑不包含子路徑
  elistdir(filedir) #遍歷word
  print('encrytion sucess\n Waiting...')
  time.sleep(2)#設(shè)置時(shí)間隨意操作
  dlistdir(filedir) #遍歷word
  print('decrytion Done')

實(shí)現(xiàn):

到此這篇關(guān)于Python實(shí)現(xiàn)對(duì)word文檔添加密碼去除密碼的示例代碼的文章就介紹到這了,更多相關(guān)Python word文檔添加密碼去除密碼內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Python中的logging模塊詳解

    Python中的logging模塊詳解

    這篇文章主要介紹了Python中的logging模塊詳解,默認(rèn)情況下Python的logging模塊將日志打印到了標(biāo)準(zhǔn)輸出中,且只顯示了大于等于WARNING級(jí)別的日志,這說(shuō)明默認(rèn)的日志級(jí)別設(shè)置為WARNING,需要的朋友可以參考下
    2023-09-09
  • Python?pytorch實(shí)現(xiàn)繪制一維熱力圖

    Python?pytorch實(shí)現(xiàn)繪制一維熱力圖

    熱力圖是非常特殊的一種圖,可以顯示不可點(diǎn)擊區(qū)域發(fā)生的事情,這篇文章主要為大家介紹了如何利用pytorch實(shí)現(xiàn)繪制一維熱力圖,感興趣的可以了解一下
    2023-05-05
  • python?matplotlib各種畫(huà)圖

    python?matplotlib各種畫(huà)圖

    這篇文章主要介紹了python?matplotlib各種畫(huà)圖,matplotlib是一種優(yōu)秀的python數(shù)據(jù)可視化第三方庫(kù),使用matpltlib庫(kù)畫(huà)圖時(shí),先將它引入,加載里面的pyplot,并命名為plt,然后使用plot函數(shù)畫(huà)圖<BR>,下面一起來(lái)了解更詳細(xì)內(nèi)容吧
    2021-12-12
  • 對(duì)python數(shù)據(jù)切割歸并算法的實(shí)例講解

    對(duì)python數(shù)據(jù)切割歸并算法的實(shí)例講解

    今天小編就為大家分享一篇對(duì)python數(shù)據(jù)切割歸并算法的實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-12-12
  • python二進(jìn)制文件的轉(zhuǎn)譯詳解

    python二進(jìn)制文件的轉(zhuǎn)譯詳解

    這篇文章主要介紹了python二進(jìn)制文件的轉(zhuǎn)譯詳解的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-07-07
  • 詳解flask入門(mén)模板引擎

    詳解flask入門(mén)模板引擎

    這篇文章主要介紹了詳解flask入門(mén)模板引擎,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-07-07
  • Python+OpenCV+pyQt5錄制雙目攝像頭視頻的實(shí)例

    Python+OpenCV+pyQt5錄制雙目攝像頭視頻的實(shí)例

    今天小編就為大家分享一篇Python+OpenCV+pyQt5錄制雙目攝像頭視頻的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-06-06
  • Python線性擬合實(shí)現(xiàn)函數(shù)與用法示例

    Python線性擬合實(shí)現(xiàn)函數(shù)與用法示例

    這篇文章主要介紹了Python線性擬合實(shí)現(xiàn)函數(shù)與用法,結(jié)合實(shí)例形式分析了Python使用線性擬合算法與不使用線性擬合算法的相關(guān)算法操作技巧,需要的朋友可以參考下
    2018-12-12
  • python類(lèi)的方法屬性與方法屬性的動(dòng)態(tài)綁定代碼詳解

    python類(lèi)的方法屬性與方法屬性的動(dòng)態(tài)綁定代碼詳解

    這篇文章主要介紹了python類(lèi)的方法屬性與方法屬性的動(dòng)態(tài)綁定代碼詳解,具有一定借鑒價(jià)值,需要的朋友可以參考下
    2017-12-12
  • python使用點(diǎn)操作符訪問(wèn)字典(dict)數(shù)據(jù)的方法

    python使用點(diǎn)操作符訪問(wèn)字典(dict)數(shù)據(jù)的方法

    這篇文章主要介紹了python使用點(diǎn)操作符訪問(wèn)字典(dict)數(shù)據(jù)的方法,涉及Python操作字典的技巧,需要的朋友可以參考下
    2015-03-03

最新評(píng)論