Python3.10将多个word文件合并,合并之后的文档不要改变原本文档中的任何格式,包括字体颜色啥的,要求代码可以打包为exe,双击之后弹出页面,页面首先选择输入文件夹,再选择输出文件夹,再输入合并以后的名字,最后点击开始,显示进度条,颜色为蓝色,进度条中要有百分比,给出代码,使用pyqt5界面化,界面要整洁干净,看着舒服,要注意,我之前的相关代码,运行之后会弹出Python停止工作的提示,请避免
时间: 2023-06-23 17:10:19 浏览: 281
以下是实现上述功能的Python代码,使用PyQt5实现界面化:
```python
import sys
import os
from PyQt5.QtWidgets import QApplication, QMainWindow, QFileDialog
from PyQt5.QtCore import Qt, QThread, pyqtSignal
from PyQt5.QtGui import QPalette, QColor, QBrush, QPixmap
from docx import Document
class MergeThread(QThread):
progress_signal = pyqtSignal(int)
def __init__(self, files_list, output_path, output_name):
super().__init__()
self.files_list = files_list
self.output_path = output_path
self.output_name = output_name
def run(self):
total_files = len(self.files_list)
merged_doc = Document(self.files_list[0])
self.update_progress(1, total_files)
for i in range(1, total_files):
doc = Document(self.files_list[i])
for p in doc.paragraphs:
merged_doc.add_paragraph(p.text, p.style)
for t in doc.tables:
merged_table = merged_doc.add_table(
rows=t.rows.count(), cols=t.columns.count())
for r in range(len(t.rows)):
for c in range(len(t.columns)):
merged_table.cell(r, c).text = t.cell(r, c).text
merged_table.cell(r, c).width = t.cell(r, c).width
self.update_progress(i + 1, total_files)
merged_doc.save(os.path.join(self.output_path, self.output_name))
def update_progress(self, current, total):
progress_percent = int(current / total * 100)
self.progress_signal.emit(progress_percent)
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setWindowTitle('Merge Word Files')
self.setGeometry(200, 200, 800, 600)
# 设置背景图片
palette = QPalette()
palette.setBrush(QPalette.Background,
QBrush(QPixmap('background.jpg')))
self.setPalette(palette)
# 添加选择输入文件夹的按钮
self.input_btn = QPushButton('Select Input Folder', self)
self.input_btn.setGeometry(200, 150, 200, 50)
self.input_btn.clicked.connect(self.select_input_folder)
# 添加选择输出文件夹的按钮
self.output_btn = QPushButton('Select Output Folder', self)
self.output_btn.setGeometry(400, 150, 200, 50)
self.output_btn.clicked.connect(self.select_output_folder)
# 添加输入输出文件名的编辑框
self.input_name_edit = QLineEdit(self)
self.input_name_edit.setGeometry(200, 250, 400, 50)
self.input_name_edit.setPlaceholderText('Input the name of the merged file')
# 添加合并按钮
self.merge_btn = QPushButton('Merge', self)
self.merge_btn.setGeometry(300, 350, 200, 50)
self.merge_btn.clicked.connect(self.merge_files)
# 添加进度条
self.progress_bar = QProgressBar(self)
self.progress_bar.setGeometry(200, 450, 400, 50)
self.progress_bar.setTextVisible(False)
def select_input_folder(self):
self.input_folder = QFileDialog.getExistingDirectory(
self, 'Select Input Folder', '')
if self.input_folder:
self.input_btn.setText(f'Input Folder: {self.input_folder}')
def select_output_folder(self):
self.output_folder = QFileDialog.getExistingDirectory(
self, 'Select Output Folder', '')
if self.output_folder:
self.output_btn.setText(f'Output Folder: {self.output_folder}')
def merge_files(self):
if not hasattr(self, 'input_folder') or not hasattr(self, 'output_folder'):
return
input_files = os.listdir(self.input_folder)
input_files = [os.path.join(self.input_folder, f) for f in input_files if f.endswith('.docx') or f.endswith('.doc')]
if not input_files:
return
output_name = self.input_name_edit.text() + '.docx'
if not output_name:
return
self.merge_thread = MergeThread(input_files, self.output_folder, output_name)
self.merge_thread.progress_signal.connect(self.update_progress_bar)
self.merge_thread.start()
def update_progress_bar(self, progress_percent):
self.progress_bar.setValue(progress_percent)
if progress_percent == 100:
self.input_btn.setText('Select Input Folder')
self.output_btn.setText('Select Output Folder')
self.input_name_edit.clear()
self.progress_bar.reset()
if __name__ == '__main__':
app = QApplication(sys.argv)
mainWindow = MainWindow()
mainWindow.show()
sys.exit(app.exec_())
```
注意事项:
1. 程序需要一个名为 `background.jpg` 的背景图片,放在代码文件同级目录下。
2. 程序还需要 `docx` 模块的支持,可以使用 `pip install python-docx` 命令进行安装。
3. 程序使用了多线程来实现进度条功能,因此需要在合并过程中更新进度条,避免界面卡死。
阅读全文