forked from etecnos/pdfeditor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
303 lines (251 loc) · 10.5 KB
/
main.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# connect the main python file with all the secondary files
from photo import photo
from split import split
from merge import merge
from merge import execute
from detect import detect, give
from tables import tables
# The Libraries we are going to use
import pygame
import sys
import time
# Initialize Pygame
pygame.init()
# Constants
SCREEN_WIDTH, SCREEN_HEIGHT = 800, 600
WHITE = (255, 255, 255)
pygame_icon = pygame.image.load('data/icon.png')
pygame.display.set_icon(pygame_icon)
# func variable changes according to which action is selected
func = 0
# Start variable helps inside the running loop
start = True
file = ""
times = 0
flag_tables = True
# Create the screen object
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption('PDF editor')
# Load images
image_files = ['data/photo1.png', 'data/photo2.png', 'data/photo3.png', 'data/photo4.png', 'data/photo5.png']
images = [pygame.image.load(image).convert_alpha() for image in image_files]
# Set image positions (arranging them horizontally)
image_positions = [
(50, 300),
(200, 300),
(350, 300),
(500, 300),
(650, 300)
]
# Variable to change based on the image clicked
clicked_image_index = -1
# call_text function is used when we want to display text in pygame's screen
def call_text(tx, wdt):
screen.fill(WHITE)
font = pygame.font.Font(None, 36)
text = font.render(tx, True, (0, 0, 0))
screen.blit(text, (SCREEN_WIDTH // 2 - wdt, 200))
# call_text_lines use is the same with the previous function, but it is used if only a specific action is completed(you can see it below)
def call_text_lines(tx):
font = pygame.font.Font(None, 28)
text = font.render(tx, True, (0, 0, 0))
screen.blit(text, (SCREEN_WIDTH // 50, 250))
# this function checks whether any of the given images is selected(image = action)
def check_image_click(pos):
global clicked_image_index
for idx, (image, img_pos) in enumerate(zip(images, image_positions)):
rect = pygame.Rect(img_pos[0], img_pos[1], image.get_width(), image.get_height())
if rect.collidepoint(pos):
# The variable below is used to hold the value of the selected action
clicked_image_index = idx + 1
# Again loop is used for the program to restart after performing an action
again = True
while again:
# Main game loop
running = True
while running:
screen.fill(WHITE)
# Draw the images
if start:
call_text("Select an action:", 100)
for image, pos in zip(images, image_positions):
screen.blit(image, pos)
# This if is checking whether an action has ended(start = false). In this case the right text is displayed
if not start:
if func == 1:
call_text("PDF is created!", 100)
if func == 2:
call_text("PDF files are created", 120)
if func == 3:
call_text("Files successfully merged(directory of the program)", 310)
if func == 4:
call_text("This word was detected " + str(times) + " times inside the PDF file", 300)
if times > 0:
call_text_lines(
"Find the exact lines the word was detected inside lines.txt(directory of the program)")
if func == 5:
if flag_tables == True:
call_text("No tables found!", 100)
else:
call_text("Tables are extracted inside tables.txt(directory of the program)", 370)
pygame.display.update()
# Wait 3 seconds and then restart the program
time.sleep(3)
running = True
start = True
break
# Event handling
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
again = False
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1 and start: # Left click
mouse_pos = pygame.mouse.get_pos()
check_image_click(mouse_pos)
# Case one(image 1)
if clicked_image_index == 1:
func = 1
call_text("Select an Image", 100)
pygame.display.flip()
photo()
clicked_image_index = -1
start = False
# Case two(image 2)
if clicked_image_index == 2:
func = 2
call_text("Select a PDF file", 90)
pygame.display.flip()
split()
clicked_image_index = -1
start = False
# Case 3(image 3)
if clicked_image_index == 3:
func = 3
width, height = 800, 600
screen = pygame.display.set_mode((width, height))
# Define colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
# Set up fonts
font = pygame.font.Font(None, 48)
small_font = pygame.font.Font(None, 36)
# Instructions and variables
input_text = ''
instruction_text = "How many files do you want to merge ?:"
running = True
enter_pressed = False
while running:
screen.fill(WHITE)
# Display the instruction text
instruction_surface = small_font.render(instruction_text, True, BLACK)
screen.blit(instruction_surface, (20, 20))
# Check for events
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.quit()
sys.exit()
# Print the pressed keys on the screen
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RETURN: # If enter is pressed
enter_pressed = True
elif event.key == pygame.K_BACKSPACE: # To handle backspace
input_text = input_text[:-1]
else:
input_text += event.unicode # Append the character typed
# Render the input text
input_surface = font.render(input_text, True, BLACK)
screen.blit(input_surface, (20, 100))
if enter_pressed:
if input_text.isdigit():
files_list = []
screen.fill(WHITE) # Clear screen
message = input_text
surface = font.render(message, True, BLACK)
screen.blit(surface, (20, height // 2))
# Handle file inputs
for i in range(int(input_text)):
screen.fill(WHITE)
message = 'Give file ' + str(i + 1)
surface = font.render(message, True, BLACK)
screen.blit(surface, (300, 200))
pygame.display.flip()
print(i)
files_list.append(merge(i))
execute(files_list)
clicked_image_index = -1
input_text = ""
start = False
break
else:
clicked_image_index = -1
pygame.display.flip()
# Case four(image 4)
if clicked_image_index == 4:
call_text("Select a PDF file", 90)
pygame.display.flip()
func = 4
file = give()
width, height = 800, 600
screen = pygame.display.set_mode((width, height))
message = ""
# Define colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
# Set up fonts
font = pygame.font.Font(None, 48)
small_font = pygame.font.Font(None, 36)
# Instructions and variables
input_text = ''
instruction_text = "Give the word you want to search for?:"
# Main loop
running = True
enter_pressed = False
while running:
screen.fill(WHITE)
# Display the instruction text
instruction_surface = small_font.render(instruction_text, True, BLACK)
screen.blit(instruction_surface, (20, 20))
# Check for events
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.quit()
sys.exit()
# Print the pressed keys on the screen
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RETURN: # If enter is pressed
enter_pressed = True
elif event.key == pygame.K_BACKSPACE: # To handle backspace
input_text = input_text[:-1]
else:
input_text += event.unicode # Append the character typed
# Render the input text
input_surface = font.render(input_text, True, BLACK)
screen.blit(input_surface, (20, 100))
if enter_pressed:
if not input_text.isdigit():
files_list = []
screen.fill(WHITE) # Clear screen
message = input_text
surface = font.render(message, True, BLACK)
screen.blit(surface, (20, height // 2))
break
pygame.display.flip()
# Conduct the word search
times = detect(file, message)
print(times)
clicked_image_index = -1
start = False
# Case five(image 5)
if clicked_image_index == 5:
func = 5
call_text("Select a PDF file", 90)
pygame.display.flip()
flag_tables = tables()
clicked_image_index = -1
start = False
# Update display
pygame.display.flip()
# Clean up Pygame
pygame.quit()