吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 2874|回复: 41
收起左侧

[Windows] Excel文件批量加密工具(开源)

  [复制链接]
墨羽风 发表于 2025-5-7 09:01
批量把文件夹目录下所有excel文件设置打开权限密码,效果图看下图


下载链接: https://pan.baidu.com/s/1ZDus7sp9C_8qnxL8aNrriw 提取码: 52pj
下载链接:https://pan.quark.cn/s/17bd2da3e61e
Snipaste_2025-05-06_21-47-25.jpg

[Python] 纯文本查看 复制代码
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
import os
import sys
import win32com.client as win32
from PyQt5.QtWidgets import (QApplication, QMainWindow, QWidget, QVBoxLayout,
                             QHBoxLayout, QLabel, QLineEdit, QPushButton,
                             QTextEdit, QFileDialog, QProgressBar, QMessageBox,
                             QCheckBox)
from PyQt5.QtCore import Qt, QThread, pyqtSignal
  
  
class EncryptionWorker(QThread):
    """加密工作线程"""
    progress_update = pyqtSignal(str)
    progress_value = pyqtSignal(int)
    finished_signal = pyqtSignal(int, int)
  
    def __init__(self, directory, password, suffix):
        super().__init__()
        self.directory = directory
        self.password = password
        self.suffix = suffix
  
    def run(self):
        """执行加密操作"""
        # 支持的Excel文件扩展名
        excel_extensions = ['.et', '.xls', '.xlsx']
  
        # 确保目录路径存在
        if not os.path.exists(self.directory):
            self.progress_update.emit(f"错误: 目录 '{self.directory}' 不存在!")
            return
  
        # 计数器
        encrypted_files = []
        failed_files = []
  
        self.progress_update.emit(f"开始扫描目录: {self.directory}")
  
        # 首先计算总文件数
        excel_files = []
        for root, _, files in os.walk(self.directory):
            for file in files:
                file_path = os.path.join(root, file)
                file_ext = os.path.splitext(file)[1].lower()
                if file_ext in excel_extensions:
                    excel_files.append(file_path)
  
        total_files = len(excel_files)
        self.progress_update.emit(f"找到 {total_files} 个Excel文件")
  
        # 初始化Excel应用程序
        excel = None
        try:
            excel = win32.gencache.EnsureDispatch('Excel.Application')
            excel.DisplayAlerts = False
  
            # 遍历处理所有文件
            for index, file_path in enumerate(excel_files):
                try:
                    self.progress_update.emit(f"正在处理: {file_path}")
                    self.progress_value.emit(int((index / total_files) * 100) if total_files > 0 else 0)
  
                    # 生成新文件名
                    file_dir = os.path.dirname(file_path)
                    file_name, file_ext = os.path.splitext(os.path.basename(file_path))
                    new_file_name = f"{file_name}{self.suffix}{file_ext}"
                    new_file_path = os.path.join(file_dir, new_file_name)
  
                    # 打开Excel文件
                    wb = excel.Workbooks.Open(os.path.abspath(file_path))
  
                    # 设置密码并另存为新文件
                    wb.SaveAs(os.path.abspath(new_file_path), Password=self.password)
                    wb.Close()
  
                    encrypted_files.append(new_file_path)
                    self.progress_update.emit(f"已加密并保存为: {new_file_path}")
                except Exception as e:
                    failed_files.append((file_path, str(e)))
                    self.progress_update.emit(f"加密失败: {file_path} - 错误: {str(e)}")
        except Exception as e:
            self.progress_update.emit(f"初始化Excel应用程序失败: {str(e)}")
        finally:
            # 确保Excel应用程序被关闭
            if excel:
                try:
                    excel.Quit()
                except:
                    pass
  
        self.progress_value.emit(100)
        self.finished_signal.emit(len(encrypted_files), len(failed_files))
  
  
