python ——PDF合并与拆分

  1. 调用combine_pdf() 可以将桌面下所有的PDF合并为output.pdf
  2. 调用cut_pdf() , 输入一个桌面下面的pdf文件,可以拆分PDF
"""
Created on Thu Aug  2 16:02:10 2018
PyPDF2的一些应用:
拆分PDF文件(书签信息不丢失)
合并PDF文件(书签信息不丢失,并且以原文件名字生成新的书签)
@author: Chen
"""
import winreg
import os
import PyPDF2 as pdf
import time


def GetDesktop():
    key = winreg.OpenKey(winreg.HKEY_CURRENT_USER,\
    r'Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders',)
    return str(winreg.QueryValueEx(key, "Desktop")[0])


# get real index
def get_real_page(reader):
    pageLabels = {}
    for i in range(reader.numPages):
        page = reader.getPage(i)
        # 用一个字典把idnum与页码对应起来。
        pageLabels[page.indirectRef.idnum]=i
    return pageLabels


# get bookmark
def get_bookmark(reader):
    real_page = get_real_page(reader)
    outline = reader.outlines
    bookmark = {}
    for i in range(len(outline)):
        bookmark[outline[i].title]=real_page[outline[i].page.idnum]
    return bookmark


# cut out the object page and add its origin bookmark
def cut_pdf():
    obj = str(input('输入文件名,包含".pdf":'))
    if os.path.exists(obj):
        f = open(obj,'rb')
        # 创建一个reader对象来读取pdf文件的信息
        reader = pdf.PdfFileReader(f)
        bookmark = get_bookmark(reader)
        # 创建一个pdfwriter对象用于生成新的pdf
        writer = pdf.PdfFileWriter()
        start = eval(input('输入开始页面:'))-1
        end = eval(input('输入结束页面:'))-1
        if end<=reader.numPages and start>=0:
            for i in range(start,end+1):
                page = reader.getPage(i)
                writer.addPage(page)
            #把原书签copy过去
            for k,v in bookmark.items():
                if v > start and v1:
        writer = pdf.PdfFileWriter()
        num = 0
        if not os.path.exists('output.pdf'):
            for i in range(len(pdf_list)):
               f=open(pdf_list[i],'rb')
               reader = pdf.PdfFileReader(f)
               bookmark = get_bookmark(reader)
               for j in range(reader.numPages):
                   writer.addPage(reader.getPage(j))
               writer.addBookmark(pdf_list[i][:-4],num)
               for k,v in bookmark.items():
                   writer.addBookmark(k,v+num)    
               num = num+reader.numPages
               output = open('output.pdf','ab')
               writer.write(output)
               output.close()
               f.close()
        else:
            print('文件目录下不能有output.pdf')
    os.chdir(GetDesktop())        
    
if __name__ == '__main__': 
    #跳转到桌面
    os.chdir(GetDesktop())
    #合并pdf
    combine_pdf()
    #拆分pdf
    cut_pdf()

你可能感兴趣的:(python ——PDF合并与拆分)