两种方法清除Excel保护密码

1 篇文章 1 订阅

一、利用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)

评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JackieZhengChina

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值