#https://blog.csdn.net/kmesky/article/details/102695520
#coding=utf8
import tkinter as tk
from tkinter import ttk
from tkinter import filedialog
from tkinter import messagebox #弹窗库
from PyPDF2 import PdfFileReader, PdfFileWriter
import os
MERGE_NONE = 0
MERGE_FILES = 1
MERGE_FOLDER = 2
def get_screen_size(window):
return window.winfo_screenwidth(), window.winfo_screenheight()
def get_window_size(window):
return window.winfo_reqwidth(), window.winfo_reqheight()
def center_window(root, width, height):
screenwidth = root.winfo_screenwidth()
screenheight = root.winfo_screenheight()
size = '%dx%d+%d+%d' % (width, height, (screenwidth - width)/2, (screenheight - height)/2)
root.geometry(size)
root.resizable(0,0)
# Create instance
win = tk.Tk()
# Add a title
win.title("PDF Merger")
win.iconbitmap("pdfMerger.ico")
#设定600*450居中,不可调整大小
center_window(win, 600, 450)
def setFileAndPage(fname, pdf, ps):
pdf.set(fname)
input = PdfFileReader(open(fname, "rb"))
# 获得源PDF文件中页面总数
pageCount = input.getNumPages()
ps.set("1-{}".format(pageCount))
def openPdfFile1():
fname = filedialog.askopenfilename(title='打开Pdf文件', filetypes=[('Pdf file', '*.pdf'), ('All Files', '*')])
setFileAndPage(fname, pdf1, ps1)
def openPdfFile2():
fname = filedialog.askopenfilename(title='打开Pdf文件', filetypes=[('Pdf file', '*.pdf'), ('All Files', '*')])
setFileAndPage(fname, pdf2, ps2)
def openPdfFile3():
fname = filedialog.askopenfilename(title='打开Pdf文件', filetypes=[('Pdf file', '*.pdf'), ('All Files', '*')])
setFileAndPage(fname, pdf3, ps3)
def openPdfFile4():
fname = filedialog.askopenfilename(title='打开Pdf文件', filetypes=[('Pdf file', '*.pdf'), ('All Files', '*')])
setFileAndPage(fname, pdf4, ps4)
def openPdfFile5():
fname = filedialog.askopenfilename(title='打开Pdf文件', filetypes=[('Pdf file', '*.pdf'), ('All Files', '*')])
setFileAndPage(fname, pdf5, ps5)
def openPdfFile6():
fname = filedialog.askopenfilename(title='打开Pdf文件', filetypes=[('Pdf file', '*.pdf'), ('All Files', '*')])
setFileAndPage(fname, pdf6, ps6)
def openPdfFile7():
fname = filedialog.askopenfilename(title='打开Pdf文件', filetypes=[('Pdf file', '*.pdf'), ('All Files', '*')])
setFileAndPage(fname, pdf7, ps7)
def openPdfFile8():
fname = filedialog.askopenfilename(title='打开Pdf文件', filetypes=[('Pdf file', '*.pdf'), ('All Files', '*')])
setFileAndPage(fname, pdf8, ps8)
def openPdfFile9():
fname = filedialog.askopenfilename(title='打开Pdf文件', filetypes=[('Pdf file', '*.pdf'), ('All Files', '*')])
setFileAndPage(fname, pdf9, ps9)
def openPdfFile10():
fname = filedialog.askopenfilename(title='打开Pdf文件', filetypes=[('Pdf file', '*.pdf'), ('All Files', '*')])
setFileAndPage(fname, pdf10, ps10)
def openPdfFolder():
folderName = filedialog.askdirectory(title="选择pdf文件夹")
print(folderName)
foundPdf = False
for path, dirnames, filenames in os.walk(folderName):
for filename in filenames:
if filename.lower().endswith(".pdf"):
foundPdf = True
break
if foundPdf:
folder.set(folderName)
else:
messagebox.showerror(folderName,'未找到pdf文件!!!')
#判断当前页码是否在选择的页码范围之内
#pageScope 1-5,10-20,15-30,3,5,-
def inPageScope(curPage, pageScope):
if pageScope == "":
return False
else:
scopes = pageScope.split(",")
for scope in scopes:
if scope == "-":
return True
elif '-' not in scope:
return curPage+1 == scope
else:
limits = scope.split("-")
if (limits[0] == "" or curPage+1 >= int( limits[0]) ) and \
(limits[1] == "" or curPage+1 <= int( limits[1]) ):
return True
else:
continue
return False
def mergeExtractedPdf(pdfFiles, outfile):
try:
print(outfile)
output = PdfFileWriter()
outputPages = 0
for pdf_file,ps in pdfFiles:
print(pdf_file)
print(ps)
print("路径:%s"%pdf_file)
if pdf_file == "" or ps == "":
continue
try:
# 读取源PDF文件
input = PdfFileReader(open(pdf_file, "rb"), strict = False)
# 获得源PDF文件中页面总数
pageCount = input.getNumPages()
print("页数:%d"%pageCount)
# 分别将page添加到输出output中
for iPage in range(pageCount):
if inPageScope(iPage, ps):
output.addPage(input.getPage(iPage))
outputPages += 1
except:
continue
print("合并后的总页数:%d."%outputPages)
# 写入到目标PDF文件
#outputStream = open(os.path.join(filepath, outfile), "wb")
outputStream = open(outfile, "wb")
output.write(outputStream)
outputStream.close()
messagebox.showinfo("合并后的总页数:%d."%outputPages, "PDF文件合并完成!")
except Exception as e:
messagebox.showerror("合并pdf出错", str(e))
# 使用os模块的walk函数,搜索出指定目录下的全部PDF文件
# 获取同一目录下的所有PDF文件的绝对路径
def getPdfFiles(filedir):
file_list = [os.path.join(root, filespath) \
for root, dirs, files in os.walk(filedir) \
for filespath in files \
if str(filespath).endswith('pdf')
]
return file_list if file_list else []
# 合并同一目录下的所有PDF文件
def mergePDF(filepath, outfile):
output = PdfFileWriter()
outputPages = 0
pdfFiles= getPdfFiles(filepath)
if pdfFiles:
for pdf_file in pdfFiles:
print("路径:%s"%pdf_file)
# 读取源PDF文件
input = PdfFileReader(open(pdf_file, "rb"))
# 获得源PDF文件中页面总数
pageCount = input.getNumPages()
outputPages += pageCount
print("页数:%d"%pageCount)
# 分别将page添加到输出output中
for iPage in range(pageCount):
output.addPage(input.getPage(iPage))
print("合并后的总页数:%d."%outputPages)
# 写入到目标PDF文件
#outputStream = open(os.path.join(filepath, outfile), "wb")
outputStream = open(outfile, "wb")
output.write(outputStream)
outputStream.close()
messagebox.showinfo("提示", "PDF文件合并完成!")
else:
messagebox.showinfo("提示", "没有可以合并的PDF文件!")
# 选择文件
selFiles = ttk.LabelFrame(win, text="选择文件或文件夹,页码范围:1-3,5-8,3(单页),4,-(全部页码)",width=560)
selFiles.grid(column=0, row=0, padx=4, pady=10)
#可以选择十个pdf文件
#pdfFile1
curRow = 0
lblF1 = ttk.Label(selFiles, text="文件1:")
lblF1.grid(column=0, row=curRow, sticky='W', pady=4)
pdf1 = tk.StringVar()
pdfFile1 = ttk.Entry(selFiles, textvariable=pdf1, width=48)
pdfFile1.grid(column=1, row=curRow, sticky='W', padx=4, pady=4)
btn1 = ttk.Button(selFiles, text='...', width=5, command=openPdfFile1)
btn1.grid(column=2, row=curRow, sticky='W', pady=4)
label1 = ttk.Label(selFiles, text="页码范围:")
label1.grid(column=3, row=curRow, sticky='W', pady=4)
ps1 = tk.StringV