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_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
=
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)
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
:
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):
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()