如何使用Python从pdf文件中删除页面? [英] How to delete pages from pdf file using Python?

查看:1364
本文介绍了如何使用Python从pdf文件中删除页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些.pdf文件,其页数超过500,但是每个文件只需要几页.必须保留文档的标题页.我确切知道该程序应删除的页面数.如何使用安装在MS Visual Studio上的Python 2.7 Environment做到这一点?

I have some .pdf files with more than 500 pages, but I need only a few pages in each file. It is necessary to preserve document`s title pages. I know exactly the numbers of the pages that program should remove. How I can do it using Python 2.7 Environment, which is installed upon MS Visual Studio?

推荐答案

尝试使用 PyPDF2 .

创建一个新文档并添加所有您不想删除的页面,而不是删除页面.

Instead of deleting pages, create a new document and add all pages which you don't want to delete.

一些示例代码(最初改编自已死的BinPress,已在此处存档) ).

Some sample code (originally adapted from BinPress which is dead, archived here).

from PyPDF2 import PdfFileWriter, PdfFileReader
pages_to_keep = [1, 2, 10] # page numbering starts from 0
infile = PdfFileReader('source.pdf', 'rb')
output = PdfFileWriter()

for i in pages_to_keep:
    p = infile.getPage(i)
    output.addPage(p)

with open('newfile.pdf', 'wb') as f:
    output.write(f)

from PyPDF2 import PdfFileWriter, PdfFileReader
pages_to_delete = [3, 4, 5] # page numbering starts from 0
infile = PdfFileReader('source.pdf', 'rb')
output = PdfFileWriter()

for i in range(infile.getNumPages()):
    if i not in pages_to_delete:
        p = infile.getPage(i)
        output.addPage(p)

with open('newfile.pdf', 'wb') as f:
    output.write(f)

这篇关于如何使用Python从pdf文件中删除页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