I am trying to merge PDF files inside a folder
I tried running the code from the same directory and it worked however when I copied the code to a different location and specified the directory path of PDF files, the merging process is not happening and I keep getting errors.
from PyPDF2 import PdfFileMerger import glob x = glob.glob("*pdf") merger = PdfFileMerger() for pdf in x: merger.append(open(pdf, 'rb')) with open("result.pdf", "wb") as fout: merger.write(fout)
This is the code that I wrote when I went one folder up with my source code folder location
x = [a for a in os.listdir('./merge_pdf') if a.endswith(".pdf")] for pdf in x: merger.append(open(pdf, 'rb')) with open("./merge_pdf/result.pdf", "wb") as fout: merger.write(fout) --->FileNotFoundError: [Errno 2] No such file or directory: '1.pdf'
For this reason I am running all of my codes from inside my PDF folders and I know it’s not a good practice.
Can anyone help me to resolve this issue, I am only in my early learning phase.
I also tried this
with open("result.pdf", "wb") as fout: merger.write("./merge_pdf/"+fout)
Advertisement
Answer
glob.glob
returns the full path to the file while os.listdir
only gives the file name. Just stick with glob.
from PyPDF2 import PdfFileMerger import glob x = glob.glob("merge_pdf/*pdf") merger = PdfFileMerger() for pdf in x: merger.append(open(pdf, 'rb')) with open("merge_pdf/result.pdf", "wb") as fout: merger.write(fout)
Another nice option is the pathlib
module.
from PyPDF2 import PdfFileMerger from pathlib import Path dir_path = Path("merge_pdf") result_pdf = dir_path/"result.pdf" merger = PdfFileMerger() for pdf in dir_path.glob("*pdf"): merger.append(pdf.open('rb')) with result_pdf.open("wb") as fout: merger.write(fout)