P13.PDF合并及页面删减、排序、旋转

P13.PDF合并及页面删减、排序、旋转.md

#分割及合并PDF
    #利用pypdf2分割PDF
    PdfFileReader()、PdfFileWriter()
    例:
    from PyPDF2 import PdfFileReader, PdfFileWriter

    pdf_reader = PdfFileWriter('Netease Q2 2019 Earnings Release-Final.pdf')
    for page in range(pdf_reader.getNumPages()):
        pdf_writer = PdfFileWriter()
        pdf_writer.addPage(pdf_reader.getPage(page))
        with open(f'./分割后PDF文件/Netease Q2 2019 Earnings {page}.pdf', 'wb') as out:
            pdf_writer.write(out)

    #利用pypdf2合并PDF
    例:
    from PyPDF2 import PdfFileReader, PdfFileWriter

    pdf_writer = PdfFileWriter()
    for page in range(16)
        pdf_reader = PdfFileReader(f'./分割后PDF文件/Netease Q2 2019 Earnings {page}.pdf')
        for page in range(pdf_reader.getNumPages()):
            pdf_writer.addPage(pdf_reader.getPage(page))
    with open('merged.pdf','wb') as out:
        pdf_writer.write(out)


#旋转及排序PDF
    #旋转某一页
    .rotateClockwise(90的倍数)     顺时针旋转90度
    .rotateCounterClockwise(90的倍数)      逆时针旋转90度
    例:
    pdf_reader = PdfFileReader(f'Netease Q2 2019 Earnings Release-Final.pdf')
    pdf_writer = PdfFileWriter()
    page = pdf_reader.getPage(0).rotateClockwise(90)
    pdf_writer.addPage(page)
    page = pdf_reader.getPage(0).rotateCounterClockwise(90)
    pdf_writer.addPage(page)
    with open('merged.pdf','wb') as out:
        pdf_writer.write(out)

    #排序PDF页面
    直接按照期望的顺序添加页面即可,此处例子为倒叙排列
    例:
    pdf_reader = PdfFileReader(f'Netease Q2 2019 Earnings Release-Final.pdf')
    pdf_writer = PdfFileWriter()
    for page in range(pdf_reader.getNumPages() - 1, -1, -1):
        pdf_writer.addPage(pdf_reader.getPage(page))
    with open('merged.pdf','wb') as out:
        pdf_writer.write(out)            
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 1024 设计师: 上身试试
应支付0元
点击重新获取
扫码支付

支付成功即可阅读