處理大量的 PDF 文檔是非常麻煩的事情,頻繁地打開(kāi)關(guān)閉文件會(huì)嚴(yán)重影響工作效率。對(duì)于一大堆內(nèi)容相關(guān)的 PDF 文件,我們?cè)谔幚頃r(shí)可以將這些 PDF 文件合并起來(lái),作為單一文件處理,從而提高處理效率。同時(shí),我們也可以選取不同PDF文件中想要的頁(yè)面制作新的 PDF 文件。本文將介紹如何利用 Python 合并 PDF 文件以及選取頁(yè)面組成新的PDF文件。
使用工具:Spire.PDF for Python
PyPI:pip install Spire.Doc
通過(guò) MergeFiles () 方法直接合并 PDF 文件
MergeFiles(List[str]) 方法可以將一個(gè)文件路徑列表對(duì)應(yīng)的所有 PDF 文件按列表順序合并為一個(gè) PDF 文件。操作示例如下:
- 遍歷文件夾,創(chuàng)建 PDF 文件路徑的列表。
- 使用 PdfDocument.MergeFiles() 方法合并列表對(duì)應(yīng)的 PDF 文件,得到一個(gè) PdfDocumentBase 對(duì)象。
- 使用 PdfDocumentBase.Save() 方法保存合并結(jié)果。
Python文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-738304.html
from spire.pdf.common import *
from spire.pdf import *
import os
# 指定文件夾路徑
folder_path = "G:/文檔/"
# 遍歷文件夾中的文件并創(chuàng)建文件路徑列表
pdf_files = []
for file_name in sorted(os.listdir(folder_path)):
if file_name.endswith(".pdf"):
file_path = os.path.join(folder_path, file_name)
pdf_files.append(file_path)
# 合并PDF文檔
pdf = PdfDocument.MergeFiles(pdf_files)
# 保存結(jié)果文檔
pdf.Save("output/合并PDF.pdf", FileFormat.PDF)
pdf.Close()
通過(guò)插入頁(yè)面合并 PDF 文檔
AppendPage(PdfDocument) 方法可以在一個(gè) PDF 文件中插入另一個(gè) PDF 文件的所有頁(yè)面,從而實(shí)現(xiàn)合并PDF文件。以下是通過(guò)這種方法合并 PDF 文件的操作示例:
- 載入文件夾下的 PDF 文件為 PdfDocument 對(duì)象并創(chuàng)建列表。
- 創(chuàng)建一個(gè)新的 PdfDocument 對(duì)象。
- PdfDocument.AppendPage(PdfDocument) 方法將載入的 PDF 文件的頁(yè)面插入到新的 PDF 文件中。
- 使用 PdfDocument.SaveToFile() 方法保存新的 PDF 文件。
Python
from spire.pdf.common import *
from spire.pdf import *
# 遍歷文件夾中的文件,載入每個(gè)PDF文件PdfDocument對(duì)象并列表
folder_path = "G:/文檔/"
pdf_files = []
for file_name in sorted(os.listdir(folder_path)):
if file_name.endswith(".pdf"):
file_path = os.path.join(folder_path, file_name)
pdf_files.append(PdfDocument(file_path))
# 創(chuàng)建一個(gè)PdfDocument對(duì)象
newPdf = PdfDocument()
# 將加載的PDF文檔的頁(yè)面插入到新的PDF文檔中
for pdf in pdf_files:
newPdf.AppendPage(pdf)
# 保存新的PDF文檔
newPdf.SaveToFile("output/插入頁(yè)面合并PDF.pdf")
合并不同 PDF 文件的指定頁(yè)面
InsertPage(PdfDocument, pageIndex: int) 方法可以將一個(gè) PDF 文件的指定頁(yè)面插入到另一個(gè) PDF 文件中。我們可以通過(guò)這個(gè)方法合并不同 PDF 文件的指定頁(yè)面。以下是操作示例:
- 創(chuàng)建 PDF 文件路徑列表。
- 載入 PDF 文件為 PdfDocument 對(duì)象并創(chuàng)建列表。
- 創(chuàng)建新的 PdfDocument 對(duì)象。
- 使用 PdfDocument.InsertPage() 方法插入指定 PDF 文件的指定頁(yè)面到新的 PDF 文件種。
- 使用 PdfDocument.SaveToFile() 方法保存新的 PDF 文件。
Python
from spire.pdf import *
from spire.pdf.common import *
# 創(chuàng)建PDF文件路徑列表
file1 = "示例1.pdf"
file2 = "示例2.pdf"
file3 = "示例3.pdf"
files = [file1, file2, file3]
# 加載每個(gè)PDF文件并添加到列表中
pdfs = []
for file in files:
pdfs.append(PdfDocument(file))
# 創(chuàng)建一個(gè)PdfDocument對(duì)象
newPdf = PdfDocument()
# 將加載的PDF文檔中選擇的頁(yè)面插入到新文檔中
newPdf.InsertPage(pdfs[0], 0)
newPdf.InsertPage(pdfs[1], 1)
newPdf.InsertPageRange(pdfs[2], 0, 1)
# 保存新的PDF文檔
newPdf.SaveToFile("output/合并不同PDF的指定頁(yè)面.pdf")
以上是關(guān)于如何使用 Spire.PDF for Pytho 合并 PDF 文件的操作介紹。如果你想了解更多此 API 的功能,可前往 Spire.PDF for Python 中文教程了解。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-738304.html
到了這里,關(guān)于Python操作PDF:PDF文件合并與PDF頁(yè)面重排的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!