-
Notifications
You must be signed in to change notification settings - Fork 1
/
gui.py
152 lines (120 loc) · 4.32 KB
/
gui.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
from PyQt6.QtWidgets import QFileDialog, QWidget, QGridLayout, QLineEdit, QPushButton, QLabel, QProgressBar
from pathlib import Path
import qdarktheme
from PyQt6.QtCore import Qt
import os
#To find if user is on dark theme
import darkdetect
from converter import Converter
class MainWindow(QWidget):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# Path to File
self.path = ""
self.progress = 0
self.pages_to_be_skipped = []
self.setWindowTitle('Result Converter')
self.setGeometry(100, 100, 900, 210)
# Dark Mode detection
if darkdetect.isDark():
qdarktheme.setup_theme()
else:
qdarktheme.setup_theme("light")
# layout - Grid
layout = QGridLayout()
self.setLayout(layout)
# file selection Button
file_browse = QPushButton('Browse')
# Connect to file selector
file_browse.clicked.connect(self.open_file_dialog)
# Save Button
file_save = QPushButton('Submit',self)
file_save.clicked.connect(self.submit)
# Skip Pages text
skip_text = QLabel("<h3>Skip:</h3>")
skip_text.setAlignment(Qt.AlignmentFlag.AlignCenter)
self.skipped_pages = QLineEdit()
# Label To show file
Name_Label=QLabel("<h3>File:</h3>")
# Align file to center of grid box
Name_Label.setAlignment(Qt.AlignmentFlag.AlignCenter)
# file_name_location
self.filename_edit = QLineEdit()
# self.filename_edit.setText(str(self.path))
# Progress bar
self.progressbar = QProgressBar()
# Info Text
self.info_text = QLabel()
self.give_info("Select Pdf File To Convert.")
# Error text
self.error_text = QLabel()
self.give_error("Enter Pages to be skipped in a comma seperated format. For ex: 1,2,3,4")
# Add all widgets
layout.addWidget(Name_Label, 0, 0)
layout.addWidget(skip_text, 1, 0)
layout.addWidget(self.filename_edit, 0, 1, 1, 5)
layout.addWidget(self.skipped_pages, 1, 1, 1, 5)
layout.addWidget(file_browse, 0, 8)
layout.addWidget(file_save, 2, 3)
layout.addWidget(self.progressbar, 3, 2, 2, 3)
layout.addWidget(self.info_text, 4, 2, 4, 3)
layout.addWidget(self.error_text, 6, 2, 4, 3)
# Show everything
self.show()
pass
def give_info(self, text):
self.info_text.setText(f'<center>{text}</center>')
pass
def give_error(self, text):
self.error_text.setText(f'<b><center>{text}</center><b>')
pass
def print_pages(self):
k = self.skipped_pages.text()
if len(k) == 0:
self.pages_to_be_skipped = []
return
self.pages_to_be_skipped = list(map(int, k.split(',')))
def set_progress(self, val):
if val >= self.progressbar.maximum():
val = self.progressbar.maximum()
self.progressbar.setValue(val)
pass
def submit(self):
if self.path == '':
self.give_info("No Path Provided")
return
self.print_pages()
# Converter object
csvpath = f'{os.getcwd()}/output.csv'
converter = Converter(self.path, csvpath)
n = converter.n_pages
self.progressbar.setMinimum(0)
self.progressbar.setMaximum(n)
self.progressbar.setRange(0,n)
# Convert page by page
for i in range(n):
if (i+1) in self.pages_to_be_skipped:
continue
self.progressbar.setValue(i+1)
converter.step(i)
# Write to csv file
converter.write()
error = converter.report_errors()
self.give_error(error)
if '!!!' in error:
self.give_info('')
return
info = f'Saved file {csvpath}'
info += f'\n<b>Manually Skipped Pages: {self.pages_to_be_skipped}<b>'
self.give_info(info)
pass
def open_file_dialog(self):
filename, filetype = QFileDialog.getOpenFileName(self,
"Select a File",
"", # Current directory if ""
"pdf files (*.pdf)"
)
if filename:
self.path = Path(filename)
self.filename_edit.setText(str(self.path))
pass