用Python处理PDF:拆分与合并PDF文档

PDF文档在信息共享和数据保存方面被广泛使用,处理PDF文档也成为常见需求。其中,合并和拆分PDF文档能够帮助我们更有效地管理PDF文档,使文档内容分布更合理。通过合并,可以将相关文档整合成一个文件,以便更好地组织和提供信息;而通过拆分,可以将大型PDF文件分割成易于管理的较小文件,方便我们更容易地访问所需范围的信息。利用Python,我们可以高效地完成这些任务。本文将介绍如何使用Python来管理PDF文档,包括合并和分割操作的实现方法。

文章目录

本文所介绍的PDF文档操作方法需要用到Spire.PDF for Python,可从官网下载或通过PyPI(pip install Spire.PDF)安装。

使用 MergeFiles() 方法合并PDF文档

PdfDocument 类代表PDF文档。 在该类中,MergeFiles() 方法可用于直接将多个PDF文档合并为一个文档,方法的参数是文件路径列表。 具体实现过程如下:

  1. 创建PDF文件路径列表。
  2. 使用 PdfDocument.MergeFiles() 方法合并 =PDF 文档。
  3. 使用 PdfDocument.SaveToFile() 方法将合并后的文档保存为新的 PDF 文档。

代码示例:

python 复制代码
from spire.pdf.common import *
from spire.pdf import *

# 创建一个PDF文件路径的列表
inputFile1 = "Sample1.pdf"
inputFile2 = "Sample2.pdf"
inputFile3 = "Sample3.pdf"
files = [inputFile1, inputFile2, inputFile3]

# 合并PDF文档
pdf = PdfDocument.MergeFiles(files)

# 保存结果文档
pdf.Save("output/合并PDF.pdf", FileFormat.PDF)
pdf.Close()

合并效果

通过复制内容合并PDF文档

使用 PdfDocument.AppendPage(PdfDocument) 方法,还可以将一个PDF文件中的页面复制到另一个PDF文件中,从而完成PDF文件的合并。 具体步骤如下:

  1. 创建PDF文件路径列表。
  2. 载入每个PDF文档为 PdfDocument 类的对象,并添加到一个列表中。
  3. 创建一个新的 PdfDocument 类的对象。
  4. 循环遍历每个加载的 PDF 文档,使用 PdfDocument.appendPage() 方法将每个PDF文档的页面插入到新的PDF文档中。
  5. 使用 PdfDocument.SaveToFile() 方法保存新的 PDF 文档。

代码示例:

python 复制代码
from spire.pdf.common import *
from spire.pdf import *

# 创建 PDF 文件路径列表
file1 = "Sample1.pdf"
file2 = "Sample2.pdf"
file3 = "Sample3.pdf"
files = [file1, file2, file3]

# 加载每个 PDF 文件为 PdfDocument 对象并添加到列表中
pdfs = []
for file in files:
    pdfs.append(PdfDocument(file))

# 创建一个 PdfDocument 对象
newPdf = PdfDocument()

# 将加载的 PDF 文档的页面插入到新的 PDF 文档中
for pdf in pdfs:
    newPdf.AppendPage(pdf)

# 保存新的 PDF 文档
newPdf.SaveToFile("output/复制页面合并PDF.pdf")
newPdf.Close()

合并效果

合并PDF文档的选定页面

我们还可以使用 PdfDocument.InsertPage()PdfDocument.InsertPageRange() 方法从一个PDF文档中选择页面和插入到另一个PDF文档中,从而实现对指定PDF页面的合并。 具体步骤如下:

  1. 创建PDF文件路径列表。
  2. 载入每个PDF文档为 PdfDocument 类的对象,并添加到一个列表中。
  3. 创建一个新的 PdfDocument 类的对象。
  4. 使用 PdfDocument.InsertPage(PdfDocument, pageIndex: int)PdfDocument.InsertPageRange(PdfDocument, startIndex: int, endIndex: int) 方法将已加载文档的指定页面插入到新的PDF文档中。
  5. 使用 PdfDocument.SaveToFile() 方法保存新的PDF文档。

代码示例:

python 复制代码
from spire.pdf import *
from spire.pdf.common import *

# 创建 PDF 文件路径列表
file1 = "Sample1.pdf"
file2 = "Sample2.pdf"
file3 = "Sample3.pdf"
files = [file1, file2, file3]

# 加载每个 PDF 文件为 PdfDocument 对象并添加到列表中
pdfs = []
for file in files:
    pdfs.append(PdfDocument(file))

# 创建一个 PdfDocument 对象
newPdf = PdfDocument()

# 从加载的 PDF 文档中选择指定页插入到新的文档中
newPdf.InsertPage(pdfs[0], 0)
newPdf.InsertPage(pdfs[1], 2)
newPdf.InsertPageRange(pdfs[2], 0, 1)

