迁移 单一题号转为图片

This commit is contained in:
weiye.wang 2024-04-25 20:23:58 +08:00
parent 01329bae69
commit a477a7309a
4 changed files with 260 additions and 3 deletions

View File

@ -0,0 +1,65 @@
# -*- coding: utf-8 -*-
################################################################################
## Form generated from reading UI file '单一题号转为图片.ui'
##
## Created by: Qt User Interface Compiler version 6.6.2
##
## WARNING! All changes made in this file will be lost when recompiling UI file!
################################################################################
from PySide6.QtCore import (QCoreApplication, QDate, QDateTime, QLocale,
QMetaObject, QObject, QPoint, QRect,
QSize, QTime, QUrl, Qt)
from PySide6.QtGui import (QBrush, QColor, QConicalGradient, QCursor,
QFont, QFontDatabase, QGradient, QIcon,
QImage, QKeySequence, QLinearGradient, QPainter,
QPalette, QPixmap, QRadialGradient, QTransform)
from PySide6.QtWidgets import (QApplication, QLabel, QLineEdit, QPushButton,
QSizePolicy, QWidget)
class Ui_Form(object):
def setupUi(self, Form):
if not Form.objectName():
Form.setObjectName(u"Form")
Form.resize(760, 490)
self.label = QLabel(Form)
self.label.setObjectName(u"label")
self.label.setGeometry(QRect(240, 160, 31, 16))
self.lineEdit_ID = QLineEdit(Form)
self.lineEdit_ID.setObjectName(u"lineEdit_ID")
self.lineEdit_ID.setGeometry(QRect(270, 160, 71, 20))
self.label_2 = QLabel(Form)
self.label_2.setObjectName(u"label_2")
self.label_2.setGeometry(QRect(240, 190, 31, 16))
self.lineEdit_dpi = QLineEdit(Form)
self.lineEdit_dpi.setObjectName(u"lineEdit_dpi")
self.lineEdit_dpi.setGeometry(QRect(270, 190, 71, 20))
self.pushButton_convert = QPushButton(Form)
self.pushButton_convert.setObjectName(u"pushButton_convert")
self.pushButton_convert.setGeometry(QRect(350, 160, 75, 51))
self.pushButton_open = QPushButton(Form)
self.pushButton_open.setObjectName(u"pushButton_open")
self.pushButton_open.setGeometry(QRect(430, 160, 75, 51))
self.label_3 = QLabel(Form)
self.label_3.setObjectName(u"label_3")
self.label_3.setGeometry(QRect(240, 220, 51, 16))
self.lineEdit_message = QLineEdit(Form)
self.lineEdit_message.setObjectName(u"lineEdit_message")
self.lineEdit_message.setGeometry(QRect(300, 220, 201, 20))
self.retranslateUi(Form)
QMetaObject.connectSlotsByName(Form)
# setupUi
def retranslateUi(self, Form):
Form.setWindowTitle(QCoreApplication.translate("Form", u"\u5355\u4e00\u9898\u53f7\u8f6c\u4e3a\u56fe\u7247", None))
self.label.setText(QCoreApplication.translate("Form", u"\u9898\u53f7", None))
self.label_2.setText(QCoreApplication.translate("Form", u"DPI", None))
self.pushButton_convert.setText(QCoreApplication.translate("Form", u"\u8f6c\u6362", None))
self.pushButton_open.setText(QCoreApplication.translate("Form", u"\u6253\u5f00", None))
self.label_3.setText(QCoreApplication.translate("Form", u"\u9644\u52a0\u6587\u5b57", None))
self.lineEdit_message.setPlaceholderText(QCoreApplication.translate("Form", u"\u8f93\u5165\u9644\u52a0\u6587\u5b57, \u7559\u7a7a\u8868\u793a\u65e0\u9644\u52a0\u6587\u5b57", None))
# retranslateUi

View File

@ -8,6 +8,7 @@ from 系列讲义生成 import MyWindow_jysc
from 讲义结构与内容录入 import MyWindow_jglr
from 答题纸对应 import MyWindow_dtlr
from 获取题号 import MyWindow_hqth
from 单一题号转为图片 import MyWindow_sctp
from database_tools_2 import *
class MyWindow(QMainWindow,Ui_MainWindow):
@ -22,14 +23,18 @@ class MyWindow(QMainWindow,Ui_MainWindow):
self.database = "tikutest"
self.pushButton_tiku.clicked.connect(self.SelectTIKU)
self.pushButton_tikutest.clicked.connect(self.SelectTIKUTEST) #以上为设置当前数据库的两个按钮
self.layout_sctp = QVBoxLayout(self.tab_sctp)
self.sctp = MyWindow_sctp(self.database)
self.layout_sctp.addWidget(self.sctp) #以上三行为初始化“生成图片”tab
self.layout_kxth = QVBoxLayout(self.tab_kxth)
self.kxth = MyWindow_kxth(self.database)
self.layout_kxth.addWidget(self.kxth) #以上三行为初始化“空闲题号”tab
self.layout_hqth = QVBoxLayout(self.tab_hqth)
self.hqth = MyWindow_hqth()
self.layout_hqth.addWidget(self.hqth) #以上三行为初始化“空闲题号”tab
self.layout_hqth.addWidget(self.hqth) #以上三行为初始化“获取题号”tab
self.layout_jglr = QVBoxLayout(self.tab_jglr)
self.jglr = MyWindow_jglr()
@ -55,7 +60,8 @@ class MyWindow(QMainWindow,Ui_MainWindow):
for func in [
self.kxth.setdbname,
self.jysc.setdbname
self.jysc.setdbname,
self.sctp.setdbname
]: #在列表中的tab里传送数据库名的连接
self.sendDBname.connect(func)
def sendValue(self):

