Python實(shí)現(xiàn)對(duì)word文檔添加密碼去除密碼的示例代碼
代碼實(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?pytorch實(shí)現(xiàn)繪制一維熱力圖
熱力圖是非常特殊的一種圖,可以顯示不可點(diǎn)擊區(qū)域發(fā)生的事情,這篇文章主要為大家介紹了如何利用pytorch實(shí)現(xiàn)繪制一維熱力圖,感興趣的可以了解一下2023-05-05對(duì)python數(shù)據(jù)切割歸并算法的實(shí)例講解
今天小編就為大家分享一篇對(duì)python數(shù)據(jù)切割歸并算法的實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-12-12Python+OpenCV+pyQt5錄制雙目攝像頭視頻的實(shí)例
今天小編就為大家分享一篇Python+OpenCV+pyQt5錄制雙目攝像頭視頻的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-06-06Python線性擬合實(shí)現(xiàn)函數(shù)與用法示例
這篇文章主要介紹了Python線性擬合實(shí)現(xiàn)函數(shù)與用法,結(jié)合實(shí)例形式分析了Python使用線性擬合算法與不使用線性擬合算法的相關(guān)算法操作技巧,需要的朋友可以參考下2018-12-12python類(lèi)的方法屬性與方法屬性的動(dòng)態(tài)綁定代碼詳解
這篇文章主要介紹了python類(lèi)的方法屬性與方法屬性的動(dòng)態(tài)綁定代碼詳解,具有一定借鑒價(jià)值,需要的朋友可以參考下2017-12-12python使用點(diǎn)操作符訪問(wèn)字典(dict)數(shù)據(jù)的方法
這篇文章主要介紹了python使用點(diǎn)操作符訪問(wèn)字典(dict)數(shù)據(jù)的方法,涉及Python操作字典的技巧,需要的朋友可以參考下2015-03-03