Python利用pangu模塊實(shí)現(xiàn)文本格式化小工具
其實(shí)使用pangu做文本格式標(biāo)準(zhǔn)化的業(yè)務(wù)代碼在之前就實(shí)現(xiàn)了,主要能夠?qū)⒅形奈谋疚臋n中的文字、標(biāo)點(diǎn)符號(hào)等進(jìn)行標(biāo)準(zhǔn)化。
但是為了方便起來(lái)我們這里使用了Qt5將其做成了一個(gè)可以操作的頁(yè)面應(yīng)用,這樣不熟悉python的朋友就可以不用寫代碼直接雙擊運(yùn)行使用就OK了。

為了使文本格式的美化過(guò)程不影響主線程的使用,特地采用QThread子線程來(lái)專門的運(yùn)行文本文檔美化的業(yè)務(wù)過(guò)程,接下來(lái)還是采用pip的方式將所有需要的非標(biāo)準(zhǔn)模塊安裝一下。
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pangu pip install -i https://pypi.tuna.tsinghua.edu.cn/simple PyQt5
將我們使用到的pyqt5應(yīng)用制作模塊以及業(yè)務(wù)模塊pangu導(dǎo)入到我們的代碼塊中。
# 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模塊在打包時(shí)資源占用過(guò)多,打的exe應(yīng)用程序的占用空間過(guò)大的情況,這次我們只導(dǎo)入了能夠使用到的相關(guān)python類,這個(gè)小細(xì)節(jié)大家注意一下。
下面創(chuàng)建一個(gè)名稱為PanGuUI的python類來(lái)實(shí)現(xiàn)對(duì)整個(gè)應(yīng)用頁(yè)面的開(kāi)發(fā),將頁(yè)面的布局以及組件相關(guān)的部分寫到這個(gè)類中。并且給頁(yè)面組件綁定好相應(yīng)的槽函數(shù)從而實(shí)現(xiàn)頁(yè)面的'點(diǎn)擊'等功能。
# 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('文本文檔美化器 公眾號(hào):Python 集中營(yíng)')
self.setWindowIcon(QIcon('txt.ico'))
self.brower = QTextBrowser()
self.brower.setFont(QFont('宋體', 8))
self.brower.setReadOnly(True)
self.brower.setPlaceholderText('處理進(jìn)程展示區(qū)域...')
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('導(dǎo)入')
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('開(kāi)始導(dǎo)入')
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(), '打開(kāi)文本文檔',
'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(), '打開(kāi)文本文檔',
'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)
創(chuàng)建名稱為PanGuThread的子線程,將具體實(shí)現(xiàn)美化格式化文本字符串的業(yè)務(wù)代碼塊寫入到子線程中。子線程繼承的是QThread的PyQt5的線程類,通過(guò)創(chuàng)建子線程并且將子線程的信號(hào)信息傳遞到主線程中,在主線程的文本瀏覽器中進(jìn)行展示達(dá)到實(shí)時(shí)跟蹤執(zhí)行結(jié)果的效果。
# 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èi)容讀取完成!')
n = 1
for line_ in lines_:
text = pangu.spacing_text(line_)
self.message.emit('第{0}行文檔內(nèi)容格式化完成!'.format(n))
list_.append(text)
n = n + 1
self.message.emit('源文件路徑信息格式化完成!')
self.message.emit('即將開(kāi)始將格式化內(nèi)容寫入新文件!')
with open(new_txt_file_path, 'a') as f:
for line_ in list_:
f.write(line_ + '\n')
self.message.emit('新文件內(nèi)容寫入完成!')
self.finished.emit(True)
except Exception as e:
self.message.emit('文件內(nèi)容讀取或格式化發(fā)生異常!')
if __name__ == '__main__':
app = QApplication(sys.argv)
main = PanGuUI()
main.show()
sys.exit(app.exec_())
完成了開(kāi)發(fā)開(kāi)始測(cè)試一下效果如何,創(chuàng)建了兩個(gè)文本文件data.txt、new_data.txt,點(diǎn)擊'開(kāi)始運(yùn)行'之后會(huì)調(diào)起整個(gè)的業(yè)務(wù)子線程實(shí)現(xiàn)文本格式化,結(jié)果完美運(yùn)行來(lái)看一下執(zhí)行過(guò)程展示。

到此這篇關(guān)于Python利用pangu模塊實(shí)現(xiàn)文本格式化小工具的文章就介紹到這了,更多相關(guān)Python pangu文本格式化內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python使用LSTM實(shí)現(xiàn)銷售額預(yù)測(cè)詳解
大家經(jīng)常會(huì)遇到一些需要預(yù)測(cè)的場(chǎng)景,比如預(yù)測(cè)品牌銷售額,預(yù)測(cè)產(chǎn)品銷量。本文給大家分享一波使用?LSTM?進(jìn)行端到端時(shí)間序列預(yù)測(cè)的完整代碼和詳細(xì)解釋,需要的可以參考一下2022-07-07
Python實(shí)現(xiàn)檢測(cè)照片中的人臉數(shù)
這篇文章主要為大家詳細(xì)介紹了如何利用Python語(yǔ)言實(shí)現(xiàn)檢測(cè)照片中共有多少?gòu)埲四?,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2022-08-08
Python實(shí)現(xiàn)完整的事務(wù)操作示例
這篇文章主要介紹了Python實(shí)現(xiàn)完整的事務(wù)操作,結(jié)合實(shí)例形式分析了Python操作mysql數(shù)據(jù)庫(kù)相關(guān)事務(wù)操作的具體流程與實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-06-06
Python實(shí)現(xiàn)報(bào)警信息實(shí)時(shí)發(fā)送至郵箱功能(實(shí)例代碼)
這篇文章主要介紹了Python實(shí)現(xiàn)報(bào)警信息實(shí)時(shí)發(fā)送至郵箱,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-11-11
一文詳細(xì)介紹Python中的OrderedDict對(duì)象
OrderedDict是Python標(biāo)準(zhǔn)庫(kù)collections模塊的一部分,下面這篇文章主要給大家介紹了關(guān)于Python中OrderedDict對(duì)象的相關(guān)資料,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-08-08
python中列表(list)和元組(tuple)的深入講解
這篇文章主要給大家介紹了關(guān)于python中列表(list)和元組(tuple)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
Python異常處理知識(shí)點(diǎn)總結(jié)
在本篇文章中小編給大家分享了關(guān)于Python異常處理的相關(guān)知識(shí)點(diǎn)以及對(duì)應(yīng)的實(shí)例內(nèi)容,需要的朋友們學(xué)習(xí)下。2019-02-02
Python3.5面向?qū)ο笈c繼承圖文實(shí)例詳解
這篇文章主要介紹了Python3.5面向?qū)ο笈c繼承,結(jié)合圖文與實(shí)例形式詳細(xì)分析了Python3.5面向?qū)ο笈c繼承的相關(guān)概念、原理、實(shí)現(xiàn)方法及操作注意事項(xiàng),需要的朋友可以參考下2019-04-04
Python調(diào)用graphviz繪制結(jié)構(gòu)化圖形網(wǎng)絡(luò)示例
今天小編就為大家分享一篇Python調(diào)用graphviz繪制結(jié)構(gòu)化圖形網(wǎng)絡(luò)示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-11-11
PyQt5打開(kāi)文件對(duì)話框QFileDialog實(shí)例代碼
這篇文章主要介紹了PyQt5打開(kāi)文件對(duì)話框QFileDialog實(shí)例代碼,分享了相關(guān)代碼示例,小編覺(jué)得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-02-02

