其实应用 pangu 做文本格式标准化的业务代码在之前就实现了,次要可能将中文文本文档中的文字、标点符号等进行标准化。
浏览全文
然而为了不便起来咱们这里应用了 Qt5 将其做成了一个能够操作的页面利用,这样不相熟 python 的敌人就能够不必写代码间接双击运行应用就 OK 了。
为了使文本格式的丑化过程不影响主线程的应用,顺便采纳 QThread 子线程来专门的运行文本文档丑化的业务过程,接下来还是采纳 pip 的形式将所有须要的非标准模块装置一下。
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pangu
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple PyQt5
将咱们应用到的 pyqt5 利用制作模块以及业务模块 pangu 导入到咱们的代码块中。
# It imports all the classes, attributes, and methods of the PyQt5.QtCore module into the global symbol table.
from PyQt5.QtCore import *
# It imports all the classes, attributes, and methods of the PyQt5.QtWidgets module into the global symbol table.
from PyQt5.QtWidgets import QApplication, QWidget, QHBoxLayout, QVBoxLayout, QTextBrowser, QLineEdit, QPushButton, \
QFormLayout, QFileDialog
# It imports all the classes, attributes, and methods of the PyQt5.QtGui module into the global symbol table.
from PyQt5.QtGui import QIcon, QFont, QTextCursor
# It imports the pangu module.
import pangu
# It imports the sys module.
import sys
# It imports the os module.
import os
为了缩小 python 模块在打包时资源占用过多,打的 exe 应用程序的占用空间过大的状况,这次咱们只导入了可能应用到的相干 python 类,这个小细节大家留神一下。
上面创立一个名称为 PanGuUI 的 python 类来实现对整个利用页面的开发,将页面的布局以及组件相干的局部写到这个类中。并且给页面组件绑定好相应的槽函数从而实现页面的 ’ 点击 ’ 等性能。
# It creates a class called PanGuUI that inherits from QWidget.
class PanGuUI(QWidget):
def __init__(self):
"""
A constructor. It is called when an object is created from a class and it allows the class to initialize the
attributes of a class.
"""
super(PanGuUI, self).__init__()
self.init_ui()
def init_ui(self):
"""This function initializes the UI."""
self.setWindowTitle('文本文档丑化器 公众号:Python 集中营')
self.setWindowIcon(QIcon('txt.ico'))
self.brower = QTextBrowser()
self.brower.setFont(QFont('宋体', 8))
self.brower.setReadOnly(True)
self.brower.setPlaceholderText('解决过程展现区域...')
self.brower.ensureCursorVisible()
self.txt_file_path = QLineEdit()
self.txt_file_path.setPlaceholderText('源文本文档门路')
self.txt_file_path.setReadOnly(True)
self.txt_file_path_btn = QPushButton()
self.txt_file_path_btn.setText('导入')
self.txt_file_path_btn.clicked.connect(self.txt_file_path_btn_click)
self.new_txt_file_path = QLineEdit()
self.new_txt_file_path.setPlaceholderText('新文本文档门路')
self.new_txt_file_path.setReadOnly(True)
self.new_txt_file_path_btn = QPushButton()
self.new_txt_file_path_btn.setText('门路')
self.new_txt_file_path_btn.clicked.connect(self.new_txt_file_path_btn_click)
self.start_btn = QPushButton()
self.start_btn.setText('开始导入')
self.start_btn.clicked.connect(self.start_btn_click)
hbox = QHBoxLayout()
hbox.addWidget(self.brower)
fbox = QFormLayout()
fbox.addRow(self.txt_file_path, self.txt_file_path_btn)
fbox.addRow(self.new_txt_file_path, self.new_txt_file_path_btn)
v_vbox = QVBoxLayout()
v_vbox.addWidget(self.start_btn)
vbox = QVBoxLayout()
vbox.addLayout(fbox)
vbox.addLayout(v_vbox)
hbox.addLayout(vbox)
self.thread_ = PanGuThread(self)
self.thread_.message.connect(self.show_message)
self.thread_.finished.connect(self.finshed)
self.setLayout(hbox)
def show_message(self, text):
"""
It shows a message
:param text: The text to be displayed
"""
cursor = self.brower.textCursor()
cursor.movePosition(QTextCursor.End)
self.brower.append(text)
self.brower.setTextCursor(cursor)
self.brower.ensureCursorVisible()
def txt_file_path_btn_click(self):
"""It opens a file dialog box and allows the user to select a file."""
txt_file = QFileDialog.getOpenFileName(self, os.getcwd(), '关上文本文档',
'Text File(*.txt)')
self.txt_file_path.setText(txt_file[0])
def new_txt_file_path_btn_click(self):
"""This function opens a file dialog box and allows the user to select a file to save the output to."""
new_txt_file = QFileDialog.getOpenFileName(self, os.getcwd(), '关上文本文档',
'Text File(*.txt)')
self.new_txt_file_path.setText(new_txt_file[0])
def start_btn_click(self):
"""A function that is called when the start button is clicked."""
self.thread_.start()
self.start_btn.setEnabled(False)
def finshed(self, finished):
""":param finished: A boolean value that is True if the download is finished, False otherwise"""
if finished is True:
self.start_btn.setEnabled(True)
创立名称为 PanGuThread 的子线程,将具体实现丑化格式化文本字符串的业务代码块写入到子线程中。子线程继承的是 QThread 的 PyQt5 的线程类,通过创立子线程并且将子线程的信号信息传递到主线程中,在主线程的文本浏览器中进行展现达到实时跟踪执行后果的成果。
# This class is a subclass of QThread, and it's used to split the text into words
class PanGuThread(QThread):
message = pyqtSignal(str)
finished = pyqtSignal(bool)
def __init__(self, parent=None):
"""
A constructor that initializes the class.
:param parent: The parent widget
"""
super(PanGuThread, self).__init__(parent)
self.working = True
self.parent = parent
def __del__(self):
"""A destructor. It is called when the object is destroyed."""
self.working = True
self.wait()
def run(self) -> None:
"""> This function runs the program"""
try:
txt_file_path = self.parent.txt_file_path.text().strip()
self.message.emit('源文件门路信息读取失常!')
new_txt_file_path = self.parent.new_txt_file_path.text().strip()
self.message.emit('新文件门路信息读取失常!')
list_ = []
with open(txt_file_path, encoding='utf-8') as f:
lines_ = f.readlines()
self.message.emit('源文件内容读取实现!')
n = 1
for line_ in lines_:
text = pangu.spacing_text(line_)
self.message.emit('第 {0} 行文档内容格式化实现!'.format(n))
list_.append(text)
n = n + 1
self.message.emit('源文件门路信息格式化实现!')
self.message.emit('行将开始将格式化内容写入新文件!')
with open(new_txt_file_path, 'a') as f:
for line_ in list_:
f.write(line_ + '\n')
self.message.emit('新文件内容写入实现!')
self.finished.emit(True)
except Exception as e:
self.message.emit('文件内容读取或格式化产生异样!')
if __name__ == '__main__':
app = QApplication(sys.argv)
main = PanGuUI()
main.show()
sys.exit(app.exec_())
实现了开发开始测试一下成果如何,创立了两个文本文件 data.txt、new_data.txt,点击 ’ 开始运行 ’ 之后会调起整个的业务子线程实现文本格式化,后果完满运行来看一下执行过程展现。
【往期精彩】
pyqt5 利用的主题款式!
GUI 利用:socket 网络聊天室!
小王,给这 2000 个客户发一下节日祝愿的邮件 …