使用Python實現(xiàn)在PDF中添加、導(dǎo)入、復(fù)制、移動與刪除頁面
在日常辦公和自動化任務(wù)中,我們經(jīng)常需要對 PDF 文件進(jìn)行頁面級的編輯,例如插入空白頁、復(fù)制現(xiàn)有頁、導(dǎo)入其他文件的頁面或刪除不需要的頁面。使用 Python,你可以輕松實現(xiàn)這些操作,而無需依賴 Adobe Acrobat。
本文將通過幾個常見場景,演示如何使用 Python 操作 PDF 頁面,包括:
- 添加空白頁
- 導(dǎo)入其他 PDF 的頁面
- 刪除特定頁面
- 在文檔內(nèi)部復(fù)制頁面
- 移動頁面到新位置
所有示例均基于 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) # 使用與第一頁相同的大小
# 可選:在新頁面上繪制文本內(nèi)容
# 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可在新頁面上繪制文本內(nèi)容,用于添加標(biāo)題或標(biāo)注。
結(jié)果展示:

2. 從另一個 PDF 導(dǎo)入頁面
from spire.pdf.common import *
from spire.pdf import *
# 加載目標(biāo)和源PDF文檔
target_document = PdfDocument()
target_document.LoadFromFile("G:/Documents/Sample53.pdf")
source_document = PdfDocument()
source_document.LoadFromFile("G:/Documents/Sample89.pdf")
# 導(dǎo)入源文檔的第一頁到目標(biāo)文檔的末尾
target_document.InsertPage(source_document, 0)
# 若要導(dǎo)入所有頁面,可使用循環(huán)
# 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("頁面已從源文檔導(dǎo)入。")說明:
InsertPage(source_document, page_index)用于將指定頁從一個 PDF 插入到另一個 PDF。- 當(dāng)源文檔包含多頁時,可遍歷其頁面進(jìn)行批量導(dǎo)入。
- 這種方法非常適合將多個文件合并成一個完整文檔。
結(jié)果展示:

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。 - 刪除頁面后應(yīng)重新保存文件以應(yīng)用更改。
此方法常用于去除封面頁、空白頁或廣告頁等不必要內(nèi)容。
結(jié)果展示:

4. 在文檔內(nèi)部復(fù)制頁面
from spire.pdf.common import *
from spire.pdf import *
document = PdfDocument()
document.LoadFromFile("G:/Documents/Sample53.pdf")
# 復(fù)制第一頁(索引為0)
if document.Pages.Count > 0:
document.InsertPage(document, 0)
document.SaveToFile("output_copy_page_within_doc.pdf", FileFormat.PDF)
document.Close()
print("頁面已在文檔內(nèi)復(fù)制。")說明:
InsertPage(document, page_index)可將同一文檔的指定頁復(fù)制到文檔末尾。- 這對于創(chuàng)建模板頁或重復(fù)頁的報表場景非常實用。
- 若要插入到特定位置,可使用帶插入位置參數(shù)的重載方法。
結(jié)果展示:

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: 導(dǎo)出要移動的頁面為單獨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: 重新加載導(dǎo)出的頁面
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 暫不支持直接移動頁面,因此可通過“導(dǎo)出 + 刪除 + 導(dǎo)入”實現(xiàn)。
- 使用
InsertPage(imported_doc, target_index, source_index)可以將頁面插入到任意位置。 - 此方法靈活可靠,尤其適用于需要調(diào)整頁面順序的情況。
結(jié)果展示:

6. 關(guān)鍵類與方法總結(jié)
| 操作類型 | 方法或?qū)傩?/th> | 說明 |
|---|---|---|
| 添加空白頁 | Pages.Add() | 在文檔末尾創(chuàng)建新頁面,可指定頁面尺寸 |
| 插入指定位置 | Pages.Insert(index) | 在指定索引位置插入空白頁 |
| 導(dǎo)入頁面 | InsertPage(source_doc, page_index) | 將其他 PDF 文件中的頁面導(dǎo)入到當(dāng)前文檔 |
| 刪除頁面 | Pages.RemoveAt(index) | 刪除指定頁面 |
| 復(fù)制頁面 | InsertPage(document, page_index) | 將當(dāng)前文檔的某頁復(fù)制到文檔末尾 |
| 移動頁面 | “導(dǎo)出→刪除→插入”組合 | 實現(xiàn)頁面位置調(diào)整 |
7. 總結(jié)
通過以上示例可以看到,Spire.PDF for Python 為 PDF 頁面級操作提供了簡潔而強大的接口。無論是添加、復(fù)制、導(dǎo)入還是刪除頁面,都可以通過幾行代碼完成。
這種編程式處理方式特別適合批量文檔編輯、自動報表生成或文件結(jié)構(gòu)整理等場景。無需安裝 Acrobat,就能輕松構(gòu)建自己的 PDF 管理工具。
以上就是使用Python實現(xiàn)在PDF中添加、導(dǎo)入、復(fù)制、移動與刪除頁面的詳細(xì)內(nèi)容,更多關(guān)于Python操作PDF頁面的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
使用Python實現(xiàn)將中文語音翻譯成英語音頻功能
本文介紹了中文語音翻譯成英語音頻的實現(xiàn)方法,主要分為三個步驟:語音識別(將中文語音轉(zhuǎn)為文本)、文本翻譯(中文轉(zhuǎn)英文)和語音合成(英文文本轉(zhuǎn)音頻),文章詳細(xì)說明了注意事項,需要的朋友可以參考下2025-08-08
TensorFlow實現(xiàn)模型斷點訓(xùn)練,checkpoint模型載入方式
這篇文章主要介紹了TensorFlow實現(xiàn)模型斷點訓(xùn)練,checkpoint模型載入方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-05-05
python實現(xiàn)win桌面通知、出現(xiàn)彈窗(三種方式)
本文主要介紹了python實現(xiàn)win桌面通知、出現(xiàn)彈窗(三種方式),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-11-11
Python 序列化和反序列化庫 MarshMallow 的用法實例代碼
marshmallow(Object serialization and deserialization, lightweight and fluffy.)用于對對象進(jìn)行序列化和反序列化,并同步進(jìn)行數(shù)據(jù)驗證。這篇文章主要介紹了Python 序列化和反序列化庫 MarshMallow 的用法實例代碼,需要的朋友可以參考下2020-02-02

