使用Python自定義頁數分割PDF文件


需求:

環境准備:

1、Pyhon3以上+PyPDF2 

2、代碼與需要分割的PDF放在同一目錄下

代碼如下(簡版):

from PyPDF2 import PdfFileReader, PdfFileWriter
import os
def pdf_splitter(path,start,end):
    fname = os.path.splitext(os.path.basename(path))[0]
    pdf = PdfFileReader(path)
    pdf_writer = PdfFileWriter()
    output_filename = '{}_page_{}.pdf'.format(start,end)
    for page in range(start,end):        
        pdf_writer.addPage(pdf.getPage(page))
        print(page)
    
    with open(output_filename,'wb') as out:
        pdf_writer.write(out)
        print('Created:{}'.format(output_filename))

start = 23
end = 34
path = '2.pdf'
pdf_splitter(path,start,end)
        

  

過程中遇到的問題:

1、PdfReadError: File has not been decrypted

解決方案:

    參考鏈接:https://blog.csdn.net/xunmengpiaoyun/article/details/83146125

 

相關鏈接:https://blog.csdn.net/Leafage_M/article/details/79705731


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM