python操作PDF------分割及合并PDF

# 安装 pip install pypdf2
import os
from PyPDF2 import PdfFileReader, PdfFileWriter

dir_name = '分割后PDF文件'
if not os.path.exists(dir_name):
    os.mkdir(dir_name)


# 用pypdf2分割PDF
pdf_reader = PdfFileReader('基于python的网页爬虫.pdf')
for page in range(pdf_reader.getNumPages()):  # 读取所有页数  从0开始
    pdf_writer = PdfFileWriter()
    pdf_writer.addPage(pdf_reader.getPage(page))
    with open(dir_name + '/{}.pdf'.format(page+1), 'wb') as f:
        pdf_writer.write(f)


# 用pypdf2合并PDF
pdf_writer = PdfFileWriter()
# 要合并的PDF数量
for page in range(17):
    pdf_reader = PdfFileReader(dir_name + '/{}.pdf'.format(page+1))
    # 每个PDF的页数
    for page in range(pdf_reader.getNumPages()):
        pdf_writer.addPage(pdf_reader.getPage(page))
with open('merged.pdf', 'wb') as f:
    pdf_writer.write(f)

 

posted @ 2020-09-04 19:13  不会飞的鲨鱼  阅读(666)  评论(0编辑  收藏  举报