0

我正在尝试从 PDF 文件中提取特定页面,并将其保存为不同的文件名。遵循此处提供的代码: https ://www.youtube.com/watch?v=W6Gt57b3Pp4&t=219s

from PyPDF2 import PdfFileReader, PdfFileWriter
pdf_file_path = "oranges.pdf"
file_base_name = pdf_file_path.replace(".pdf","")

pdf = PdfFileReader(pdf_file_path) #Creating PDF instance
pages = [0,2,4]

pdfwriter = PdfFileWriter() #Creating pdfWriter instance
print

for page_num in pages:
    pdfwriter().addPage(pdf.getPage(page_num))
with open("{0}_subset".format(file_base_name),'wb') as f:
    pdfwriter.write(f)
    f.close

但是我在第 12 行收到了这个错误: TypeError: 'PdfFileWriter' object is not callable 我应该如何解决这个错误?

4

1 回答 1

0

除非对象指向方法,否则不能对对象调用方法调用。

将行更改pdfwriter().addPage()pdfwriter.addPage()

from PyPDF2 import PdfFileReader, PdfFileWriter
pdf_file_path = "oranges.pdf"
file_base_name = pdf_file_path.replace(".pdf","")

pdf = PdfFileReader(pdf_file_path) #Creating PDF instance
pages = [0,2,4]

pdfwriter = PdfFileWriter() #Creating pdfWriter instance


for page_num in pages:
    pdfwriter.addPage(pdf.getPage(page_num))

with open("{0}_subset".format(file_base_name),'wb') as f:
    pdfwriter.write(f)
    f.close()
于 2020-07-12T07:24:03.800 回答