-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqrgen.py
115 lines (89 loc) · 3.31 KB
/
qrgen.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
import sys
sys.path.append(r'C:\Users\mamet\PycharmProjects\QRgenerator')
sys.path.append(r'C:\Users\mamet\PycharmProjects\QRgenerator\poppler')
import glob
from Classes_and_Func.Scanner import *
from Classes_and_Func.QR import *
from Classes_and_Func.Stamper import *
from Classes_and_Func.Printer import *
import pdf2image
import os
import termcolor
import colorama
import PyPDF2
from Classes_and_Func.QRParse import parser
import time
colorama.init()
pdf_write_object = PyPDF2.PdfFileWriter()
def get_file_path(args):
return sys.argv[1]
def connect(str_list):
"""Connect all strings of array"""
res = ""
for el in str_list:
res += el
return res
def get_file_name(filename):
"""Returns filename without extension"""
dot_array = filename.split(".")
res = ""
for el in dot_array[:-1]:
res += el
return res
def add_pdf_to_merging(pdf_filename, pdf_write_object):
pdf_read_object = PyPDF2.PdfFileReader(get_file_name(pdf_filename) + "_withQR.pdf")
for page in range(pdf_read_object.numPages):
pdf_write_object.addPage(pdf_read_object.getPage(page))
return pdf_write_object
def main(args):
global pdf_write_object
# PATH_to_folder = '/home/alex/Documents/PythonProjects/APrint/private/PDFs/'
# NEED_SAVE_QR = False
# MERGE_PDFS = True
#
PATH_to_folder = args.folder_path
NEED_SAVE_QR = args.sQR
MERGE_PDFS = args.MergePDF
print(str(args))
paths = (glob.glob(PATH_to_folder + "\*.pdf"))
print("Найдено {} PDF".format(str(len(paths))))
for i, path in enumerate(paths):
print(termcolor.colored(path, "green"))
scanner = Scanner(path)
data = scanner.data # Информация о i-ой платёжке
qr = QR(cost=data["cost"], name=data["name"], address=data["address"], period=data["period"])
if NEED_SAVE_QR:
qr.qr.save("qr({}).png".format(i))
doc_image = pdf2image.convert_from_path(path)
stamper = Stamper(qr=qr.qr, document_image=doc_image[0])
stamper.do_stamp()
try:
printer = Printer(stamper.document, os.path.dirname(path) + '\\QR\\' + path.split("\\")[-1])
printer.save()
except FileNotFoundError as e:
print(PATH_to_folder)
os.mkdir(PATH_to_folder + "\\QR\\")
print(termcolor.colored("Создал новую папку в " + str(PATH_to_folder + "\\QR\\"), "yellow"))
result_filename = os.path.dirname(path) + "\\QR\\" + path.split("\\")[-1]
stamper.document, os.path.dirname(path) + "\\QR\\"
printer = Printer(stamper.document, result_filename)
printer.save()
# Merging pdfs
if MERGE_PDFS:
pdf_write_object = add_pdf_to_merging(result_filename, pdf_write_object)
# except Exception as e:
# print(a)
# # print(termcolor.colored("Ошибка при сохранении файла" + str(path) + str(e), "red"))
if MERGE_PDFS:
merged_pdf = open(PATH_to_folder + "Merged.pdf", 'wb')
pdf_write_object.write(merged_pdf)
merged_pdf.close()
if __name__ == '__main__':
args = parser.parse_args()
time.sleep(10)
main(args)
print()
print("Готого")
pass
for i in range(10):
pass