TypeError: 'PdfFileWriter' object is not callable

TypeError: 'PdfFileWriter' object is not callable

我正在尝试从 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 行收到此错误: 类型错误:'PdfFileWriter' 对象不可调用 我该如何解决这个错误?

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

将行 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()