切分pdf脚本

安装依赖

requirement.txt

PyPDF2~=2.0

安装依赖

python3 -m pip install -r requirement.txt

切分pdf脚本

from PyPDF2 import PdfFileWriter, PdfFileReader

def pdf_split(pdf_in,pdf_out,start,end):
    # 初始化一个pdf
    output = PdfFileWriter()
    # 读取pdf
    with open(pdf_in,'rb') as in_pdf:
        pdf_file = PdfFileReader(in_pdf)
        # 从pdf中取出指定页
        for i in range(start, end):
            output.addPage(pdf_file.getPage(i))
        # 写出pdf
        with open(pdf_out,'wb') as out_pdf:
            print('Writing to', pdf_out)
            output.write(out_pdf)

if __name__ == '__main__':
    pdf_in = 'in.pdf'
    pdf_out = 'out.pdf'
    start = 0
    end = 10
    pdf_split(pdf_in, pdf_out, start, end)
posted @ 2023-04-08 16:47  BuzzWeek  阅读(21)  评论(0编辑  收藏  举报