一、利用VBA脚本直接清除
打Excel,打开脚本编辑器(Alt+F11)或者如图,右键sheet名称
输入代码并运行,即可清除密码保护:
Sub DeletePW()
ActiveSheet.Protect DrawingObjects:=True, Contents:=True, AllowFiltering:=True
ActiveSheet.Protect DrawingObjects:=False, Contents:=True, AllowFiltering:=True
ActiveSheet.Protect DrawingObjects:=True, Contents:=True, AllowFiltering:=True
ActiveSheet.Protect DrawingObjects:=False, Contents:=True, AllowFiltering:=True
ActiveSheet.Unprotect
End Sub
二、用python代码批量处理多个Excel文件
注意:这种方法前提是得知道密码。
直接上代码:
'''
Title: 批量清除Excel保护密码
Author: JackieZheng
Date: 2022-04-07 20:38:46
LastEditTime: 2022-04-08 18:35:37
LastEditors: Please set LastEditors
Description:
FilePath: \pythonCode\RemoveExcelPwd.py
'''
import os
import win32com.client
from win32com.client import Dispatch
# 如果有打开的excel窗口先关闭,否则后边会报错
def removePassword(path,password):
try:
xlApp = win32com.client.DispatchEx('Excel.Application')
except Exception as err:
print('错误: %s' % err)
try:
for file in os.listdir(path):
filepath = os.path.join(path, file)
if not os.path.isfile(filepath):
continue
print(filepath)
xlApp.Visible = False
xlApp.DisplayAlerts = False
wb = xlApp.Workbooks.Open(filepath)
try:
# print(password)
wb.Unprotect(password)
wb.Checkcompatibility = False
sht = wb.Worksheets('Sheet1')
sht.Unprotect(password)
except Exception as err:
print('清除 %s 的保护密码出错:%s' % (file, err))
wb.Save()
wb.Close(SaveChanges=True)
finally:
if hasattr(xlApp, 'Quit'):
xlApp.Quit()
path=r'E:\数据21院校专业计划'
password=r'135246'
removePassword(path,password)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)