-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
203 lines (169 loc) · 7.74 KB
/
app.py
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QLineEdit, QPushButton
from PyQt5.QtWidgets import QWidget, QVBoxLayout, QHBoxLayout, QMessageBox, QFileDialog, QCheckBox
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QFont
from main import add_lines_to_pdf # Assuming add_lines_to_pdf function is in main.py
class PDFInsertionApp(QMainWindow):
def __init__(self):
super().__init__()
self.input_pdf_label = None
self.input_pdf_edit = None
self.input_pdf_btn = None
self.output_pdf_label = None
self.output_pdf_edit = None
self.lines_file_label = None
self.lines_file_edit = None
self.lines_file_btn = None
self.font_label = None
self.font_edit = None
self.font_size_label = None
self.font_size_edit = None
self.text_height_label = None
self.text_height_edit = None
self.initial_y_label = None
self.initial_y_edit = None
self.left_margin_label = None
self.left_margin_edit = None
self.right_margin_label = None
self.right_margin_edit = None
self.ignore_first_pages_label = None
self.ignore_first_pages_edit = None
self.ignore_last_pages_label = None
self.ignore_last_pages_edit = None
self.shuffle_lines_checkbox = None
self.submit_btn = None
self.pos_x = 500
self.pos_y = 300
self.screen_width = 450
self.screen_height = 400
self.setWindowTitle("PDF Text Insertion App")
self.setGeometry(self.pos_x, self.pos_y, self.screen_width, self.screen_height)
self.initUI()
def initUI(self):
# Create widgets
self.input_pdf_label = QLabel("Input PDF Path:")
self.input_pdf_edit = QLineEdit()
self.input_pdf_edit.setMaximumWidth(325)
self.input_pdf_btn = QPushButton("Browse")
self.input_pdf_btn.clicked.connect(self.browse_input_pdf)
self.output_pdf_label = QLabel("Output PDF Name:")
self.output_pdf_edit = QLineEdit("final_pdf")
self.output_pdf_edit.setMaximumWidth(325)
self.lines_file_label = QLabel("Lines File Path:")
self.lines_file_edit = QLineEdit("assets/lines.csv")
self.lines_file_edit.setMaximumWidth(325)
self.lines_file_btn = QPushButton("Browse")
self.lines_file_btn.clicked.connect(self.browse_lines_file)
self.font_label = QLabel("Font Name in the System:")
self.font_edit = QLineEdit("Helvetica-Oblique")
self.font_size_label = QLabel("Font Size:")
self.font_size_edit = QLineEdit("12")
self.text_height_label = QLabel("Text Height:")
self.text_height_edit = QLineEdit("25")
self.initial_y_label = QLabel("Initial Y:")
self.initial_y_edit = QLineEdit("46")
self.left_margin_label = QLabel("Left Margin:")
self.left_margin_edit = QLineEdit("40")
self.right_margin_label = QLabel("Right Margin:")
self.right_margin_edit = QLineEdit("40")
self.ignore_first_pages_label = QLabel("Ignore First Pages:")
self.ignore_first_pages_edit = QLineEdit("2")
self.ignore_last_pages_label = QLabel("Ignore Last Pages:")
self.ignore_last_pages_edit = QLineEdit("0")
self.shuffle_lines_checkbox = QCheckBox("Shuffle Lines", checked=True)
self.submit_btn = QPushButton("Generate PDF")
self.submit_btn.clicked.connect(self.generate_pdf)
# Layout
central_widget = QWidget()
self.setCentralWidget(central_widget)
vbox = QVBoxLayout()
vbox.addWidget(self.input_pdf_label)
hbox = QHBoxLayout()
hbox.addWidget(self.input_pdf_edit)
hbox.addWidget(self.input_pdf_btn)
vbox.addLayout(hbox)
vbox.addWidget(self.output_pdf_label)
vbox.addWidget(self.output_pdf_edit)
vbox.addWidget(self.lines_file_label)
hbox = QHBoxLayout()
hbox.addWidget(self.lines_file_edit)
hbox.addWidget(self.lines_file_btn)
vbox.addLayout(hbox)
vbox.addWidget(self.font_label)
vbox.addWidget(self.font_edit)
hbox = QHBoxLayout()
hbox.addWidget(self.font_size_label)
hbox.addWidget(self.font_size_edit)
hbox.addWidget(self.text_height_label)
hbox.addWidget(self.text_height_edit)
vbox.addLayout(hbox)
hbox = QHBoxLayout()
hbox.addWidget(self.initial_y_label)
hbox.addWidget(self.initial_y_edit)
hbox.addWidget(self.left_margin_label)
hbox.addWidget(self.left_margin_edit)
hbox.addWidget(self.right_margin_label)
hbox.addWidget(self.right_margin_edit)
vbox.addLayout(hbox)
hbox = QHBoxLayout()
hbox.addWidget(self.ignore_first_pages_label)
hbox.addWidget(self.ignore_first_pages_edit)
hbox.addWidget(self.ignore_last_pages_label)
hbox.addWidget(self.ignore_last_pages_edit)
vbox.addLayout(hbox)
vbox.addWidget(self.shuffle_lines_checkbox)
vbox.addWidget(self.submit_btn)
central_widget.setLayout(vbox)
def browse_input_pdf(self):
options = QFileDialog.Options()
options |= QFileDialog.DontUseNativeDialog
file_filter = "PDF File (*.pdf)"
filename, _ = QFileDialog.getOpenFileName(self, "Select Input PDF File", "", file_filter, options=options)
if filename:
self.input_pdf_edit.setText(filename)
def browse_lines_file(self):
options = QFileDialog.Options()
options |= QFileDialog.DontUseNativeDialog
file_filter = "Text File (*.txt *.csv *.xlsx)"
filename, _ = QFileDialog.getOpenFileName(self, "Select Lines File", "", file_filter, options=options)
if filename:
self.lines_file_edit.setText(filename)
def generate_pdf(self):
input_pdf_path = self.input_pdf_edit.text().strip()
output_pdf_name = self.output_pdf_edit.text().strip()
lines_file_path = self.lines_file_edit.text().strip()
font = self.font_edit.text().strip()
font_size = int(self.font_size_edit.text().strip())
text_height = int(self.text_height_edit.text().strip())
initial_y = int(self.initial_y_edit.text().strip())
left_margin = int(self.left_margin_edit.text().strip())
right_margin = int(self.right_margin_edit.text().strip())
ignore_first_pages = int(self.ignore_first_pages_edit.text().strip())
ignore_last_pages = int(self.ignore_last_pages_edit.text().strip())
shuffle_lines = self.shuffle_lines_checkbox.isChecked()
# Process output_pdf_path to be "output/{USER_INPUT}.pdf"
if not output_pdf_name.endswith(".pdf"):
output_pdf_name += ".pdf"
output_pdf_path = f"output/{output_pdf_name}"
try:
add_lines_to_pdf(input_pdf_path=input_pdf_path,
output_pdf_path=output_pdf_path,
lines_file_path=lines_file_path,
font_size=font_size,
left_margin=left_margin,
right_margin=right_margin,
text_height=text_height,
initial_y=initial_y,
font=font,
shuffle_lines=shuffle_lines,
ignore_first_pages=ignore_first_pages,
ignore_last_pages=ignore_last_pages)
QMessageBox.information(self, "Success", "PDF generated successfully.")
except Exception as e:
QMessageBox.critical(self, "Error", f"Error generating PDF: {str(e)}")
if __name__ == "__main__":
app = QApplication(sys.argv)
window = PDFInsertionApp()
window.show()
sys.exit(app.exec_())