日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

PyPDF2 | 利用 Python 实现 PDF 分割

發布時間:2025/3/15 python 45 豆豆
生活随笔 收集整理的這篇文章主要介紹了 PyPDF2 | 利用 Python 实现 PDF 分割 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1. PDF 分割

由于疫情影響被迫在家上網課,因此教材也只能用電子版。但有一門教材是對開的掃描版,導致在 iPad 上閱讀很不友好,因此決定尋找一個工具將 PDF 對半分開。

圖1 分割前的 PDF

在百度了一番后,發現大多都是使用 Adobe Acrobat 軟件進行剪裁,這完全不 Pythonic,因此又找了用 Python 處理 PDF 文件的方法,最后發現了 PyPDF2 這個庫,本文將利用這個庫,實現對 PDF 的分割。

首先,你需要通過 pip 安裝這個庫:

pip install PyPDF2

實現切割 PDF 的思想很簡單,只要我們能測量出 PDF 的長寬,接著分別將左右裁剪拼接即可,而 PyPDF2 已經提供了這些功能:

# PdfFileReader 模塊用于讀取 pdf # PdfFileWriter 模塊用于創建要保存的 pdf from PyPDF2 import PdfFileReader, PdfFileWriter# 1. 讀取 pdf pdf_input = PdfFileReader(open('xxx.pdf', 'rb'))# 2. 創建要保存的 pdf 對象 pdf_output = PdfFileWriter()# 3. 選取第一頁 pdf 讀取長寬 page = pdf_input_left.getPage(0) width = float(page.mediaBox.getWidth()) height = float(page.mediaBox.getHeight())# 4. 計算 pdf 的總頁數 page_count = pdf_input_left.getNumPages()# 5. 修改某一頁 pdf 的尺寸 page = pdf_input.getPage(i) page.mediaBox.lowerLeft = (x,y) page.mediaBox.lowerRight = (x,y) page.mediaBox.upperLeft = (x,y) page.mediaBox.upperRight = (x,y)# 6. 將修改好的 pdf 添加到我們要輸出的文件中 pdf_output.addPage(page)# 7. 循環所有的頁數后,將文件輸出為 pdf 文件 pdf_output.write(open('xxx,pdf', 'wb'))

需要注意的是,PyPDF2 默認將較短的邊作為 X 軸,較長的邊作為 Y 軸,對應的坐標如下:

圖2 縱向比例下的 PyPDF2 坐標

然而我們的 PDF 是橫向比例的,如下圖所示:

圖3 橫向比例 PDF 示例

相當于:

圖4 橫向比例下的 PyPDF2 坐標

即:

圖5 旋轉后的橫向比例下的 PyPDF2 坐標

要注意與圖 1 坐標的區別。

在弄清楚了 PyPDF 的坐標后,我們就可以通過調整四個角的坐標來分別獲得左右兩個 PDF 了,對于左邊的 PDF,其對應的坐標為:

圖6 左半圖的 PyPDF2 坐標

因此坐標設置如下:

page_left.mediaBox.lowerLeft = (0, height/2) page_left.mediaBox.lowerRight = (width, height/2) page_left.mediaBox.upperLeft = (0, height) page_left.mediaBox.upperRight = (width, height)

而右半圖的坐標為:

圖7 右半圖的 PyPDF2 坐標

對應的坐標設置為:

page_right.mediaBox.lowerLeft = (0, 0) page_right.mediaBox.lowerRight = (width, 0) page_right.mediaBox.upperLeft = (0, height/2) page_right.mediaBox.upperRight = (width, height/2)

最后匯總得:

from PyPDF2 import PdfFileReader, PdfFileWriterinfile = '應用多元統計分析 高惠璇.pdf' outfile = '應用多元統計分析 高惠璇 split.pdf'pdf_input_left = PdfFileReader(open(infile, 'rb')) pdf_input_right = PdfFileReader(open(infile, 'rb')) pdf_output = PdfFileWriter()page = pdf_input_left.getPage(0) width = float(page.mediaBox.getWidth()) height = float(page.mediaBox.getHeight()) page_count = pdf_input_left.getNumPages()for i in range(page_count):# left pagepage_left = pdf_input_left.getPage(i)page_left.mediaBox.lowerLeft = (0, height/2)page_left.mediaBox.lowerRight = (width, height/2)page_left.mediaBox.upperLeft = (0, height)page_left.mediaBox.upperRight = (width, height)pdf_output.addPage(page_left)# right pagepage_right = pdf_input_right.getPage(i)page_right.mediaBox.lowerLeft = (0, 0)page_right.mediaBox.lowerRight = (width, 0)page_right.mediaBox.upperLeft = (0, height/2)page_right.mediaBox.upperRight = (width, height/2)pdf_output.addPage(page_right)pdf_output.write(open(outfile, 'wb'))

看下轉換效果,Bingo!

圖8 轉換后的 PDF 效果

2. 調整邊緣

轉換后發現,PDF 存在這黑邊,因此我們可以通過調整對應的坐標來減少黑邊的現象:

圖9 PDF 黑邊 from PyPDF2 import PdfFileReader, PdfFileWriter def pdf_split(infile, outfile, left_margin=0, right_margin=0, down_margin=0):pdf_input_left = PdfFileReader(open(infile, 'rb')) # 讀取切割為左邊的 pdfpdf_input_right = PdfFileReader(open(infile, 'rb')) # 讀取切割為右邊的 pdfpdf_output = PdfFileWriter() # 定義要保存的 pdfpage = pdf_input_left.getPage(0) # 選取第一頁 來讀取 pdf 的長寬width = float(page.mediaBox.getWidth())height = float(page.mediaBox.getHeight())page_count = pdf_input_left.getNumPages() # 讀取 pdf 頁數for i in range(page_count):# 切割左邊 pdfpage_left = pdf_input_left.getPage(i)page_left.mediaBox.lowerLeft = (0, height/2)page_left.mediaBox.lowerRight = (width, height/2)page_left.mediaBox.upperLeft = (down_margin, height-left_margin)page_left.mediaBox.upperRight = (width, height-left_margin)pdf_output.addPage(page_left)# 切割右邊 pdfpage_right = pdf_input_right.getPage(i)page_right.mediaBox.lowerLeft = (down_margin, right_margin)page_right.mediaBox.lowerRight = (width, right_margin)page_right.mediaBox.upperLeft = (down_margin, height/2)page_right.mediaBox.upperRight = (width, height/2)pdf_output.addPage(page_right)pdf_output.write(open(outfile, 'wb')) # 保存 pdfprint('Done!') infile = '應用多元統計分析 高惠璇.pdf' outfile = '應用多元統計分析 高惠璇 split.pdf' left_margin=10 right_margin=10 down_margin = 20 pdf_split(infile, outfile, left_margin, right_margin, down_margin) Done!

看下最后效果:

圖10 調整后的 PDF 黑邊情況

其他文章推薦

機器學習算法與 Python 實現專欄

SQL 入門教程專欄

總結

以上是生活随笔為你收集整理的PyPDF2 | 利用 Python 实现 PDF 分割的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。