python 删除pdf 空白页

环境

python == 3.10
PyPDF2 ==3.0.1

安装

pip install PyPDF2

流程

  • 将空白页和内容页读取出来,看看内部结构有什么不同
  • 以此为依据,遍历整个PDF 文件,标记处有内容的页面,写入到另外一个PDF文件。

python 代码

# 每一个页都是一个字典对象,看第一层没区别
# 参考文章中 第一层 keys 一样, 但是 /Resources下结构有所不同,空白页没有"/XObject"键
# 我的第一层keys 不一样,  但是 /Resources下结构一样
# 另外 PyPDF2 版本不一样,各个模块有更新,自己看源码进行更新,或者根据报错提示进行更新

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中的空白页

  • 10
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python删除Word空白页有几种方法。一种常见的方法是使用Python-docx库来处理Word文档。你可以使用该库打开Word文档,并遍历每一页,判断是否为空白页,然后删除该页。下面是一个示例代码: ```python from docx import Document def delete_blank_pages(filename): doc = Document(filename) i = 0 while i < len(doc.sections): section = doc.sections[i] header = section.header footer = section.footer # 判断页眉页脚是否为空 if not header.is_header_empty or not footer.is_footer_empty: i += 1 continue page = section._element.getnext() if page is None: i += 1 continue # 判断页面内容是否为空 for element in page.iter(): if element.text.strip(): break else: doc.sections[i]._element.getparent().remove(section._element) continue i += 1 doc.save('result.docx') delete_blank_pages('input.docx') ``` 另一种方法是使用PyPDF2库处理PDF文档,首先将Word文档转换为PDF,然后使用PyPDF2库打开PDF文档,遍历每一页,判断是否为空白页,然后删除该页。下面是一个示例代码: ```python import PyPDF2 def delete_blank_pages(filename): pdf = PyPDF2.PdfReader(filename) writer = PyPDF2.PdfWriter() for page_number in range(len(pdf.pages)): page = pdf.pages<span class="em">1</span><span class="em">2</span> #### 引用[.reference_title] - *1* [【Word】删除word文档中的空白页](https://blog.csdn.net/qq_57268251/article/details/125898606)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [文章管理系统](https://download.csdn.net/download/yanxiaogang888/8230209)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值