This repository has been archived on 2024-06-23. You can view files and clone it, but cannot push or open issues or pull requests.
mathdeptv2/工具v4/单一题号转为图片.py

70 lines
2.5 KiB
Python

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 AND NOT obsolete;"
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()