View File

@ -0,0 +1,69 @@
from PySide6.QtWidgets import QWidget, QApplication, QFileDialog
from Ui_单一题号转为图片 import Ui_Form
from database_tools_2 import *
import os
from datetime import datetime
class MyWindow_sctp(QWidget,Ui_Form):
def __init__(self,database_name):
super().__init__()
self.database_name = database_name
self.setupUi(self)
self.bind()
def setdbname(self,string):
self.database_name = string
print(self.database_name)
def bind(self):
self.pushButton_open.setDisabled(True)
self.pushButton_convert.clicked.connect(self.convert)
self.pushButton_open.clicked.connect(self.open)
self.lineEdit_ID.textChanged.connect(self.toggleopen)
self.lineEdit_dpi.textChanged.connect(self.toggleopen)
def convert(self):
self.time = datetime.now().strftime("%Y%m%d%H%M%S")
self.id = self.lineEdit_ID.text()
self.dpi = self.lineEdit_dpi.text()
mydb = connect(hostname = db_host, port = db_port, username=db_user, pwd=db_pwd, db = self.database_name)
mycursor = mydb.cursor()
sql = "SELECT content FROM problems WHERE ID = %s;"
val = (self.id.strip().zfill(6),)
mycursor.execute(sql,val)
content = mycursor.fetchall()[0][0]
mydb.close()
prefix = self.lineEdit_message.text().strip()
latexcontent = f"{prefix} {content}"
latex_raw = ReadTextFile("模板文件/独立文件模板.txt")
#识别操作系统
if sys.platform != "win32":
latex_raw = re.sub(r"fontset[\s]*=[\s]*none","fontset = fandol",latex_raw)
latex_raw = re.sub(r"\\setCJKmainfont",r"% \\setCJKmainfont",latex_raw)
latexdata = StringSubstitute(r"<<待替换\d*>>",latex_raw,[latexcontent])
makedir("临时文件/pics")
SaveTextFile(latexdata,f"临时文件/pics/ID{self.id}-{self.dpi}-{self.time}.tex")
os.system(f"xelatex -interaction=batchmode -output-directory=临时文件/pics ID{self.id}-{self.dpi}-{self.time}.tex")
os.chdir("临时文件/pics")
os.system(f"pdftocairo -png -r {self.dpi} ID{self.id}-{self.dpi}-{self.time}.pdf")
os.chdir("../..")
self.pushButton_open.setEnabled(True)
def open(self):
startfile(os.path.join("临时文件/pics", f"ID{self.id}-{self.dpi}-{self.time}-1.png"))
def toggleopen(self):
self.pushButton_open.setDisabled(True)
if __name__ == '__main__':
app = QApplication([])
windows = MyWindow()
windows.show()
app.exec()

View File

@ -0,0 +1,117 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Form</class>
<widget class="QWidget" name="Form">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>760</width>
<height>490</height>
</rect>
</property>
<property name="windowTitle">
<string>单一题号转为图片</string>
</property>
<widget class="QLabel" name="label">
<property name="geometry">
<rect>
<x>240</x>
<y>160</y>
<width>31</width>
<height>16</height>
</rect>
</property>
<property name="text">
<string>题号</string>
</property>
</widget>
<widget class="QLineEdit" name="lineEdit_ID">
<property name="geometry">
<rect>
<x>270</x>
<y>160</y>
<width>71</width>
<height>20</height>
</rect>
</property>
</widget>
<widget class="QLabel" name="label_2">
<property name="geometry">
<rect>
<x>240</x>
<y>190</y>
<width>31</width>
<height>16</height>
</rect>
</property>
<property name="text">
<string>DPI</string>
</property>
</widget>
<widget class="QLineEdit" name="lineEdit_dpi">
<property name="geometry">
<rect>
<x>270</x>
<y>190</y>
<width>71</width>
<height>20</height>
</rect>
</property>
</widget>
<widget class="QPushButton" name="pushButton_convert">
<property name="geometry">
<rect>
<x>350</x>
<y>160</y>
<width>75</width>
<height>51</height>
</rect>
</property>
<property name="text">
<string>转换</string>
</property>
</widget>
<widget class="QPushButton" name="pushButton_open">
<property name="geometry">
<rect>
<x>430</x>
<y>160</y>
<width>75</width>
<height>51</height>
</rect>
</property>
<property name="text">
<string>打开</string>
</property>
</widget>
<widget class="QLabel" name="label_3">
<property name="geometry">
<rect>
<x>240</x>
<y>220</y>
<width>51</width>
<height>16</height>
</rect>
</property>
<property name="text">
<string>附加文字</string>
</property>
</widget>
<widget class="QLineEdit" name="lineEdit_message">
<property name="geometry">
<rect>
<x>300</x>
<y>220</y>
<width>201</width>
<height>20</height>
</rect>
</property>
<property name="placeholderText">
<string>输入附加文字, 留空表示无附加文字</string>
</property>
</widget>
</widget>
<resources/>
<connections/>
</ui>