欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python如何實(shí)現(xiàn)刪除pdf空白頁

 更新時間:2025年05月14日 09:07:39   作者:去追風(fēng),去看海  
這篇文章主要為大家詳細(xì)介紹了如何使用Python實(shí)現(xiàn)刪除pdf空白頁以及批量刪除掃描PDF中的空白頁,文中的示例代碼簡潔易懂,需要的小伙伴可以了解下

python 刪除pdf 空白頁

環(huán)境

python == 3.10

PyPDF2 ==3.0.1

安裝

pip install PyPDF2

流程

將空白頁和內(nèi)容頁讀取出來,看看內(nèi)部結(jié)構(gòu)有什么不同

以此為依據(jù),遍歷整個PDF 文件,標(biāo)記處有內(nèi)容的頁面,寫入到另外一個PDF文件。

python 代碼

# 每一個頁都是一個字典對象,看第一層沒區(qū)別
# 參考文章中 第一層 keys 一樣, 但是 /Resources下結(jié)構(gòu)有所不同,空白頁沒有"/XObject"鍵
# 我的第一層keys 不一樣,  但是 /Resources下結(jié)構(gòu)一樣
# 另外 PyPDF2 版本不一樣,各個模塊有更新,自己看源碼進(jìn)行更新,或者根據(jù)報錯提示進(jìn)行更新

from PyPDF2 import PdfReader, PdfWriter

def remove_pdf_blank_pages(path):
    pdfReader = PdfReader(open(path, 'rb'))
    writer = PdfWriter()
    pages = len(pdfReader.pages)
    # blank = pdfReader.pages[1]
    # full = pdfReader.pages[2]
    #print('*'*10)
    #print(blank.keys())# dict_keys(['/Type', '/Parent', '/Resources', '/MediaBox', '/Contents'])
    #print(full.keys())# dict_keys(['/Type', '/Parent', '/Resources', '/MediaBox', '/Annots', '/Tabs', '/StructParents', '/Contents'])
    #print(blank['/Resources'])
    #{'/Font': IndirectObject(600, 0, 139632281578944), '/XObject': {'/Im553': IndirectObject(553, 0, 139632281578944), '/Im7': IndirectObject(7, 0, 139632281578944)}, '/ProcSet': ['/PDF', '/Text', '/ImageC', '/ImageI', '/ImageB']}
    #print(full['/Resources'])
    #{'/Font': IndirectObject(600, 0, 139632281578944), '/XObject': {'/Im553': IndirectObject(553, 0, 139632281578944), '/Im7': IndirectObject(7, 0, 139632281578944)}, '/ProcSet': ['/PDF', '/Text', '/ImageC', '/ImageI', '/ImageB']}
    #print('*' * 10)
    for i in range(pages):
        page = pdfReader.pages[i]
        # if "/XObject" in page["/Resources"].keys() or "/Font" in page["/Resources"].keys():
        #     writer.add_page(page)
        if "/StructParents" in page.keys() or "/Tabs" in page.keys() or "/Annots" in page.keys():
            writer.add_page(page)

    writer.write(open(path, 'wb'))

使用Python批量刪除掃描PDF中的空白頁

對于經(jīng)??磼呙鑀DF資料的人來說,經(jīng)常會碰到如下問題:

PDF縮略圖

因為一些格式轉(zhuǎn)換的原因,一些空白頁時不時的出現(xiàn),而且規(guī)律不定,一會是偶數(shù)頁碼一會是奇數(shù)頁碼,逐個選中刪除的話,對于幾百頁的文檔,非常費(fèi)時。

百度搜索刪除PDF空白頁,得到的是一個要收費(fèi)的工具,有了Python就可以利用免費(fèi)開源庫輕松解決。

先安裝 PyPDF2庫,在Powershell 或CMD命令行模式安裝PyPDF2

Install PyPDF2

流程

將空白頁和內(nèi)容頁讀取出來,看看內(nèi)部結(jié)構(gòu)有什么不同,以此為依據(jù),遍歷整個PDF 文件,標(biāo)記處有內(nèi)容的頁面,寫入到另外一個PDF文件。

該文件中17頁為空白頁,18頁為內(nèi)容頁:

from PyPDF2 import PdfFileReader, PdfFileWriter

path=r"D:\ebook\PDF\test.pdf"

reader = PdfFileReader(open(path, 'rb'))
"""
注意PyPDF2中頁碼從0開始
"""
blank= reader.getPage(16)
full = reader.getPage(17)

每一個頁都是一個字典對象,看第一層沒區(qū)別

blank.keys()
Out[24]: dict_keys(['/Type', '/Contents', '/Parent', '/Resources', '/MediaBox'])

full.keys()
Out[25]: dict_keys(['/Type', '/Contents', '/Parent', '/Resources', '/MediaBox'])

經(jīng)查發(fā)現(xiàn)/Resources下結(jié)構(gòu)有所不同,空白頁沒有"/XObject"鍵:

blank['/Resources']
Out[26]: {'/ExtGState': {'/Opa0': {'/Type': '/ExtGState', '/CA': 1}}}

full['/Resources']
Out[27]: 
{'/ExtGState': {'/Opa0': {'/Type': '/ExtGState', '/CA': 1},
  '/Opa1': {'/Type': '/ExtGState', '/ca': 1}},
 '/XObject': {'/Image0': {'/BitsPerComponent': 8,
   '/Height': 1130,
   '/Filter': ['/DCTDecode'],
   '/ColorSpace': '/DeviceRGB',
   '/Type': '/XObject',
   '/Subtype': '/Image',
   '/DL': 434222,
   '/Width': 792}}}

所以對于有”/XObject“鍵的,就是有圖像的頁面。同時發(fā)現(xiàn)一些只有文字沒圖像的頁面,還有"/Font" 鍵,于是將有這兩個鍵的頁面標(biāo)記,然后寫入第二個PDF文件即可:

from PyPDF2 import PdfFileReader, PdfFileWriter

path = r"D:\ebook\PDF\test.pdf"
path_output = r"D:\ebook\PDF\output.pdf"

reader = PdfFileReader(open(path, 'rb'))
writer = PdfFileWriter()
pages = pdfReader.getNumPages()

for i in range(pages):
    page = reader.getPage(i)
    if "/XObject" in page["/Resources"].keys() or "/Font" in page["/Resources"].keys():
        writer.addPage(page)
    
writer.write(open(path_output, 'wb'))

到此這篇關(guān)于Python如何實(shí)現(xiàn)刪除pdf空白頁的文章就介紹到這了,更多相關(guān)Python刪除pdf空白頁內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論