使用Python实现在PDF中添加、导入、复制、移动与删除页面
作者:大丸子
在日常办公和自动化任务中,我们经常需要对 PDF 文件进行页面级的编辑,使用 Python,你可以轻松实现这些操作,而无需依赖 Adobe Acrobat,本文将通过几个常见场景,演示如何使用 Python 操作 PDF 页面,需要的朋友可以参考下
在日常办公和自动化任务中,我们经常需要对 PDF 文件进行页面级的编辑,例如插入空白页、复制现有页、导入其他文件的页面或删除不需要的页面。使用 Python,你可以轻松实现这些操作,而无需依赖 Adobe Acrobat。
本文将通过几个常见场景,演示如何使用 Python 操作 PDF 页面,包括:
- 添加空白页
- 导入其他 PDF 的页面
- 删除特定页面
- 在文档内部复制页面
- 移动页面到新位置
所有示例均基于 Free Spire.PDF for Python,你可以通过以下命令安装该库:
pip install spire.pdf.free
1. 向 PDF 添加空白页
from spire.pdf.common import *
from spire.pdf import *
document = PdfDocument()
document.LoadFromFile("G:/Documents/Sample53.pdf")
# 在文档末尾添加一个新页面
new_page = document.Pages.Add(document.Pages.get_Item(0).Size) # 使用与第一页相同的大小
# 可选:在新页面上绘制文本内容
# text_element = PdfTextWidget("这是一个新添加的空白页面", PdfFont(PdfFontFamily.Helvetica, 12))
# text_element.Draw(new_page, PointF(50, 50))
document.SaveToFile("output_add_blank_page.pdf", FileFormat.PDF)
document.Close()
print("空白页面已添加。")说明:
document.Pages.Add()会在文档末尾添加一个新页面,并返回该页面对象。- 若希望在指定位置插入页面,可使用
document.Pages.Insert(index)。 - 通过
PdfTextWidget可在新页面上绘制文本内容,用于添加标题或标注。
结果展示:

2. 从另一个 PDF 导入页面
from spire.pdf.common import *
from spire.pdf import *
# 加载目标和源PDF文档
target_document = PdfDocument()
target_document.LoadFromFile("G:/Documents/Sample53.pdf")
source_document = PdfDocument()
source_document.LoadFromFile("G:/Documents/Sample89.pdf")
# 导入源文档的第一页到目标文档的末尾
target_document.InsertPage(source_document, 0)
# 若要导入所有页面,可使用循环
# for i in range(source_document.Pages.Count):
# target_document.InsertPage(source_document, i)
target_document.SaveToFile("output_import_page.pdf", FileFormat.PDF)
target_document.Close()
source_document.Close()
print("页面已从源文档导入。")说明:
InsertPage(source_document, page_index)用于将指定页从一个 PDF 插入到另一个 PDF。- 当源文档包含多页时,可遍历其页面进行批量导入。
- 这种方法非常适合将多个文件合并成一个完整文档。
结果展示:

3. 删除 PDF 中的页面
from spire.pdf.common import *
from spire.pdf import *
document = PdfDocument()
document.LoadFromFile("input.pdf")
# 删除第二页(索引从0开始)
if document.Pages.Count > 1:
document.Pages.RemoveAt(1)
document.SaveToFile("output_delete_page.pdf", FileFormat.PDF)
document.Close()
print("页面已删除。")说明:
RemoveAt(index)可删除指定索引的页面。- 索引从
0开始,即第一页为0,第二页为1。 - 删除页面后应重新保存文件以应用更改。
此方法常用于去除封面页、空白页或广告页等不必要内容。
结果展示:

4. 在文档内部复制页面
from spire.pdf.common import *
from spire.pdf import *
document = PdfDocument()
document.LoadFromFile("G:/Documents/Sample53.pdf")
# 复制第一页(索引为0)
if document.Pages.Count > 0:
document.InsertPage(document, 0)
document.SaveToFile("output_copy_page_within_doc.pdf", FileFormat.PDF)
document.Close()
print("页面已在文档内复制。")说明:
InsertPage(document, page_index)可将同一文档的指定页复制到文档末尾。- 这对于创建模板页或重复页的报表场景非常实用。
- 若要插入到特定位置,可使用带插入位置参数的重载方法。
结果展示:

5. 移动页面到新的位置
from spire.pdf.common import *
from spire.pdf import *
document = PdfDocument()
document.LoadFromFile("G:/Documents/Sample53.pdf")
if document.Pages.Count > 1:
temp_path = "temp_page.pdf"
# Step 1: 导出要移动的页面为单独PDF
temp_doc = PdfDocument()
temp_doc.InsertPage(document, 1, 0)
temp_doc.SaveToFile(temp_path, FileFormat.PDF)
temp_doc.Close()
# Step 2: 删除原文档中的该页
document.Pages.RemoveAt(1)
# Step 3: 重新加载导出的页面
imported_doc = PdfDocument()
imported_doc.LoadFromFile(temp_path)
# Step 4: 插入到新位置(例如第一页前)
document.InsertPage(imported_doc, 0, 0)
document.SaveToFile("output_move_page.pdf", FileFormat.PDF)
document.Close()
print("页面已成功移动。")说明:
- Spire.PDF 暂不支持直接移动页面,因此可通过“导出 + 删除 + 导入”实现。
- 使用
InsertPage(imported_doc, target_index, source_index)可以将页面插入到任意位置。 - 此方法灵活可靠,尤其适用于需要调整页面顺序的情况。
结果展示:

6. 关键类与方法总结
| 操作类型 | 方法或属性 | 说明 |
|---|---|---|
| 添加空白页 | Pages.Add() | 在文档末尾创建新页面,可指定页面尺寸 |
| 插入指定位置 | Pages.Insert(index) | 在指定索引位置插入空白页 |
| 导入页面 | InsertPage(source_doc, page_index) | 将其他 PDF 文件中的页面导入到当前文档 |
| 删除页面 | Pages.RemoveAt(index) | 删除指定页面 |
| 复制页面 | InsertPage(document, page_index) | 将当前文档的某页复制到文档末尾 |
| 移动页面 | “导出→删除→插入”组合 | 实现页面位置调整 |
7. 总结
通过以上示例可以看到,Spire.PDF for Python 为 PDF 页面级操作提供了简洁而强大的接口。无论是添加、复制、导入还是删除页面,都可以通过几行代码完成。
这种编程式处理方式特别适合批量文档编辑、自动报表生成或文件结构整理等场景。无需安装 Acrobat,就能轻松构建自己的 PDF 管理工具。
以上就是使用Python实现在PDF中添加、导入、复制、移动与删除页面的详细内容,更多关于Python操作PDF页面的资料请关注脚本之家其它相关文章!
