一、利用VBA腳本直接清除
打Excel,打開腳本編輯器(Alt+F11)或者如圖,右鍵sheet名稱
輸入代碼并運行,即可清除密碼保護:
Sub DeletePW()ActiveSheet.Protect DrawingObjects:=True, Contents:=True, AllowFiltering:=TrueActiveSheet.Protect DrawingObjects:=False, Contents:=True, AllowFiltering:=TrueActiveSheet.Protect DrawingObjects:=True, Contents:=True, AllowFiltering:=TrueActiveSheet.Protect DrawingObjects:=False, Contents:=True, AllowFiltering:=TrueActiveSheet.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):continueprint(filepath)xlApp.Visible = FalsexlApp.DisplayAlerts = Falsewb = xlApp.Workbooks.Open(filepath)try:# print(password)wb.Unprotect(password)wb.Checkcompatibility = Falsesht = 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:\數據\2021院校專業計劃'
password=r'135246'
removePassword(path,password)