class ExcelEncryptorApp(QMainWindow):
    """Excel文件批量加密工具主窗口"""
  
    def __init__(self):
        super().__init__()
        self.init_ui()
  
    def init_ui(self):
        """初始化用户界面"""
        # 设置窗口标题和大小
        self.setWindowTitle('Excel文件批量加密工具----吾爱破解----By:墨羽风')
        self.setGeometry(300, 300, 600, 500)
  
        # 创建中央部件
        central_widget = QWidget()
        self.setCentralWidget(central_widget)
  
        # 创建主布局
        main_layout = QVBoxLayout(central_widget)
  
        # 目录选择部分
        dir_layout = QHBoxLayout()
        dir_label = QLabel('目录路径:')
        self.dir_edit = QLineEdit()
        browse_btn = QPushButton('浏览...')
        browse_btn.clicked.connect(self.browse_directory)
  
        dir_layout.addWidget(dir_label)
        dir_layout.addWidget(self.dir_edit)
        dir_layout.addWidget(browse_btn)
  
        # 密码输入部分
        pwd_layout = QHBoxLayout()
        pwd_label = QLabel('加密密码:')
        self.pwd_edit = QLineEdit()
        self.pwd_edit.setEchoMode(QLineEdit.Password)
  
        pwd_layout.addWidget(pwd_label)
        pwd_layout.addWidget(self.pwd_edit)
  
        # 文件后缀输入部分
        suffix_layout = QHBoxLayout()
        suffix_label = QLabel('文件后缀:')
        self.suffix_edit = QLineEdit()
        self.suffix_edit.setText("_加密")
  
        suffix_layout.addWidget(suffix_label)
        suffix_layout.addWidget(self.suffix_edit)
  
        # 操作按钮
        btn_layout = QHBoxLayout()
        self.encrypt_btn = QPushButton('开始加密')
        self.encrypt_btn.clicked.connect(self.start_encryption)
  
        btn_layout.addStretch()
        btn_layout.addWidget(self.encrypt_btn)
  
        # 进度条
        self.progress_bar = QProgressBar()
        self.progress_bar.setValue(0)
  
        # 日志输出区域
        log_label = QLabel('处理日志:')
        self.log_text = QTextEdit()
        self.log_text.setReadOnly(True)
  
        # 添加所有部件到主布局
        main_layout.addLayout(dir_layout)
        main_layout.addLayout(pwd_layout)
        main_layout.addLayout(suffix_layout)
        main_layout.addLayout(btn_layout)
        main_layout.addWidget(self.progress_bar)
        main_layout.addWidget(log_label)
        main_layout.addWidget(self.log_text)
  
    def browse_directory(self):
        """打开目录选择对话框"""
        directory = QFileDialog.getExistingDirectory(self, '选择目录')
        if directory:
            self.dir_edit.setText(directory)
  
    def start_encryption(self):
        """开始加密操作"""
        directory = self.dir_edit.text().strip()
        password = self.pwd_edit.text().strip()
        suffix = self.suffix_edit.text().strip()
  
        # 验证输入
        if not directory:
            QMessageBox.warning(self, '输入错误', '请选择要处理的目录')
            return
  
        if not password:
            QMessageBox.warning(self, '输入错误', '请输入加密密码')
            return
  
        if not suffix:
            reply = QMessageBox.question(self, '确认操作',
                                         '您没有输入文件后缀,加密后的文件将覆盖原文件,是否继续?',
                                         QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
            if reply == QMessageBox.No:
                return
  
        # 确认操作
        reply = QMessageBox.question(self, '确认操作',
                                     f'将对目录 "{directory}" 中的所有Excel文件进行加密,并保存为带有后缀 "{suffix}" 的新文件,是否继续?',
                                     QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
  
        if reply == QMessageBox.Yes:
            # 清空日志和进度条
            self.log_text.clear()
            self.progress_bar.setValue(0)
  
            # 禁用按钮,防止重复操作
            self.encrypt_btn.setEnabled(False)
  
            # 创建并启动工作线程
            self.worker = EncryptionWorker(directory, password, suffix)
            self.worker.progress_update.connect(self.update_log)
            self.worker.progress_value.connect(self.progress_bar.setValue)
            self.worker.finished_signal.connect(self.encryption_finished)
            self.worker.start()
  
    def update_log(self, message):
        """更新日志输出"""
        self.log_text.append(message)
        # 自动滚动到底部
        self.log_text.verticalScrollBar().setValue(
            self.log_text.verticalScrollBar().maximum()
        )
  
    def encryption_finished(self, encrypted, failed):
        """加密完成后的处理"""
        self.update_log(f"\n加密完成! 成功加密 {encrypted} 个文件,失败 {failed} 个文件。")
        self.encrypt_btn.setEnabled(True)
  
        # 显示完成消息
        QMessageBox.information(self, '操作完成',
                                f'加密完成!\n成功加密 {encrypted} 个文件\n失败 {failed} 个文件')
  
  
def main():
    """程序入口点"""
    app = QApplication(sys.argv)
    window = ExcelEncryptorApp()
    window.show()
    sys.exit(app.exec_())
  
  
if __name__ == "__main__":
    main()

免费评分

参与人数 10吾爱币 +10 热心值 +8 收起 理由
yhuacn + 1 + 1 我很赞同!
jy443396 + 1 + 1 热心回复!
grrr_zhao + 1 + 1 谢谢@Thanks!
wuloveyou + 1 + 1 我很赞同!
smtwtfs + 1 + 1 我很赞同!
cioceo + 1 谢谢@Thanks!
chinaxndd + 1 + 1 我很赞同!
vaycore + 1 谢谢@Thanks!
woyucheng + 1 + 1 谢谢@Thanks!
helian147 + 1 + 1 热心回复!

查看全部评分

本帖被以下淘专辑推荐:

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

hs_32 发表于 2025-5-9 22:52
开始扫描目录: C:/Users/Administrator/Desktop/33
找到 5 个Excel文件
初始化Excel应用程序失败: This COM object can not automate the makepy process - please run makepy manually for this object

加密完成! 成功加密 0 个文件,失败 0 个文件。
xian54966 发表于 2025-5-7 09:12
Tomyi 发表于 2025-5-7 09:15
pockyoyy 发表于 2025-5-7 09:18
感谢楼主分享
accanewton 发表于 2025-5-7 09:24
实用,手动点赞
otra 发表于 2025-5-7 09:26
批量加密是亮点
a84189097 发表于 2025-5-7 09:28
实用的功能,感谢楼主
hannoch 发表于 2025-5-7 09:31
挺不错的,很使用,感谢楼主
wei7758 发表于 2025-5-7 09:32
谢谢分享,这个功能挺实用
调味包 发表于 2025-5-7 09:50
最后加密的文件保存成CSV 留痕, 免得重要资料忘记密码,哈哈
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2025-5-26 09:54

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表