Python中通过PyPDF2实现PDF合并
生活随笔
收集整理的這篇文章主要介紹了
Python中通过PyPDF2实现PDF合并
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
場景
PyPDF 2是一個純python PDF庫,能夠分割、合并、裁剪和轉換PDF文件的頁面。它還可以向PDF文件中添加自定義數據、查看選項和密碼。它可以從PDF檢索文本和元數據,還可以將整個文件合并在一起。
PyPDF 2 1.26.0文檔:
https://pythonhosted.org/PyPDF2/
實現
新建PDF1
新建PDF2
使用pip 安裝pypddf2
新建pdfMerge.py
from PyPDF2 import PdfFileReader, PdfFileWriterdef merge_pdfs(paths, output):pdf_writer = PdfFileWriter()for path in paths:pdf_reader = PdfFileReader(path)for page in range(pdf_reader.getNumPages()):# 將每頁添加到writer對象pdf_writer.addPage(pdf_reader.getPage(page))# 寫入合并的pdfwith open(output, 'wb') as out:pdf_writer.write(out)if __name__ == '__main__':paths = ['1.pdf', '2.pdf']merge_pdfs(paths, output='merged.pdf')運行
打開輸出的merge.pdf
資源以及代碼下載
https://download.csdn.net/download/badao_liumang_qizhi/11146143
總結
以上是生活随笔為你收集整理的Python中通过PyPDF2实现PDF合并的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MyBatisPlus的ActiveRe
- 下一篇: Python中通过PyPDF2实现PDF