2
我有一些.pdf文件超过500页,但我只需要在每个文件中的几页。有必要保存文档的标题页面。我确切地知道程序应该删除的页面的数量。如何使用安装在MS Visual Studio上的Python 2.7环境来实现它?如何使用Python从pdf文件中删除页面?
我有一些.pdf文件超过500页,但我只需要在每个文件中的几页。有必要保存文档的标题页面。我确切地知道程序应该删除的页面的数量。如何使用安装在MS Visual Studio上的Python 2.7环境来实现它?如何使用Python从pdf文件中删除页面?
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 range(infile.getNumPages()):
if i in pages_to_keep:
p = infile.getPage(i)
output.addPage(p)
with open('newfile.pdf', 'wb') as f:
output.write(f)
此代码可以工作! – Alexander