On the internet to find a few merged PDF software, found that is not very useful, generally do not have the ability to add bookmarks.
A python-merged PDF script was found on the web, and there was no bookmark feature added. So I wrote a small tool, the code is as follows:
1 #!/usr/bin/env Python32 #-*-coding:utf-8-*-3 " "4 #文件名: pdfmerge.py5 This script is used to merge PDF files and output PDF files to generate bookmarks by the PDF file name entered6 examples of use are:7 python pdfmerge.py-p "D:\pdf-files"-O "merged-out.pdf"-B True '8 9 Example Description:Ten The path of the PDF file to be merged: D:\pdf-files One output file name of the merged PDF file: Merged-out.pdf A whether to import bookmark values from PDF file: True - " " - ImportOS the fromArgparseImportArgumentparser, Rawtexthelpformatter - fromPyPDF2ImportPdffilereader, Pdffilewriter, Pdffilemerger - - defGetFileNames (filepath="', filelist_out=[],file_ext=' All'): + #traverse all files under FilePath, including files under subdirectories - forFpath, dirs, FSinchOs.walk (filepath): + forFinchFS: AFi_d =Os.path.join (Fpath, F) at ifFile_ext = =' All': - filelist_out.append (fi_d) - elifOs.path.splitext (Fi_d) [1] = =File_ext: - filelist_out.append (fi_d) - Else: - Pass in returnFilelist_out - to defMergefiles (Path, output_filename, import_bookmarks=False): + #iterate through all the PDFs in the directory and export them to a PDF file, the exported PDF file is bookmarked by default, and the book is signed as the previous file name - #bookmarks for original files are not imported by default, and Import_bookmarks=true can be used to import bookmarks from the original file into the output PDF file . theMerger =Pdffilemerger () *FileList = GetFileNames (Filepath=path, file_ext='. pdf') $ Panax Notoginseng forFileNameinchfilelist: -F=open (filename,'RB') thefile_rd=Pdffilereader (f) +Short_filename=os.path.basename (os.path.splitext (filename) [0]) AMerger.append (File_rd, Bookmark=short_filename, import_bookmarks=import_bookmarks) the Print('merge file:%s'%(filename)) + f.close () -Out_filename=os.path.join (Os.path.abspath (path), output_filename) $ merger.write (out_filename) $ Print('merged output file:%s'%(out_filename)) - merger.close () - the if __name__=="__main__": -description="\ nthe script is used to merge PDF files, and the output PDF files are generated by the PDF file name entered \ n Using the example below:"Wuyidescription=description+'\npython pdfmerge.py-p "D:\pdf-files"-O "merged-out.pdf"-B True' thedescription=description+'\ n'+"Example Description:" -description=description+'\ n'+"The path of the PDF file to be merged: D:\pdf-files" Wudescription=description+'\ n'+"output file name of the merged PDF file: Merged-out.pdf" -description=description+'\ n'+"whether to import bookmark values from PDF file: True" About $ #Add program Help, program help support line break symbols -Parser = Argumentparser (description=description, formatter_class=rawtexthelpformatter) - - #add command-line Options A +Parser.add_argument ("- P","--path", thedest="Path", -default=".", $help="The directory where the pdf file resides") theParser.add_argument ("- o","--output", thedest="Output_filename", thedefault="merged.pdf", thehelp="the output file name of the merged PDF", -Metavar="FILE") inParser.add_argument ("- b","--bookmark", thedest="Import_bookmarks", thedefault="False", Abouthelp="whether to import bookmarks from PDF file, value can be ' True ' or ' False '") the theargs =Parser.parse_args () theMergefiles (Args.path, Args.output_filename, Args.import_bookmarks)
Bulk merge PDF files with Python (with bookmark feature)