defmain(): # find all the pdf files in current directory. mypath = os.getcwd() print(mypath) pattern = r"\.pdf$" #以下文件路径在debian10中适用 file_names_lst = [mypath + '/'+f for f in os.listdir(mypath) if re.search(pattern, f, re.IGNORECASE) andnot re.search(r'Merged.pdf',f)]
# merge the file. opened_file = [open(file_name,'rb') for file_name in file_names_lst] pdfFM = PyPDF2.PdfFileMerger() for file in opened_file: pdfFM.append(file)
# output the file. withopen(mypath + '/' +"Merged.pdf", 'wb') as write_out_file: pdfFM.write(write_out_file)
# close all the input files. for file in opened_file: file.close() if __name__ == '__main__': main()