其他分享
首页 > 其他分享> > PDF文档的合并与拆分.py

PDF文档的合并与拆分.py

作者:互联网

# -*- coding: utf-8 -*-
# 合并多个PDF文档
from PyPDF2 import PdfFileReader, PdfFileWriter


def demo1():
    pdf_writer = PdfFileWriter()
    name_list = ["file/a.pdf", "file/b.pdf", "file/c.pdf"]
    for i in name_list:
        pdf_reader = PdfFileReader(i)
        pages = pdf_reader.getNumPages()
        for j in range(pages):
            content = pdf_reader.getPage(j)
            pdf_writer.addPage(content)

    with open("file/new_file/合并后PDF.pdf", "wb") as p:
        pdf_writer.write(p)


# 拆分PDF文档
def demo2():
    pdf_reader = PdfFileReader("file/历年中国电影票房榜.pdf")
    pages = pdf_reader.getNumPages()
    for i in range(pages):
        pdf_writer = PdfFileWriter()
        content = pdf_reader.getPage(i)
        pdf_writer.addPage(content)
        with open(f"file/new_file/{i+2012}年中国电影票房榜单.pdf", "wb") as p:
            pdf_writer.write(p)


# 调用合并函数
demo1()
# 调用拆分函数
demo2()

 

标签:py,writer,reader,content,文档,file,pdf,PDF
来源: https://www.cnblogs.com/zhaoyiguang/p/16671310.html