-
Notifications
You must be signed in to change notification settings - Fork 0
/
combinePdfs.py
76 lines (59 loc) · 2.01 KB
/
combinePdfs.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
#! python3
# combinePdfs.py - Combines all the PDFs in the current working directory into
# a single PDF.
import PyPDF2
import os
import argparse
import sys
dragPath = ""
parameterPath = "."
# parser = argparse.ArgumentParser(prog="JuntaPDF's",
# description="Junta PDF's em um único arquivo (combinado.pdf)")
# parser.add_argument(
# '--caminho', help="Caminho da pasta onde estão os PDF's, se não for informado, utiliza a pasta atual", default=".")
#args = parser.parse_known_args()
#print("args", args)
print(sys.argv)
print('teste')
x = 0
for atributo in sys.argv[1:]:
x += 1
if atributo.startswith("caminho"):
parameterPath = atributo[8:]
print("caminho", x, parameterPath)
else:
if atributo.strip != "":
dragPath = atributo
print("caminho2", x, atributo)
if dragPath != "":
parameterPath = dragPath
if os.path.exists(parameterPath) == False:
print("caminho", parameterPath)
input("O caminho informado não existe, por favor verifique.")
sys.exit()
if len(dragPath) > 0:
if parameterPath == '.':
if os.path.exists(dragPath):
parameterPath = dragPath
# Get all the PDF filenames.
pdfFiles = []
for filename in os.listdir(parameterPath):
if filename.endswith('.pdf') and filename != 'combinado.pdf':
pdfFiles.append(parameterPath + '\\' + filename)
pdfFiles.sort()
if len(pdfFiles) == 0:
input("O caminho informado não possui PDF's, por favor verifique.")
sys.exit()
pdfWriter = PyPDF2.PdfFileWriter()
# Loop through all the PDF files.
for filename in pdfFiles:
pdfFileObj = open(filename, 'rb')
pdfReader = PyPDF2.PdfFileReader(pdfFileObj)
# Loop through all the pages (except the first) and add them.
for pageNum in range(0, pdfReader.numPages):
pageObj = pdfReader.getPage(pageNum)
pdfWriter.addPage(pageObj)
# Save the resulting PDF to a file.
pdfOutput = open(parameterPath + '\combinado.pdf', 'wb')
pdfWriter.write(pdfOutput)
pdfOutput.close()