两种方法清除Excel保护密码
生活随笔
收集整理的這篇文章主要介紹了
两种方法清除Excel保护密码
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
一、利用VBA腳本直接清除
打Excel,打開腳本編輯器(Alt+F11)或者如圖,右鍵sheet名稱
輸入代碼并運(yùn)行,即可清除密碼保護(hù):
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代碼批量處理多個(gè)Excel文件?
注意:這種方法前提是得知道密碼。
直接上代碼:
''' Title: 批量清除Excel保護(hù)密碼 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窗口先關(guān)閉,否則后邊會(huì)報(bào)錯(cuò) def removePassword(path,password):try:xlApp = win32com.client.DispatchEx('Excel.Application')except Exception as err:print('錯(cuò)誤: %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 的保護(hù)密碼出錯(cuò):%s' % (file, err))wb.Save()wb.Close(SaveChanges=True)finally:if hasattr(xlApp, 'Quit'):xlApp.Quit()path=r'E:\數(shù)據(jù)\2021院校專業(yè)計(jì)劃' password=r'135246' removePassword(path,password)總結(jié)
以上是生活随笔為你收集整理的两种方法清除Excel保护密码的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [转]阿里开源低代码引擎LowCodeE
- 下一篇: MQ(Message Queue)简介