I am attempting to create a function that combines pdf files into a single file. I am attempting to take a dictionary that is constructed as follows:
mydict = {"001 Pre-Op - Production Run Form - Shift-1-C.pdf":
['001 Pre-Op - Production Run Form - Shift-1-1.pdf',
'001 Pre-Op - Production Run Form - Shift-1-2.pdf'],
"001 Pre-Op - Production Run Form - Shift-2-C.pdf":
['001 Pre-Op - Production Run Form - Shift-2-1.pdf',
'001 Pre-Op - Production Run Form - Shift-2-2.pdf']}
I would like to create a new pdf with the key name (ie "001 Pre-Op - Production Run Form - Shift-1-C.pdf") that's created by combing the two values (['001 Pre-Op - Production Run Form - Shift-1-1.pdf','001 Pre-Op - Production Run Form - Shift-1-2.pdf']) for each key. I am currently using this code to combine the values, rename as key name, & delete the "values" that were PDFs:
from PyPDF2 import PdfFileWriter, PdfFileReader, PdfFileMerger, PdfFileReader
import pathlib
import shutil
import datetime
import os
merger = PdfFileMerger()
pdfs1 = ['001 Pre-Op - Production Run Form - Shift-1-1.pdf',
'001 Pre-Op - Production Run Form - Shift-1-2.pdf']
try:
for pdf in pdfs1:
merger.append(pdf)
merger.write("001 Pre-Op - Production Run Form - Shift-1-C.pdf")
if os.path.exists("001 Pre-Op - Production Run Form - Shift-1-C.pdf"):
[os.remove(f) for f in pdfs1]
else:
pass
except Exception:
pass
pdfs2 = ['001 Pre-Op - Production Run Form - Shift-2-1.pdf',
'001 Pre-Op - Production Run Form - Shift-2-2.pdf']
try:
for pdf in pdfs2:
merger.append(pdf)
merger.write("001 Pre-Op - Production Run Form - Shift-2-C.pdf")
if os.path.exists("001 Pre-Op - Production Run Form - Shift-2-C.pdf"):
[os.remove(f) for f in pdfs2]
else:
pass
except Exception:
pass
You can see this is a very manual process to combine the PDFs & delete the original PDFs if they were able to be combined. This makes it very error prone as it's manual. My real dictionary has 100+ keys/values to look up across.
I am looking for a more pythonic solution that allows me to combine, name, & remove PDFs with dictionary. Any help would be greatly appreciated!
I've tried the below loop but nothing happens:
for k,v in mydict.items():
try:
for pdf in v:
print(pdf)
merger.append(pdf)
except Exception:
pass
The problem is not with the algorithm.
I don't use this package myself, but what I noticed is that you don't use the
with # (requires the magic method __enter__ in PdfFileMerger)
statement to create the file and close it automatically, nor the PdfFileMerger.close() method after writing.
Try the following code:
for k, v in mydict.items():
try:
merger = PdfFileMerger()
for pdf in v:
print(pdf)
merger.append(pdf)
merger.write(k)
merger.close()
if os.path.exists(k):
print(1)
except Exception:
pass