-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
137 lines (124 loc) · 5.25 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
import eel
from PyPDF2 import PdfFileWriter, PdfFileReader
from reportlab.pdfgen import canvas
from datetime import datetime
from tkinter import Tk
from tkinter.filedialog import askdirectory,askopenfilename,askopenfilenames
import time
from glob import glob
#
# target=r"D:\Download\Documents\34779154304018873.pdf"
# reader=PdfFileReader(target)
# write=PdfFileWriter()
# page=reader.getPage(0)
# page.cropBox.setLowerLeft((30,100))
# page.cropBox.setUpperLeft((30,842))
# page.cropBox.setUpperRight((595,842))
# page.cropBox.setLowerRight((595,100))
# write.addPage(page)
# with open("test_crop.pdf","wb") as out:
# write.write(out)
@eel.expose
def start(target):
# target=r"D:\Download\Documents\34779154304018873.pdf"
c = canvas.Canvas(r"D:/grid.pdf")
width=10
space=10
c.setDash([2,4,width,space])
c.setLineWidth(6)
c.line(300,0,300,850)
c.line(0,420,600,420)
c.save()
result_name=datetime.now().strftime(f"result %Y-%m-%d at %I.%M.%S %p")
grid=PdfFileReader("D:/grid.pdf","rb")
output = PdfFileWriter()
output.insertPage(grid.getPage(0))
# output.getPage(0)
blank_page = output.getPage(0)
itr = 0
# measures = [('0', '450'), ('320', '450'), ('0', '0'), ('320', '0')]
measures = [('0', '450'), ('250', '450'), ('0', '0'), ('250', '0')]
page_capacity = 4
scale = 0.45
if len(target)==1:
print(f"current file {target[0]}")
with open(fr"{target[0]}", 'rb') as current_file:
input1 = PdfFileReader(current_file)
for count, i in enumerate(range(0, input1.getNumPages(), 1)):
page = input1.getPage(i)
if (input1.getNumPages() / 2) <= 3:
print("file page <=3")
blank_page.mergeScaledTranslatedPage(page2=page, scale=f'{scale}', tx=f'{measures[count][0]}', ty=f'{measures[count][1]}')
elif (input1.getNumPages() / 2) > 3:
print("file page >3")
blank_page.mergeScaledTranslatedPage(page2=page, scale=f'{scale}', tx=f'{measures[itr][0]}', ty=f'{measures[itr][1]}')
itr = (itr + 1) % len(measures)
if (count + 1) % page_capacity == 0 and (count+1) < input1.getNumPages():
print("add blank page")
# output.insertPage(grid.getPage(0))
blank_page = output.getPage(0)
with open(f'D:/{result_name}.pdf', "wb") as out_f:
output.write(out_f)
else:
scale=0.45
if len(target)<=4:
print("enter 4 file case")
for count, pdf in enumerate(target):
with open(f'{pdf}','rb') as current_file:
input1=PdfFileReader(current_file)
bill=input1.getPage(0)
bill.cropBox.setLowerLeft((30, 100))
bill.cropBox.setUpperLeft((30, 842))
bill.cropBox.setUpperRight((595, 842))
bill.cropBox.setLowerRight((595, 100))
# target=r"D:\Download\Documents\34779154304018873.pdf"
# reader=PdfFileReader(target)
# write=PdfFileWriter()
# page=reader.getPage(0)
# page.cropBox.setLowerLeft((30,100))
# page.cropBox.setUpperLeft((30,842))
# page.cropBox.setUpperRight((595,842))
# page.cropBox.setLowerRight((595,100))
# write.addPage(page)
# with open("test_crop.pdf","wb") as out:
# write.write(out)
blank_page.mergeScaledTranslatedPage(page2=bill, scale=f'{scale}', tx=f'{measures[count][0]}', ty=f'{measures[count][1]}')
with open(f'D:/{result_name}.pdf', "wb") as out_f:
output.addPage(bill)
output.write(out_f)
else:
print("enter more 4 case")
for count, pdf in enumerate(target):
with open(f'{pdf}','rb') as current_file:
input1=PdfFileReader(current_file)
bill=input1.getPage(0)
blank_page.mergeScaledTranslatedPage(page2=bill, scale=f'{scale}', tx=f'{measures[count%len(measures)][0]}', ty=f'{measures[count%len(measures)][1]}')
with open(f'D:/{result_name}.pdf', "wb") as out_f:
output.write(out_f)
if (count + 1) % page_capacity == 0 and (count + 1) < len(target):
# output.insertPage(grid.getPage(0))
# output.getNumPages()
blank_page = output.getPage(0)
print("done")
time.sleep(1)
eel.say_hello_js('Python World!')
@eel.expose
def get_path():
root = Tk()
root.withdraw()
root.wm_attributes('-topmost', 1)
folder_path = askopenfilenames(filetypes=[("pdf files", "*.pdf")])
# print(folder_path)
return folder_path
lis=(
'D:/Download/Documents/reportlab-reference.pdf',
'D:/Download/Documents/reportlab-userguide.pdf',
'D:/Download/Documents/sql-basics-cheat-sheet-a4.pdf'
)
len(lis)
@eel.expose
def test(file):
print(file,type(file))
eel.init('D:/Python/Amazon_bills/web')
# eel.t
eel.start('main.html')