Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
117 lines (84 loc) · 3.12 KB
/
Copy pathmain.py
File metadata and controls
117 lines (84 loc) · 3.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# import os
# import sys
# from PyQt6.QtWidgets import QApplication, QLabel, QWidget, QComboBox
# subjects = [ sub.strip(".docx") for sub in os.listdir("data/") ]
# app = QApplication([])
# window = QWidget()
# lst = QComboBox(parent=window)
# lst.addItems(subjects)
# window.show()
# sys.exit(app.exec())
import os
import sys
from spire.doc import *
from functools import partial
from spire.doc.common import *
from PyQt6.QtWidgets import QApplication, QLabel, QWidget, QComboBox, QPushButton, QLineEdit
class Graphics:
def __init__(self, window):
self.X = 10
self.Y = 40
self.doc = Document()
self.elements = []
self.window = window
def loadFile(self, fileName):
try:
doc = Document()
doc.LoadFromFile(f"data/{fileName}.docx")
self.doc = doc
return fileName
except:
print(fileName)
print("Error! File not found!")
def addButton(self, text, func, x, y):
choose = QPushButton(parent=self.window, text=text)
choose.clicked.connect(func)
choose.move(x, y)
return choose
def addList(self, elmnts, x, y):
combo = QComboBox(parent=self.window)
combo.addItems(elmnts)
combo.move(x, y)
return combo
# doc, fileName = loadFile()
def updateInfo(self):
for label , box in self.elements[:-2]:
self.doc.Replace(f"*{label.text()}*", box.text(), False, False)
self.doc.SaveToFile(f"output/{self.elements[-1][0].text()}.docx", FileFormat.Docx2016)
def updateGUI(self):
pattern = Regex("\*.*?\*")
fields = self.doc.FindAllPattern(pattern)
fields = [ field.SelectedText.strip("*") for field in fields ]
elements = []
for i in range(len(fields)):
box = QLineEdit(parent=self.window)
box.move(self.X, self.Y + 30 * i)
box.show()
label = QLabel(parent=self.window, text=fields[i])
label.move(self.X + box.width() + 10, self.Y + 30 * i)
label.show()
elements.append( (label, box) )
outputName = QLineEdit(parent=self.window)
button = self.addButton("Submit", self.updateInfo, self.X, self.Y + 30 * (len(fields) + 1) )
button.show()
outputName.move(self.X + button.width() + 30, self.Y + 30 * (len(fields) + 1) )
outputName.resize(200, outputName.height() - 5)
outputName.show()
elements.append( (button, button) )
elements.append( (outputName, outputName) )
self.elements = elements
def loadAndUpdate(self, comboBox):
self.loadFile(comboBox.currentText())
self.updateGUI()
name = ""
subjects = [""] + [ sub.strip(".docx") for sub in os.listdir("data/") ]
app = QApplication([])
window = QWidget()
window.setWindowTitle("PyQt App")
window.setGeometry(100, 100, 400, 400)
widget = Graphics(window)
subjects = widget.addList(subjects, 150, 10)
subjects.resize(200, subjects.height())
button = widget.addButton("choose", partial( widget.loadAndUpdate, subjects), 50, 10)
window.show()
sys.exit(app.exec())