Python使用pypdf按指定頁碼范圍批量拆分PDF
在處理電子書、掃描書籍或技術(shù)文檔時,經(jīng)常會遇到一個需求:
按照指定頁碼范圍,把一個 PDF 拆分成多個 PDF 文件(例如按章節(jié)拆分)
本文將介紹一種簡單、穩(wěn)定、無需外部依賴的方法,使用 Python 的 pypdf 庫來實現(xiàn) PDF 的批定頁碼分割。
一、環(huán)境準備
Python 版本
Python 3.8+(推薦 3.9 / 3.10 / 3.11)
可用以下命令確認:
python --version
安裝 pypdf
使用 pip 安裝最新版 pypdf:
pip install pypdf
如果你在 Linux / macOS 上,且存在 Python2/3 共存問題,可使用:
pip3 install pypdf
安裝完成后測試是否成功:
from pypdf import PdfReader, PdfWriter
print("pypdf installed OK")
二、實現(xiàn)思路說明
關(guān)鍵點解析
- PdfReader:讀取原始 PDF
- PdfWriter:創(chuàng)建新的 PDF 文件
- PDF 頁碼從 0 開始,而我們?nèi)粘?吹降氖菑?1 開始
- 用戶只需定義一個頁碼范圍列表即可完成拆分
適用場景
- 按目錄拆書
- 按章節(jié)導出
- 按頁碼人工校正后的分割
三、完整 Python 實現(xiàn)代碼
from pypdf import PdfReader, PdfWriter
import os
def split_pdf_by_page_ranges(input_pdf, output_folder, ranges):
reader = PdfReader(input_pdf)
# 創(chuàng)建輸出目錄
if not os.path.exists(output_folder):
os.makedirs(output_folder)
for idx, (start_page, end_page) in enumerate(ranges, 1):
writer = PdfWriter()
# PDF 內(nèi)部頁碼從 0 開始,因此要 -1
for page_num in range(start_page - 1, end_page):
if page_num < len(reader.pages):
writer.add_page(reader.pages[page_num])
else:
print(f"Warning: page {page_num + 1} out of range.")
output_filename = (
f"{output_folder}/chapter_{idx}_pages_{start_page}-{end_page}.pdf"
)
with open(output_filename, "wb") as output_file:
writer.write(output_file)
print(f"Saved {output_filename}")
四、自定義章節(jié)頁碼范圍
你只需要定義一個 頁碼區(qū)間列表,格式如下:
# ?? 自定義分章節(jié)頁碼(格式:[(開始頁, 結(jié)束頁), ...])
page_ranges = [
(1, 34), # 序
(35, 50), # 第一章
(51, 73), # 第二章
(74, 93), # 第三章
(94, 118), # 第四章
(119, 152), # 第五章
(153, 166), # 第六章
(167, 183), # 第七章
(184, 206), # 第八章
(207, 230), # 第九章
(231, 251), # 第十章
]
注意事項:
- 頁碼是 PDF 閱讀器中看到的頁碼
- 不需要關(guān)心 0 / 1 的問題,代碼已處理
- 超出 PDF 總頁數(shù)會自動提示 Warning,不會報錯
五、執(zhí)行拆分
split_pdf_by_page_ranges(
"input.pdf",
"./output_manual_split",
page_ranges
)
執(zhí)行后目錄結(jié)構(gòu)如下:
output_manual_split/
├── chapter_1_pages_1-34.pdf
├── chapter_2_pages_35-50.pdf
├── chapter_3_pages_51-73.pdf
├── ...
└── chapter_11_pages_231-251.pdf
完整代碼
from pypdf import PdfReader, PdfWriter
import os
def split_pdf_by_page_ranges(input_pdf, output_folder, ranges):
reader = PdfReader(input_pdf)
if not os.path.exists(output_folder):
os.makedirs(output_folder)
for idx, (start_page, end_page) in enumerate(ranges, 1):
writer = PdfWriter()
# 頁碼從 0 開始,用戶輸入通常是從 1 開始
for page_num in range(start_page - 1, end_page):
if page_num < len(reader.pages):
writer.add_page(reader.pages[page_num])
else:
print(f"Warning: page {page_num + 1} out of range.")
output_filename = f"{output_folder}/chapter_{idx}_pages_{start_page}-{end_page}.pdf"
with open(output_filename, "wb") as output_file:
writer.write(output_file)
print(f"Saved {output_filename}")
# ?? 自定義你的分章節(jié)頁碼(格式:[(開始頁, 結(jié)束頁), ...])
page_ranges = [
(1, 34), # 序
(35, 50), # 第一章
(51, 73), # 第二章
(74,93), # 第三章
(94,118), # 第四章
(119,152), # 第五章
(153,166), # 第六章
(167,183), # 第七章
(184,206), # 第八章
(207,230), # 第九章
(231,251), # 第十章
]
# 用法示例
split_pdf_by_page_ranges("input.pdf", "./output_manual_split", page_ranges)
以上就是Python使用pypdf按指定頁碼范圍批量拆分PDF的詳細內(nèi)容,更多關(guān)于Python pypdf拆分PDF的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Python 數(shù)據(jù)處理庫 pandas 入門教程基本操作
pandas是一個Python語言的軟件包,在我們使用Python語言進行機器學習編程的時候,這是一個非常常用的基礎(chǔ)編程庫。本文是對Python 數(shù)據(jù)處理庫 pandas 入門教程,非常不錯,感興趣的朋友一起看看吧2018-04-04
Python3 shutil(高級文件操作模塊)實例用法總結(jié)
在本篇文章里小編給大家整理的是一篇關(guān)于Python3 shutil實例用法內(nèi)容,有興趣的朋友們可以學習下。2020-02-02
Pytorch實現(xiàn)簡單自定義網(wǎng)絡(luò)層的方法
這篇文章主要給大家介紹了關(guān)于Pytorch實現(xiàn)簡單自定義網(wǎng)絡(luò)層的相關(guān)資料,文中通過實例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2022-05-05

