使用Python批量合并PDF檔案(帶書籤功能)

來源:互聯網
上載者:User

標籤:list   emerge   ext   fileread   nbsp   ret   協助   script   路徑   

網上找了幾個合并pdf的軟體,發現不是很好用,一般都沒有添加書籤的功能。

在網上尋找了python合并pdf的指令碼,發現也沒有添加書籤的功能。於是自己動手編寫了一個小工具,代碼如下:

 1 #!/usr/bin/env python3 2 # -*- coding: utf-8 -*- 3 ‘‘‘ 4    #檔案名稱:pdfmerge.py 5    本指令碼用來合并pdf檔案,輸出的pdf檔案按輸入的pdf檔案名稱產生書籤 6    使用樣本如下: 7    python pdfmerge.py -p "D:\pdf-files" -o "merged-out.pdf" -b True‘ 8     9    樣本說明:10    要合并的pdf檔案所在的路徑: D:\pdf-files11    合并後的pdf檔案的輸出檔案名:merged-out.pdf12    是否從pdf檔案中匯入書籤的值:True13 ‘‘‘14 import os15 from argparse import ArgumentParser, RawTextHelpFormatter16 from PyPDF2 import PdfFileReader, PdfFileWriter, PdfFileMerger17 18 def getfilenames(filepath=‘‘,filelist_out=[],file_ext=‘all‘):19     # 遍曆filepath下的所有檔案,包括子目錄下的檔案20     for fpath, dirs, fs in os.walk(filepath):21         for f in fs:22             fi_d = os.path.join(fpath, f)23             if  file_ext == ‘all‘:24                 filelist_out.append(fi_d)25             elif os.path.splitext(fi_d)[1] == file_ext:26                 filelist_out.append(fi_d)27             else:28                 pass29     return filelist_out30 31 def mergefiles(path, output_filename, import_bookmarks=False):32     # 遍曆目錄下的所有pdf將其合并輸出到一個pdf檔案中,輸出的pdf檔案預設帶書籤,書籤名為之前的檔案名稱33     # 預設情況下原始檔案的書籤不會匯入,使用import_bookmarks=True可以將原檔案所帶的書籤也匯入到輸出的pdf檔案中34     merger = PdfFileMerger()35     filelist = getfilenames(filepath=path, file_ext=‘.pdf‘)36 37     for filename in filelist:38         f=open(filename, ‘rb‘)39         file_rd=PdfFileReader(f)40         short_filename=os.path.basename(os.path.splitext(filename)[0])41         merger.append(file_rd, bookmark=short_filename, import_bookmarks=import_bookmarks)42         print(‘合并檔案:%s‘%(filename))43         f.close()44     out_filename=os.path.join(os.path.abspath(path), output_filename)45     merger.write(out_filename)46     print(‘合并後的輸出檔案:%s‘%(out_filename))47     merger.close()48 49 if __name__ == "__main__":50     description="\n本指令碼用來合并pdf檔案,輸出的pdf檔案按輸入的pdf檔案名稱產生書籤\n使用樣本如下:"51     description=description+‘\npython pdfmerge.py -p "D:\pdf-files" -o "merged-out.pdf" -b True‘52     description=description+‘\n\n‘+"樣本說明:"53     description=description+‘\n‘+"要合并的pdf檔案所在的路徑: D:\pdf-files"54     description=description+‘\n‘+"合并後的pdf檔案的輸出檔案名:merged-out.pdf"55     description=description+‘\n‘+"是否從pdf檔案中匯入書籤的值:True"56    57     # 添加程式協助,程式協助支援分行符號號58     parser = ArgumentParser(description=description, formatter_class=RawTextHelpFormatter)59 60     # 添加命令列選項61 62     parser.add_argument("-p", "--path",63                         dest="path",64                         default=".",65                         help="PDF檔案所在目錄")66     parser.add_argument("-o", "--output",67                         dest="output_filename",68                         default="merged.pdf",69                         help="合并PDF的輸出檔案名",70                         metavar="FILE")71     parser.add_argument("-b", "--bookmark",72                     dest="import_bookmarks",73                     default="False",74                     help="是否從pdf檔案中匯入書籤,值可以是‘True‘或者‘False‘")75 76     args = parser.parse_args()77     mergefiles(args.path, args.output_filename, args.import_bookmarks)

 

使用Python批量合并PDF檔案(帶書籤功能)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.