# 保存新的 PDF 文档
newPdf.SaveToFile("output/选择页面合并PDF.pdf")
newPdf.Close()

合并效果

将PDF文档拆分为单页文档

PdfDocument.Split(PdfDocument) 方法可用于将多页PDF文档分割成多个单页PDF文件。 具体步骤如下:

  1. 创建一个 PdfDocument 类的对象,并使用 PdfDocument.LoadFromFile() 方法加载PDF文件。
  2. 使用 PdfDocument.Split() 方法将文档分割成多个单页PDF文件。

代码示例:

python 复制代码
from spire.pdf.common import *
from spire.pdf import *

# 创建一个 PdfDocument 对象
doc = PdfDocument()

# 加载 PDF 文件
doc.LoadFromFile("output/合并PDF.pdf")

# 将 PDF 文件拆分为多个单页的 PDF 文件
doc.Split("Output/PDF/拆分PDF-{0}.pdf", 1)
doc.Close()

拆分效果

按页面范围分割PDF文档

要按页面范围将PDF文件分割成两个或多个PDF文件,需要先创建两个或多个新的PDF文件,然后将源PDF中的特定页面或页面范围导入到新创建的PDF文件中。 以下是详细步骤:

  1. 创建一个 PdfDocument 类对象,并使用 PdfDocument.LoadFromFile() 方法加载PDF文件。
  2. 创建另外三个 PdfDocument 对象。
  3. 使用 PdfDocument.InsertPage() 方法从源文件中导入第一页到第一个文档中。
  4. 使用 PdfDocument.InsertPageRange() 方法从源文件中导入第 2-4 页到第二个文档中。
  5. 使用 PdfDocument.InsertPageRange() 方法将源文件中的其余页面导入到第三个文档中。
  6. 使用 PdfDocument.SaveToFile() 方法保存三个文档。

代码示例:

python 复制代码
from spire.pdf.common import *
from spire.pdf import *

# 创建一个 PdfDocument 对象
doc = PdfDocument()

# 加载 PDF 文件
doc.LoadFromFile("output/合并PDF.pdf")

# 创建三个 PdfDocument 对象
newDoc_1 = PdfDocument()
newDoc_2 = PdfDocument()
newDoc_3 = PdfDocument()

# 将源文件的第一页插入到第一个文档中
newDoc_1.InsertPage(doc, 0)

# 将源文件的第2至4页插入到第二个文档中
newDoc_2.InsertPageRange(doc, 1, 3)

# 将源文件的剩余页插入到第三个文档中
newDoc_3.InsertPageRange(doc, 4, doc.Pages.Count - 1)

# 保存三个文档
newDoc_1.SaveToFile("output/PDF1/自定义拆分PDF-1.pdf")
newDoc_2.SaveToFile("output/PDF1/自定义拆分PDF-2.pdf")
newDoc_3.SaveToFile("output/PDF1/自定义拆分PDF-3.pdf")

# 关闭文档对象
doc.Close()
newDoc_1.Close()
newDoc_2.Close()
newDoc_3.Close()

拆分结果

以上文章介绍了如何用Python操作PDF文档,进行PDF文档的合并和拆分。除了简单的合并和拆分外,上述方法还可用于自由操作PDF页面,从而组成新的PDF文档。Spire.PDF for Python 还有许多其他PDF文档操作功能,访问Spire.PDF for Python教程查看更多信息。

相关推荐
学Linux的语莫4 分钟前
python开发知识点
python
nbsaas-boot8 分钟前
slice / map 在 Go GC 与内存碎片上的真实成本
开发语言·后端·golang
会飞的小新19 分钟前
Shell 脚本中的信号与 trap:从 Ctrl+C 到优雅退出
linux·开发语言
LawrenceLan21 分钟前
Flutter 零基础入门(十):final、const 与不可变数据
开发语言·flutter·dart
Frdbio24 分钟前
环腺苷酸(cAMP)ELISA检测试剂盒
linux·人工智能·python
狗狗学不会34 分钟前
RK3588 极致性能:使用 Pybind11 封装 MPP 实现 Python 端 8 路视频硬件解码
人工智能·python·音视频
love530love1 小时前
EPGF 新手教程 21把“环境折磨”从课堂中彻底移除:EPGF 如何重构 AI / Python 教学环境?
人工智能·windows·python·重构·架构·epgf
ldccorpora1 小时前
Chinese News Translation Text Part 1数据集介绍,官网编号LDC2005T06
数据结构·人工智能·python·算法·语音识别
大学生毕业题目1 小时前
毕业项目推荐:99-基于yolov8/yolov5/yolo11的肾结石检测识别系统(Python+卷积神经网络)
人工智能·python·yolo·目标检测·cnn·pyqt·肾结石检测
源代码•宸1 小时前
Leetcode—1266. 访问所有点的最小时间【简单】
开发语言·后端·算法·leetcode·职场和发展·golang