Skip to content

Commit

Permalink
Fixed relative paths
Browse files Browse the repository at this point in the history
  • Loading branch information
nimaid committed Jul 2, 2020
1 parent e92e4b1 commit 4a48949
Showing 1 changed file with 27 additions and 11 deletions.
38 changes: 27 additions & 11 deletions nested-pdf-merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,16 @@ def dir_path(string):
help="the character used to seperate the direcotry ordering numbers from the bookmark names (like '.' or ')')")
args = vars(ap.parse_args())

input_dir_name = args["input_dir"].strip(os.path.sep).split(os.path.sep)[-1]
if not os.path.isabs(args["input_dir"]):
input_dir = os.path.join(PROG_PATH, input_dir)
else:
input_dir = args["input_dir"]

input_dir_name = input_dir.strip(os.path.sep).split(os.path.sep)[-1]


if args["output_file"] == None:
output_file = input_dir_name + os.path.extsep + "pdf"
output_file = input_dir + os.path.extsep + "pdf"
else:
out_dir, out_name = os.path.split(args["output_file"])
out_name_split = out_name.split(os.path.extsep)
Expand All @@ -48,6 +54,12 @@ def dir_path(string):
# No extention provided
output_file = args["output_file"] + os.path.extsep + "pdf"

print(output_file)

output_file_dir, output_file_name = os.path.split(output_file)

print(output_file_name)

# Do main imports
from fpdf import FPDF
from PIL import Image
Expand Down Expand Up @@ -103,15 +115,16 @@ def get_directory_structure(rootdir):

# Walk though folder structure (recursive alphabetical)
# Save image paths to ordered dictionary
page_dict, page_list = get_directory_structure(args["input_dir"])
page_dict, page_list = get_directory_structure(input_dir)

# Get size from first page
cover = Image.open(page_list[0])
width, height = cover.size

# Create PDF from page_list(no bookmarks)
print("Adding images to PDF...")
temp_pdf = "temp_" + output_file
temp_pdf = os.path.join(output_file_dir, "temp_" + output_file_name)
print(temp_pdf)
pdf = FPDF(unit = "pt", format = [width, height])
for page in page_list:
print("Adding page: " + page)
Expand All @@ -120,18 +133,15 @@ def get_directory_structure(rootdir):
print("Saving temporary PDF '{}'".format(temp_pdf))
pdf.output(temp_pdf, "F")


# Load PDF into PyPDF2
print("Loading temporary PDF into editing library...")
output_pdf = PdfFileWriter()
input_pdf = PdfFileReader(open(temp_pdf, 'rb'))

input_pdf_file = open(temp_pdf, 'rb')
input_pdf = PdfFileReader(input_pdf_file)
for p in range(input_pdf.numPages):
output_pdf.addPage(input_pdf.getPage(p))

# Delete temporary PDF
print("Deleting temporary PDF '{}'".format(temp_pdf))
os.remove(temp_pdf)

# Add nested bookmarks from page_dict
print()
print("Creating nested bookmarks...")
Expand Down Expand Up @@ -177,9 +187,15 @@ def iterdict(d, base_path=""):
page_index = page_list.index(filename)
last_page_index = page_index + 1
#print(ident + k + "\tPage #" + str(page_index + 1))
iterdict(page_dict[input_dir_name], base_path=input_dir_name)
iterdict(page_dict[input_dir_name], base_path=input_dir)

# Save final PDF
print("Saving bookmarked PDF '{}'".format(output_file))
with open(output_file, 'wb') as f:
output_pdf.write(f)

# Delete temporary PDF
print()
print("Deleting temporary PDF '{}'".format(temp_pdf))
input_pdf_file.close()
os.remove(temp_pdf)

0 comments on commit 4a48949

Please sign in to comment.