实现Python pdf切割 ValueError: seek of closed file

参考网上的教材,实现pdf文件的切割,提示一个问题
ValueError: seek of closed file

原来是pdf文件关闭导致的问题。
将其改成一个程序就解决了。

import PyPDF2

pdf_path = r'E:\zhuanxie\jpm\2.pdf'
out_path =r'E:\zhuanxie\jpm\23.pdf'
# 切割PDF文件
start_page = 1
end_page = 10

with open(pdf_path, 'rb') as file:
    pdf = PyPDF2.PdfReader(file)

    output_pdf = PyPDF2.PdfWriter()
    for page_num in range(start_page - 1, end_page):
        output_pdf.add_page(pdf.pages[page_num])
        
    with open(out_path, 'wb') as file2:
        output_pdf.write(file2)

posted on 2024-03-11 21:17  耀扬  阅读(413)  评论(0编辑  收藏  举报

导航