问题来源
在收藏文章时,计划用标签方式将文章归类,比如,下面是学习Python时做的标签
希望点击这些标签,能够跳转到对应的锚点,查看收藏的文章,如下
以第一个为例:
标签代码为 [`os.walk()`](#os.walk())
锚点代码为<span id=命名法>`命名法`</span>
超链接代码为[Python下划线与命名规范](https://www.cnblogs.com/yaksea/archive/2011/08/30/2159416.html)
每次手动输入都很麻烦,于是打算写个小程序,直接输入标签、锚点等,自动生成三部分代码。
问题分析
问题很简单,无非就是在"语法外壳"
内填入自己的内容。
用到的知识点
PyQt5按钮、行编辑、标签等元素,信号与槽,UI布局;
剪切板写入。
代码
from PyQt5 import QtCore, QtGui, QtWidgets
#使用pip install pywin32安装下面两个模块
import win32clipboard as w
import win32con
def getText():#没用到
w.OpenClipboard()
d = w.GetClipboardData(win32con.CF_TEXT)
w.CloseClipboard()
#return(d).decode('GBK')
return d
def setText(aString):
w.OpenClipboard()
w.EmptyClipboard()
w.SetClipboardData(win32con.CF_UNICODETEXT, aString)
w.CloseClipboard()
class Ui_Form(object):
catalog=""
anchor=""
link=""
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(552, 250)
self.widget = QtWidgets.QWidget(Form)
self.widget.setGeometry(QtCore.QRect(20, 10, 501, 200))
self.widget.setObjectName("widget")
self.verticalLayout_3 = QtWidgets.QVBoxLayout(self.widget)
self.verticalLayout_3.setContentsMargins(0, 0, 0, 0)
self.verticalLayout_3.setObjectName("verticalLayout_3")
self.horizontalLayout_6 = QtWidgets.QHBoxLayout()
self.horizontalLayout_6.setObjectName("horizontalLayout_6")
self.pushButton_generate = QtWidgets.QPushButton(self.widget)
self.pushButton_generate.setObjectName("pushButton_generate")
self.horizontalLayout_6.addWidget(self.pushButton_generate)
self.verticalLayout = QtWidgets.QVBoxLayout()
self.verticalLayout.setObjectName("verticalLayout")
self.horizontalLayout_2 = QtWidgets.QHBoxLayout()
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.textEdit_keyword = QtWidgets.QTextEdit(self.widget)
self.textEdit_keyword.setObjectName("textEdit_keyword")
self.horizontalLayout_2.addWidget(self.textEdit_keyword)
self.label_3 = QtWidgets.QLabel(self.widget)
self.label_3.setObjectName("label_3")
self.horizontalLayout_2.addWidget(self.label_3)
self.verticalLayout.addLayout(self.horizontalLayout_2)
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setObjectName("horizontalLayout")
self.textEdit_title = QtWidgets.QTextEdit(self.widget)
self.textEdit_title.setObjectName("textEdit_title")
self.horizontalLayout.addWidget(self.textEdit_title)
self.label_2 = QtWidgets.QLabel(self.widget)
self.label_2.setObjectName("label_2")
self.horizontalLayout.addWidget(self.label_2)
self.verticalLayout.addLayout(self.horizontalLayout)
self.horizontalLayout_3 = QtWidgets.QHBoxLayout()
self.horizontalLayout_3.setObjectName("horizontalLayout_3")
self.textEdit_link = QtWidgets.QTextEdit(self.widget)
self.textEdit_link.setObjectName("textEdit_link")
self.horizontalLayout_3.addWidget(self.textEdit_link)
self.label_4 = QtWidgets.QLabel(self.widget)
self.label_4.setObjectName("label_4")
self.horizontalLayout_3.addWidget(self.label_4)
self.verticalLayout.addLayout(self.horizontalLayout_3)
self.horizontalLayout_6.addLayout(self.verticalLayout)
self.verticalLayout_3.addLayout(self.horizontalLayout_6)
self.verticalLayout_2 = QtWidgets.QVBoxLayout()
self.verticalLayout_2.setObjectName("verticalLayout_2")
self.horizontalLayout_7 = QtWidgets.QHBoxLayout()
self.horizontalLayout_7.setObjectName("horizontalLayout_7")
self.pushButton_copycatalog = QtWidgets.QPushButton(self.widget)
self.pushButton_copycatalog.setObjectName("pushButton_copycatalog")
self.horizontalLayout_7.addWidget(self.pushButton_copycatalog)
self.horizontalLayout_4 = QtWidgets.QHBoxLayout()
self.horizontalLayout_4.setObjectName("horizontalLayout_4")
self.textEdit_catalog = QtWidgets.QTextEdit(self.widget)
self.textEdit_catalog.setObjectName("textEdit_catalog")
self.horizontalLayout_4.addWidget(self.textEdit_catalog)
self.label_5 = QtWidgets.QLabel(self.widget)
self.label_5.setObjectName("label_5")
self.horizontalLayout_4.addWidget(self.label_5)
self.horizontalLayout_7.addLayout(self.horizontalLayout_4)
self.verticalLayout_2.addLayout(self.horizontalLayout_7)
self.horizontalLayout_8 = QtWidgets.QHBoxLayout()
self.horizontalLayout_8.setObjectName("horizontalLayout_8")
self.pushButton_copyanchor = QtWidgets.QPushButton(self.widget)
self.pushButton_copyanchor.setObjectName("pushButton_copyanchor")
self.horizontalLayout_8.addWidget(self.pushButton_copyanchor)
self.horizontalLayout_5 = QtWidgets.QHBoxLayout()
self.horizontalLayout_5.setObjectName("horizontalLayout_5")
self.textEdit_anchor = QtWidgets.QTextEdit(self.widget)
self.textEdit_anchor.setObjectName("textEdit_anchor")
self.horizontalLayout_5.addWidget(self.textEdit_anchor)
self.label_6 = QtWidgets.QLabel(self.widget)
self.label_6.setObjectName("label_6")
self.horizontalLayout_5.addWidget(self.label_6)
self.horizontalLayout_8.addLayout(self.horizontalLayout_5)
self.verticalLayout_2.addLayout(self.horizontalLayout_8)
self.horizontalLayout_9 = QtWidgets.QHBoxLayout()
self.horizontalLayout_9.setObjectName("horizontalLayout_9")
self.pushButton_content = QtWidgets.QPushButton(self.widget)
self.pushButton_content.setObjectName("pushButton_content")
self.horizontalLayout_9.addWidget(self.pushButton_content)
self.horizontalLayout_10 = QtWidgets.QHBoxLayout()
self.horizontalLayout_10.setObjectName("horizontalLayout_10")
self.textEdit_content = QtWidgets.QTextEdit(self.widget)
self.textEdit_content.setObjectName("textEdit_content")
self.horizontalLayout_10.addWidget(self.textEdit_content)
self.label_7 = QtWidgets.QLabel(self.widget)
self.label_7.setObjectName("label_7")
self.horizontalLayout_10.addWidget(self.label_7)
self.horizontalLayout_9.addLayout(self.horizontalLayout_10)
self.verticalLayout_2.addLayout(self.horizontalLayout_9)
self.verticalLayout_3.addLayout(self.verticalLayout_2)
self.pushButton_generate.clicked.connect(self.generate)
self.pushButton_copycatalog.clicked.connect(self.copycatalog)
self.pushButton_copyanchor.clicked.connect(self.copyanchor)
self.pushButton_content.clicked.connect(self.copycontent)
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def copycatalog(self):
setText(self.textEdit_catalog.toPlainText())
self.pushButton_copycatalog.setText("已复制")
def copyanchor(self):
setText(self.textEdit_anchor.toPlainText())
self.pushButton_copyanchor.setText("已复制")
def copycontent(self):
setText(self.textEdit_content.toPlainText())
self.pushButton_content.setText("已复制")
def generate(self):
self.catalog="[`{}`](#{}) ".format(self.textEdit_keyword.toPlainText(),self.textEdit_keyword.toPlainText())
self.textEdit_catalog.setPlainText(self.catalog)
self.anchor="<span id={}>`{}`</span>".format(self.textEdit_keyword.toPlainText(),self.textEdit_keyword.toPlainText())
self.textEdit_anchor.setPlainText(self.anchor)
self.link="[{}]({})".format(self.textEdit_title.toPlainText(),self.textEdit_link.toPlainText())
self.textEdit_content.setPlainText(self.link)
self.pushButton_content.setText("复制")
self.pushButton_copyanchor.setText("复制")
self.pushButton_copycatalog.setText("复制")
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Markdown锚点生成"))
self.pushButton_generate.setText(_translate("Form", "生成"))
self.label_3.setText(_translate("Form", "输入关键字"))
self.label_2.setText(_translate("Form", "输入标题"))
self.label_4.setText(_translate("Form", "输入链接"))
self.pushButton_copycatalog.setText(_translate("Form", "复制"))
self.label_5.setText(_translate("Form", "目录"))
self.pushButton_copyanchor.setText(_translate("Form", "复制"))
self.label_6.setText(_translate("Form", "锚点"))
self.pushButton_content.setText(_translate("Form", "复制"))
self.label_7.setText(_translate("Form", "内容"))
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow
if __name__ == '__main__':
app = QApplication(sys.argv)
MainWindow = QMainWindow()
ui = Ui_Form()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
效果
输入关键字
、标题
、链接
后,点击生成
,即可生成后面三条代码。点击前面的复制
,内容便会复制到剪切板上,直接在Markdown编辑器中粘贴即可。
参考
剪切板的操作,参考Python使用Windows剪贴板
打包
用PyQt写界面的一个好处是,打包非常方便,而且可以只生成一个exe文件。
pyinstaller -F -w markdownTool.py