Python实现对word文档添加密码去除密码的示例代码
作者:1want0 时间:2021-05-05 20:42:05
代码实现如下:
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)#设置时间随意操作
dlistdir(filedir) #遍历word
print('decrytion Done')
实现:
来源:https://blog.csdn.net/qq_34652083/article/details/111771403
标签:Python,word,密码
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
轻松解决Mysql“信息包过大”的处理方法
2008-06-07 16:50:00
浅谈Python协程asyncio
2021-01-12 21:30:32
asp如何在刷新链接之前验证文件是否存在?
2010-06-22 21:09:00
Python matplotlib画图实例之绘制拥有彩条的图表
2023-01-26 02:06:25
![](https://img.aspxhome.com/file/2023/8/86268_0s.jpg)
阿里系的中国雅虎新首页浅谈
2008-07-16 12:19:00
![](https://img.aspxhome.com/file/UploadPic/20087/16/2008716122345245s.png)
解决pyshp UnicodeDecodeError的问题
2021-08-01 10:17:39
一个较复杂的字符串截取函数
2009-11-02 10:45:00
用Python下载一个网页保存为本地的HTML文件实例
2023-04-15 18:41:53
![](https://img.aspxhome.com/file/2023/4/92044_0s.jpg)
关于分页查询和性能问题
2008-03-11 12:25:00
Python之列表的append()方法最容易踩的坑
2022-05-14 18:56:53
用Python编程实现语音控制电脑
2022-01-06 22:53:57
IE7兼容模式与兼容视图
2010-06-28 18:48:00
python同时替换多个字符串方法示例
2021-11-25 00:37:54
opencv基于Haar人脸检测和眼睛检测
2022-02-10 13:43:59
![](https://img.aspxhome.com/file/2023/1/90891_0s.jpg)
python之当你发现QTimer不能用时的解决方法
2021-05-18 04:49:37
python多线程抓取天涯帖子内容示例
2021-03-18 11:56:42
Python操作json的方法实例分析
2022-10-13 10:39:09
![](https://img.aspxhome.com/file/2023/1/95721_0s.png)
python实现网上购物系统
2021-06-12 16:32:26
Python动态配置管理Dynaconf的实现示例详解
2021-01-24 19:35:06
asp 动态数组 提供Add、Insert、Remove、RemoveAt、Search等方法。
2011-03-17 11:10:00