Python 3-分割PDF档案

时间:2018-11-07 18:12:09

标签: python-3.x pypdf2

我希望从我的PDF文件中提取第30至34页并将其合并为一个文档。但是,我的代码似乎只能使我进入第34页。

import os
from PyPDF2 import PdfFileReader, PdfFileWriter

path = 'file_path'

pdf = PdfFileReader(path, "rb")

for page in range(29, 33):
    pdf_writer = PdfFileWriter()
    pdf_writer.addPage(pdf.getPage(page))

output_fname = "Output.pdf"

with open(output_fname, 'wb') as out:
    pdf_writer.write(out)

print ("PDF file has been split")

好吧,如果我更改了代码的中间部分以手动添加页面,我会注意到它可以正常工作。但是我确实需要一种不太笨拙的方法来提取大量页面。

output.addPage(pdf.getPage(29))
output.addPage(pdf.getPage(30))

2 个答案:

答案 0 :(得分:0)

Nvm我意识到我的错误。本应从循环语句中排除pdf_writer = PdfFileWriter()。

import os
from PyPDF2 import PdfFileReader, PdfFileWriter

path = 'file_path'
pdf = PdfFileReader(path, "rb")

pdf_writer = PdfFileWriter()

for page in range(29, 33):
    pdf_writer.addPage(pdf.getPage(page))

output_fname = "Output.pdf"

with open(output_fname, 'wb') as out:
    pdf_writer.write(out)

print ("PDF file has been split")

答案 1 :(得分:0)

import os
from PyPDF2 import PdfFileReader, PdfFileWriter

path = r"C:\Users\scriptdocuchm_ktm.pdf"

pdf = PdfFileReader(path, "rb")
pdflist = []
pdf_writer = PdfFileWriter()

for page in range(66,70):
    pdflist.append(page)
    pdf_writer.addPage(pdf.getPage(page))

print(pdflist)

output_filename = "split.pdf"

with open(output_filename, 'wb') as out:
    pdf_writer.write(out)

print ("pdf file has been split")