|
2 | 2 | import consts
|
3 | 3 |
|
4 | 4 | import re
|
5 |
| -from loguru import logger |
6 | 5 | from sys import stdout, platform
|
7 | 6 | from random import randint, choice
|
8 | 7 | import os
|
9 | 8 | import subprocess as sb
|
10 | 9 | from argparse import ArgumentTypeError
|
11 | 10 |
|
| 11 | +from PyPDF2 import PdfReader |
| 12 | +from loguru import logger |
| 13 | + |
12 | 14 | def logger_config(v):
|
13 | 15 | logger.remove()
|
14 | 16 | if int(v) == 0:
|
@@ -243,3 +245,199 @@ def calculate_seal_coords(sign_coords, new_page=False):
|
243 | 245 | seal_coords = [[x1-pd, y1-pd], [x2+pd, y2+pd]]
|
244 | 246 |
|
245 | 247 | return seal_coords
|
| 248 | + |
| 249 | +# coords: list - список из пар координат (x, y) |
| 250 | +def calculate_borders(original_coords, creator_and_date=False, task=False): |
| 251 | + |
| 252 | + def calculate(coords): |
| 253 | + if len(coords) == 1: |
| 254 | + x1 = min(coords) |
| 255 | + y1 = coords[0][1] |
| 256 | + y2 = coords[0][1] |
| 257 | + |
| 258 | + elif len(coords) > 1: |
| 259 | + x1 = min(coords) |
| 260 | + y1 = 10000 |
| 261 | + y2 = 0 |
| 262 | + |
| 263 | + for pair in coords: |
| 264 | + if pair[1] < y1: |
| 265 | + y1 = pair[1] |
| 266 | + |
| 267 | + if pair[1] > y2: |
| 268 | + y2 = pair[1] |
| 269 | + |
| 270 | + else: |
| 271 | + return [] |
| 272 | + |
| 273 | + x1 = PDFunits_to_px(x1[0]) |
| 274 | + y1 = PDFunits_to_px(y1) |
| 275 | + y2 = PDFunits_to_px(y2) |
| 276 | + |
| 277 | + x_offset = consts.text_borders[0] |
| 278 | + y_offset = consts.text_borders[1] |
| 279 | + |
| 280 | + if task: |
| 281 | + return [[x1 - x_offset, y1 - y_offset * 2], [2385, y2 + y_offset / 2]] |
| 282 | + elif creator_and_date: |
| 283 | + return [[x1 - x_offset, y1 - y_offset * 0.75], [2385, y2 + y_offset / 4]] |
| 284 | + else: |
| 285 | + return [[x1 - x_offset, y1 - y_offset * 1.6], [2385, y2 + y_offset / 2]] |
| 286 | + |
| 287 | + if original_coords == ["page_break"]: |
| 288 | + return original_coords |
| 289 | + elif "page_break" in original_coords: |
| 290 | + splitted_coords = [] |
| 291 | + result = [] |
| 292 | + for pair in original_coords: |
| 293 | + if pair != "page_break": |
| 294 | + splitted_coords.append(pair) |
| 295 | + else: |
| 296 | + result.append(calculate(splitted_coords)) |
| 297 | + result.append("page_break") |
| 298 | + splitted_coords = [] |
| 299 | + else: |
| 300 | + return calculate(original_coords) |
| 301 | + |
| 302 | + return result |
| 303 | + |
| 304 | + |
| 305 | +# pdf_path: str - путь к pdf файлу |
| 306 | +# data: tuple - кортеж данных для генерации |
| 307 | +def calculate_text_coords(pdf_path, data): |
| 308 | + header, header_coords = data[0], [] |
| 309 | + name, name_coords = data[1], [] |
| 310 | + intro, intro_coords = data[2], [] |
| 311 | + instruction, instruction_coords = data[3], [[] * i for i in range(len(data[3]))] |
| 312 | + responsible, responsible_coords = data[4], [] |
| 313 | + creator, creator_coords = data[5], [] |
| 314 | + date, date_coords = data[6], [] |
| 315 | + |
| 316 | + reader = PdfReader(pdf_path) |
| 317 | + for page in reader.pages: |
| 318 | + # page = reader.pages[0] |
| 319 | + |
| 320 | + raw_data = [] |
| 321 | + def visitor_t(text, cm, tm, fontDict, fontSize): |
| 322 | + raw_data.append([text, tm[4], tm[5]]) # [text, x1, y1] |
| 323 | + |
| 324 | + page.extract_text(visitor_text=visitor_t) # Посимвольное извлечение координат текста |
| 325 | + |
| 326 | + for i in range(len(raw_data)): |
| 327 | + raw_data[i][1] = int(raw_data[i][1]) |
| 328 | + raw_data[i][2] = int(raw_data[i][2]) |
| 329 | + |
| 330 | + # Объединение символов с одинаковыми координатами в строки |
| 331 | + # text - список со строками |
| 332 | + # coords - список с соответствующими строкам координатами |
| 333 | + text, coords = [], [] |
| 334 | + for i in range(len(raw_data)): |
| 335 | + if [raw_data[i][1], raw_data[i][2]] not in coords: |
| 336 | + coords.append([raw_data[i][1], raw_data[i][2]]) |
| 337 | + text.append(raw_data[i][0]) |
| 338 | + else: |
| 339 | + text[-1] += raw_data[i][0] |
| 340 | + |
| 341 | + if (len(text) != len(coords)): |
| 342 | + logger.error("[text] != [coords]") |
| 343 | + raise SystemExit |
| 344 | + |
| 345 | + formatted_markup = {} |
| 346 | + for i in range(len(text)): |
| 347 | + if text[i] == '': |
| 348 | + continue |
| 349 | + |
| 350 | + formatted_markup[text[i]] = coords[i] |
| 351 | + |
| 352 | + # Если ключ словаря (строка в документе) входит в секцию данных для генерации, |
| 353 | + # то добавить координаты строки в соответствующий список |
| 354 | + for k in list(formatted_markup): |
| 355 | + |
| 356 | + if ((k.replace('\n', '') in header) and (not name_coords)): |
| 357 | + val = formatted_markup.pop(k) |
| 358 | + header_coords.append(val) |
| 359 | + |
| 360 | + if ((k.replace('\n', '') in name) and (not intro_coords)): |
| 361 | + try: |
| 362 | + val = formatted_markup.pop(k) |
| 363 | + name_coords.append(val) |
| 364 | + except KeyError: |
| 365 | + pass |
| 366 | + |
| 367 | + if (k.replace('\n', '') in intro): |
| 368 | + try: |
| 369 | + val = formatted_markup.pop(k) |
| 370 | + intro_coords.append(val) |
| 371 | + except KeyError: |
| 372 | + pass |
| 373 | + |
| 374 | + for i in range(len(instruction)): |
| 375 | + task = instruction[i]["task_text"] |
| 376 | + if (k.replace('\n', '') in task): |
| 377 | + try: |
| 378 | + val = formatted_markup.pop(k) |
| 379 | + instruction_coords[i].append(val) |
| 380 | + except KeyError: |
| 381 | + pass |
| 382 | + |
| 383 | + if (k.replace('\n', '') in responsible): |
| 384 | + try: |
| 385 | + val = formatted_markup.pop(k) |
| 386 | + responsible_coords.append(val) |
| 387 | + except KeyError: |
| 388 | + pass |
| 389 | + |
| 390 | + if (k.replace('\n', '') in creator): |
| 391 | + try: |
| 392 | + val = formatted_markup.pop(k) |
| 393 | + creator_coords.append(val) |
| 394 | + except KeyError: |
| 395 | + pass |
| 396 | + |
| 397 | + if (k.replace('\n', '') in '.' + date): |
| 398 | + try: |
| 399 | + val = formatted_markup.pop(k) |
| 400 | + date_coords.append(val) |
| 401 | + except KeyError: |
| 402 | + pass |
| 403 | + |
| 404 | + header_coords.append("page_break") |
| 405 | + name_coords.append("page_break") |
| 406 | + intro_coords.append("page_break") |
| 407 | + instruction_coords.append(["page_break"]) |
| 408 | + responsible_coords.append("page_break") |
| 409 | + creator_coords.append("page_break") |
| 410 | + date_coords.append("page_break") |
| 411 | + |
| 412 | + |
| 413 | + header_coords = calculate_borders(header_coords) |
| 414 | + |
| 415 | + name_coords = calculate_borders(name_coords) |
| 416 | + |
| 417 | + intro_coords = calculate_borders(intro_coords) |
| 418 | + |
| 419 | + task_coords = [] |
| 420 | + for task in instruction_coords: |
| 421 | + task_coords.append(calculate_borders(task, task=True)) |
| 422 | + |
| 423 | + responsible_coords = calculate_borders(responsible_coords) |
| 424 | + |
| 425 | + creator_coords = calculate_borders(creator_coords, creator_and_date=True) |
| 426 | + |
| 427 | + date_coords = calculate_borders(date_coords, creator_and_date=True) |
| 428 | + |
| 429 | + # try: |
| 430 | + # intro_coords[0][1] -= 55 |
| 431 | + # except TypeError: |
| 432 | + # pass |
| 433 | + |
| 434 | + # for i in range(len(task_coords)): |
| 435 | + # try: |
| 436 | + # task_coords[i][0][1] -= 80 |
| 437 | + # except TypeError: |
| 438 | + # continue |
| 439 | + |
| 440 | + result = [header_coords, name_coords, intro_coords, task_coords, responsible_coords, |
| 441 | + creator_coords, date_coords] |
| 442 | + |
| 443 | + return result |
0 commit comments