python合并多个pdf_python合并多个pdf文件
假設(shè)您有個無聊的工作,將幾十個PDF文檔合并成一個PDF文件。 他們每個都有封面頁作為第一頁,但你不希望在最終結(jié)果中重復(fù)覆蓋表。 即使有有很多免費的程序來組合PDF,其中許多只是合并整個文件在一起。 讓我們編寫一個Python程序來自定義哪些頁面你想要的是組合PDF。從高層次來看,這是程序?qū)⒁龅氖虑?#xff1a;
查找當(dāng)前工作目錄中的所有PDF文件。
對文件名進(jìn)行排序,以便按順序添加PDF。
將每個PDF的每個頁面(不包括第一頁)寫入輸出文件。
在實現(xiàn)方面,您的代碼需要執(zhí)行以下操作:
調(diào)用 os.listdir() 來查找工作目錄中的所有文件,刪除所有非PDF文件。
調(diào)用Python的sort()列表方法來按字母順序排列文件名。
為輸出PDF創(chuàng)建PdfFileWriter對象。
遍歷每個PDF文件,為其創(chuàng)建PdfFileReader對象。
在每個PDF文件中循環(huán)遍歷每個頁面(第一頁除外)。
將頁面添加到輸出PDF。
將輸出PDF寫入名為allminutes.pdf的文件。
對于此項目,請打開一個新的文件編輯器窗口并將其另存為 “combinePdfs.py”
Step 1:找到所有的PDF文件
首先,您的程序需要獲取所有擴(kuò)展名為.pdf的文件的列表
當(dāng)前的工作目錄并對它們進(jìn)行排序。 讓你的代碼看起來像
以下:
在這里插入代碼片
在shebang線和關(guān)于什么的描述性評論之后程序沒有,這段代碼導(dǎo)入了os和PyPDF2模塊。該
os.listdir(’.’) 調(diào)用將返回當(dāng)前工作中的每個文件的列表目錄。 代碼循環(huán)遍歷此列表,并僅添加帶有.pdf擴(kuò)展的那些文件pdfFiles。之后,此列表按字母順序排序,使用key = str.lower關(guān)鍵字參數(shù)對sort() 進(jìn)行排序。創(chuàng)建PdfFileWriter對象以保存組合的PDF頁面。最后,一些評論概述了該計劃的其余部分。
#! /usr/bin/python3
# combinePdfs.py - Combines all the PDFs in the current working directory into
# a single PDF.
import PyPDF2, os
# Get all the PDF filenames.
pdfFiles = []
for filename in os.listdir('.'):
if filename.endswith('.pdf'):
pdfFiles.append(filename)
pdfFiles.sort(key = str.lower)
pdfWriter = PyPDF2.PdfFileWriter()
# TODO: Loop through all the PDF files.
# TODO: Loop through all the pages (except the first) and add them.
# TODO: Save the resulting PDF to a file.
第二步:打開每一個 PDF 文件
現(xiàn)在程序必須讀取pdfFiles中的每個PDF文件。 添加以下內(nèi)容:
#! /usr/bin/python3
# combinePdfs.py - Combines all the PDFs in the current working directory into
# a single PDF.
import PyPDF2, os
# Get all the PDF filenames.
pdfFiles = []
for filename in os.listdir('.'):
if filename.endswith('.pdf'):
pdfFiles.append(filename)
pdfFiles.sort(key = str.lower)
pdfWriter = PyPDF2.PdfFileWriter()
# Loop through all the PDF files.
for filename in pdfFiles:
pdfFileObj = open(filename, 'rb')
pdfReader = PyPDF2.PdfFileReader(pdfFileObj)
# TODO: Loop through all the pages (except the first) and add them.
# TODO: Save the resulting PDF to a file.
對于每個PDF,循環(huán)通過以讀二進(jìn)制模式(以’rb’作為第二個參數(shù))調(diào)用open() 。 open()調(diào)用返回一個File對象,它被傳遞給PyPDF2.PdfFileReader() 。
第三步: 添加每一頁
對于每個PDF,您都希望遍歷除第一個頁面之外的每個頁面。 加上這個代碼到你的程序:
#! /usr/bin/python3
# combinePdfs.py - Combines all the PDFs in the current working directory into
# a single PDF.
import PyPDF2, os
# Get all the PDF filenames.
pdfFiles = []
for filename in os.listdir('.'):
if filename.endswith('.pdf'):
pdfFiles.append(filename)
pdfFiles.sort(key = str.lower)
pdfWriter = PyPDF2.PdfFileWriter()
# Loop through all the PDF files.
for filename in pdfFiles:
pdfFileObj = open(filename, 'rb')
pdfReader = PyPDF2.PdfFileReader(pdfFileObj)
# Loop through all the pages (except the first) and add them.
for pageNum in range(1, pdfReader.numPages):
pageObj = pdfReader.getPage(pageNum)
pdfWriter.addPage(pageObj)
# TODO: Save the resulting PDF to a file.
for循環(huán)中的代碼將每個Page對象分別復(fù)制到PdfFileWriter對象。 請記住,您想跳過第一頁。 以來
PyPDF2認(rèn)為0是第一頁,你的循環(huán)應(yīng)該從1 開始,然后轉(zhuǎn)到但不包括pdfReader.numPages中的整數(shù)。
第四步: 保存結(jié)果
在這些嵌套的for循環(huán)完成循環(huán)之后,pdfWriter變量將會循環(huán)包含PdfFileWriter對象,其中包含所有PDF的頁面。最后一步是將此內(nèi)容寫入硬盤驅(qū)動器上的文件。 將此代碼添加到你程序中:
#!/usr/bin/python3
# combinePdfs.py - Combines all the PDFs in the current working directory into
# a single PDF.
import PyPDF2, os
# Get all the PDF filenames.
pdfFiles = []
for filename in os.listdir('/home/hux/books/python'):
if filename.endswith('.pdf'):
pdfFiles.append('/home/hux/books/python/'+filename)
pdfFiles.sort(key = str.lower)
pdfWriter = PyPDF2.PdfFileWriter()
# Loop through all the PDF files.
for filename in pdfFiles:
pdfFileObj = open(filename, 'rb')
pdfReader = PyPDF2.PdfFileReader(pdfFileObj, strict=False)
for pageNum in range(1, pdfReader.numPages):
pageObj = pdfReader.getPage(pageNum)
pdfWriter.addPage(pageObj)
pdfOutput = open('allminutes.pdf', 'wb')
pdfWriter.write(pdfOutput)
pdfOutput.close()
總結(jié)
以上是生活随笔為你收集整理的python合并多个pdf_python合并多个pdf文件的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: LaTeX:equation, alig
- 下一篇: python 日志不会按照日期分割_dj