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.
JavaScript
x
12
12
1
from PyPDF2 import PdfFileMerger
2
import glob
3
4
x = glob.glob("*pdf")
5
merger = PdfFileMerger()
6
7
for pdf in x:
8
merger.append(open(pdf, 'rb'))
9
10
with open("result.pdf", "wb") as fout:
11
merger.write(fout)
12
This is the code that I wrote when I went one folder up with my source code folder location
JavaScript
1
9
1
x = [a for a in os.listdir('./merge_pdf') if a.endswith(".pdf")]
2
for pdf in x:
3
merger.append(open(pdf, 'rb'))
4
5
with open("./merge_pdf/result.pdf", "wb") as fout:
6
merger.write(fout)
7
8
--->FileNotFoundError: [Errno 2] No such file or directory: '1.pdf'
9
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
JavaScript
1
3
1
with open("result.pdf", "wb") as fout:
2
merger.write("./merge_pdf/"+fout)
3
Advertisement
Answer
glob.glob
returns the full path to the file while os.listdir
only gives the file name. Just stick with glob.
JavaScript
1
12
12
1
from PyPDF2 import PdfFileMerger
2
import glob
3
4
x = glob.glob("merge_pdf/*pdf")
5
merger = PdfFileMerger()
6
7
for pdf in x:
8
merger.append(open(pdf, 'rb'))
9
10
with open("merge_pdf/result.pdf", "wb") as fout:
11
merger.write(fout)
12
Another nice option is the pathlib
module.
JavaScript
1
14
14
1
from PyPDF2 import PdfFileMerger
2
from pathlib import Path
3
4
dir_path = Path("merge_pdf")
5
result_pdf = dir_path/"result.pdf"
6
7
merger = PdfFileMerger()
8
9
for pdf in dir_path.glob("*pdf"):
10
merger.append(pdf.open('rb'))
11
12
with result_pdf.open("wb") as fout:
13
merger.write(fout)
14