PyPDF2切割多页为单页

from PyPDF2 import PdfFileReader, PdfFileWriter
import os

def split_pdf(infile, out_path):
    """
    :param infile: 待拆分的pdf文件
    :param out_path: 拆分成单页的pdf文件的存储路径
    :return: 无
    """

    if not os.path.exists(out_path):
        os.makedirs(out_path)
    with open(infile, 'rb') as infile:
    
        pdfReader = PdfFileReader(infile)
        number_of_pages = pdfReader.getNumPages()  #计算此PDF文件中的页数
        
        for i in range(number_of_pages):
            pdfReader=PdfFileReader(infile)   #一定要重新读取,要不会报错。  
            #print(i)
            pdfWriter = PdfFileWriter()    #循环创建空白的pdf
            pdfWriter.addPage(pdfReader.getPage(i))    
            out_file_name = out_path + str(i+1)+'.pdf'
            print(out_path + str(i+1)+'.pdf')
            with open(out_file_name, 'wb') as outfile:
                pdfWriter.write(outfile) 

if __name__ == '__main__':
    in_File = './P320200037.pdf'
    out_Path = './Single/'  # 生成输出文件夹
    split_pdf(in_File, out_Path)

 

 
参考:
 
posted @ 2020-04-14 22:21  活捉火星人  阅读(252)  评论(0编辑  收藏  举报