-
Notifications
You must be signed in to change notification settings - Fork 1
/
pdf.py
40 lines (34 loc) · 1.05 KB
/
pdf.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
from PyPDF2 import PdfFileWriter, PdfFileReader,PdfFileMerger
import os
import glob
import time
def remove_blank():
files = os.listdir('temp')
print(len(files))
for i in range(len(files)):
input_pdf = PdfFileReader(open(f"temp/temp{i}.pdf", "rb"))
output_pdf = PdfFileWriter()
output_pdf.addPage(input_pdf.getPage(0))
with open(f"temp\_temp{i}.pdf", "wb") as output_file:
output_pdf.write(output_file)
def remove_all_file():
folder_path = 'temp'
file_list = glob.glob(folder_path + '/*')
for file_path in file_list:
os.remove(file_path)
def merge(name):
remove_blank()
time.sleep(2)
pdf_count = 0
files = os.listdir('temp')
pdf_count = int(len(files)/2)
merger = PdfFileMerger()
print(pdf_count)
for i in range(pdf_count):
file_name = f"temp/_temp{i}.pdf"
merger.append(open(file_name, "rb"))
with open(f"pdfs/{name}.pdf", "wb") as output_file:
merger.write(output_file)
time.sleep(2)
time.sleep(2)
# merge("new")