Python實現(xiàn)對word文檔添加密碼去除密碼的示例代碼
更新時間:2020年12月29日 14:56:57 作者:1want0
這篇文章主要介紹了Python實現(xiàn)對word文檔添加密碼去除密碼,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
代碼實現(xiàn)如下:
import win32com.client,os,time
def word_encryption(path, password):
# 若加密保存.docx時,覆蓋原文件,則無法成功添加密碼。但是保存為另一個文件名,則可以添加密碼。
# 因此將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) # 改臨時文件名稱為原文件名稱
time.sleep(0.5) # 不要刪除,不要刪除
def word_decryption(path, password):
# 若加密保存.docx時,覆蓋原文件,則無法成功添加密碼。但是保存為另一個文件名,則可以添加密碼。
# 因此將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) # 改臨時文件名稱為原文件名稱
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è)置時間隨意操作
dlistdir(filedir) #遍歷word
print('decrytion Done')
實現(xiàn):

到此這篇關(guān)于Python實現(xiàn)對word文檔添加密碼去除密碼的示例代碼的文章就介紹到這了,更多相關(guān)Python word文檔添加密碼去除密碼內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python+OpenCV+pyQt5錄制雙目攝像頭視頻的實例
今天小編就為大家分享一篇Python+OpenCV+pyQt5錄制雙目攝像頭視頻的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-06-06
Python線性擬合實現(xiàn)函數(shù)與用法示例
這篇文章主要介紹了Python線性擬合實現(xiàn)函數(shù)與用法,結(jié)合實例形式分析了Python使用線性擬合算法與不使用線性擬合算法的相關(guān)算法操作技巧,需要的朋友可以參考下2018-12-12
python類的方法屬性與方法屬性的動態(tài)綁定代碼詳解
這篇文章主要介紹了python類的方法屬性與方法屬性的動態(tài)綁定代碼詳解,具有一定借鑒價值,需要的朋友可以參考下2017-12-12
python使用點操作符訪問字典(dict)數(shù)據(jù)的方法
這篇文章主要介紹了python使用點操作符訪問字典(dict)數(shù)據(jù)的方法,涉及Python操作字典的技巧,需要的朋友可以參考下2015-03-03

