-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmergePDFs.py
88 lines (53 loc) · 2.2 KB
/
mergePDFs.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
# -*- coding: utf-8 -*-
"""
Created on Fri May 25 09:21:12 2018
Program for merging PDFs
@author: Allen Gonczol
"""
import tkinter
from tkinter import filedialog, Tk, messagebox, simpledialog
from PyPDF2 import PdfFileMerger
import os
def merger(output_path, input_paths):
pdf_merger = PdfFileMerger()
file_handles = []
for path in input_paths:
pdf_merger.append(path)
with open(output_path, 'wb') as fileobj:
pdf_merger.write(fileobj)
if __name__ == '__main__':
print("Loading Window, please wait...")
root = Tk()
root.withdraw()
#Get name to be used for merged file
merged_name = simpledialog.askstring("FileName", "Enter the name to use for the merged PDF file.", parent=root)
#Add .pdf to end if user does not enter it
#TODO: handle casing
if not merged_name.endswith(".pdf"):
merged_name += ".pdf"
print(merged_name)
#limit to PDF files
ftypes = [("PDF file", "*.pdf")]
#Explain how to use program
messagebox.showinfo("Instructions",'Choose files in the order you want them merged.\nSelect "cancel" when finished.')
#Get a list of PDF file names to iterate through.
#Must be selected in desired order
file_names = []
output_dir = None
while True:
root.filename = filedialog.askopenfile(filetypes=ftypes)
try:
file_names.append(root.filename.name)
print(root.filename.name)
if output_dir == None:
output_dir = os.path.dirname(root.filename.name)
merged_name = output_dir + "/" + merged_name #Use forward slash to match what is returned from askdialog
except Exception as e:
print(e)
break
try:
merger(merged_name, file_names)
messagebox.showinfo("Success!",'Files successfully merged into PDF named "{}"'.format(merged_name))
except Exception as e:
messagebox.showinfo("Error!","Error: {}. For more help, visit https://github.com/afgonczol/mergePDFs".format(e))
root.destroy()