I tried to create a script to loop through parent folder and subfolders and merge all of the pdfs into one. Below if the code I wrote so far, but I don't know how to combine them into one script.
Reference: Merge PDF files
The first function is to loop through all of the subfolders under parent folder and get a list of path for each pdf.
import os
from PyPDF2 import PdfFileMerger
root = r"folder path"
path = os.path.join(root, "folder path")
def list_dir():
for path,subdirs,files in os.walk(root):
for name in files:
if name.endswith(".pdf") or name.endswith(".ipynb"):
print (os.path.join(path,name))
Second, I created a list to append all of the path to pdf files in the subfolders and merge into one combined file. At this step, I was told:
TypeError: listdir: path should be string, bytes, os.PathLike or None, not list
root_folder = []
root_folder.append(list_dir())
def pdf_merge():
merger = PdfFileMerger()
allpdfs = [a for a in os.listdir(root_folder)]
for pdf in allpdfs:
merger.append(open(pdf,'rb'))
with open("Combined.pdf","wb") as new_file:
merger.write(new_file)
pdf_merge()
Where and what should I modify the code in order to avoid the error and also combine two functions together?