Python實(shí)現(xiàn)對(duì)word文檔添加密碼去除密碼的示例代碼
代碼實(shí)現(xiàn)如下:
import win32com.client,os,time def word_encryption(path, password): # 若加密保存.docx時(shí),覆蓋原文件,則無法成功添加密碼。但是保存為另一個(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í)文件名稱為原文件名稱 time.sleep(0.5) # 不要?jiǎng)h除,不要?jiǎng)h除def word_decryption(path, password): # 若加密保存.docx時(shí),覆蓋原文件,則無法成功添加密碼。但是保存為另一個(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í)文件名稱為原文件名稱 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:UsersAdministratorDesktop'# 指定路徑不包含子路徑 elistdir(filedir) #遍歷word print(’encrytion sucessn 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)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. springcloud alibaba nacos linux配置的詳細(xì)教程2. Vue為什么要謹(jǐn)慎使用$attrs與$listeners3. SpringBoot 使用 @Value 注解讀取配置文件給靜態(tài)變量賦值4. Java 生成帶Logo和文字的二維碼5. SpringBoot整合MybatisPlus的教程詳解6. 解決在Vue中使用axios POST請求變成OPTIONS的問題7. Spring Boot2發(fā)布調(diào)用REST服務(wù)實(shí)現(xiàn)方法8. Android自定義控件實(shí)現(xiàn)方向盤效果10. python中的socket實(shí)現(xiàn)ftp客戶端和服務(wù)器收發(fā)文件及md5加密文件
