一、利用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:\数据\2021院校专业计划'
password=r'135246'
removePassword(path,password)

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