用Python写了一个PDF合并的小工具
# -*- coding: utf-8 -*-# @Time : 2021-12-6 9:10
# @Author : Administrator
# @Email : lcmmljs@foxmail.com
# @File : main.py
# @Project : Project
import glob
import PySimpleGUI as sg
from PyPDF2 import PdfFileReader, PdfFileWriter
# 创建GUI布局
fra1 = sg.Frame('打开文件夹', [, ])
fra2 = sg.Frame('文件列表', [, s=(54, 30), key='-LIST-')],
])
col = sg.Column([, ], pad=(0, 0))
layout = []
window = sg.Window('合并PDF', layout, keep_on_top=True)
# 创建程序主循环
while True:
event, value = window.read()
if event is None:
break
elif event == '确认':
if value['-IN-'] != '':
path = window['-IN-'].get() + '/*.pdf'
file = glob.glob(path)
if file:
window['-LIST-'].update(file)
else:
sg.popup_ok('文件夹内未识别到PDF文件~', title='错误', keep_on_top=True)
else:
sg.popup_ok('请选择文件夹~', title='错误', keep_on_top=True)
elif event == '上移':
if value['-LIST-']:
choice = value['-LIST-']
index = file.index(choice)
file, file = file, file
window['-LIST-'].update(file)
elif event == '下移':
if value['-LIST-']:
choice = value['-LIST-']
index = file.index(choice)
file, file = file, file
window['-LIST-'].update(file)
elif event == '合并':
if file:
pdfFileWriter = PdfFileWriter()
for pdf in file:
pdfReader = PdfFileReader(open(pdf, 'rb'))
numPages = pdfReader.getNumPages()
for i in range(0, numPages):
pageObj = pdfReader.getPage(i)
pdfFileWriter.addPage(pageObj)
folder = sg.popup_get_folder('请选择文件保存位置:', title='提示', keep_on_top=True)
pdfFileWriter.write(open(folder + '//合并文件.pdf', 'wb'))
window.close()
稍微更新一下~
# -*- coding: utf-8 -*-
# @Time : 2021-12-6 9:10
# @Author : Administrator
# @Email : lcmmljs@foxmail.com
# @File : main.py
# @Project : Project
import glob
import PySimpleGUI as sg
from PyPDF2 import PdfFileReader, PdfFileWriter
# 创建GUI布局
fra1 = sg.Frame('打开文件夹', [, ])
fra2 = sg.Frame('文件列表', [, s=(54, 30), key='-LIST-')],
])
col = sg.Column([, ], pad=(0, 0))
layout = []
window = sg.Window('合并PDF', layout, keep_on_top=True)
# 创建程序主循环
while True:
event, value = window.read()
if event is None:
break
elif event == '确认':
if value['-IN-'] != '':
path = window['-IN-'].get() + '/*.pdf'
file = glob.glob(path)
if file:
filefloder = file.split('\\') + '/'
filename = for i in file]
window['-LIST-'].update(filename)
else:
sg.popup_ok('文件夹内未识别到PDF文件~', title='错误', keep_on_top=True)
else:
sg.popup_ok('请选择文件夹~', title='错误', keep_on_top=True)
elif event == '上移':
try:
choice = value['-LIST-']
index = filename.index(choice)
filename, filename = filename, filename
window['-LIST-'].update(filename)
except NameError:
pass
elif event == '下移':
try:
choice = value['-LIST-']
index = filename.index(choice)
filename, filename = filename, filename
window['-LIST-'].update(filename)
except NameError:
pass
elif event == '移除':
try:
choice = value['-LIST-']
filename.remove(choice)
window['-LIST-'].update(filename)
except NameError:
pass
except IndexError:
pass
elif event == '合并':
try:
if filename:
file =
pdfFileWriter = PdfFileWriter()
for pdf in file:
pdfReader = PdfFileReader(open(pdf, 'rb'))
numPages = pdfReader.getNumPages()
for i in range(0, numPages):
pageObj = pdfReader.getPage(i)
pdfFileWriter.addPage(pageObj)
folder = sg.popup_get_folder('请选择文件保存位置:', title='提示', keep_on_top=True)
pdfFileWriter.write(open(folder + '//合并文件.pdf', 'wb'))
sg.popup_ok('文件已合并完成~', keep_on_top=True)
except NameError:
pass
window.close()
本帖最后由 楪祈53409 于 2023-6-8 19:30 编辑
你好,我对你的代码进行了维护,修改了部分被废弃的函数.
我在py3.0上运行的,初次运行没安装库的先win+r分别运行pip install PySimpleGUI 和pip install PyPDF2
安装所需的库.
以下是我维护好的代码:
import glob
import PySimpleGUI as sg
from PyPDF2 import PdfWriter, PdfReader
fra1 = sg.Frame('打开文件夹', [, ])
fra2 = sg.Frame('文件列表', [, size=(54, 30), key='-LIST-')],
])
col = sg.Column([, ], pad=(0, 0))
layout = []
window = sg.Window('合并PDF', layout, keep_on_top=True)
filename = []
while True:
event, value = window.read()
if event is None:
break
elif event == '确认':
if value['-IN-'] != '':
path = value['-IN-'] + '/*.pdf'
file = glob.glob(path)
if file:
filefolder = file.split('\\') + '/'
filename = for i in file]
window['-LIST-'].update(filename)
else:
sg.popup_ok('文件夹内未识别到PDF文件~', title='错误', keep_on_top=True)
else:
sg.popup_ok('请选择文件夹~', title='错误', keep_on_top=True)
elif event == '上移':
try:
choice = value['-LIST-']
index = filename.index(choice)
filename, filename = filename, filename
window['-LIST-'].update(filename)
except NameError:
pass
elif event == '下移':
try:
choice = value['-LIST-']
index = filename.index(choice)
filename, filename = filename, filename
window['-LIST-'].update(filename)
except NameError:
pass
except IndexError:
pass
elif event == '移除':
try:
choice = value['-LIST-']
filename.remove(choice)
window['-LIST-'].update(filename)
except NameError:
pass
except IndexError:
pass
elif event == '合并':
try:
if filename:
file =
pdfFileWriter = PdfWriter()
for pdf in file:
pdfReader = PdfReader(open(pdf, 'rb'))
for page in pdfReader.pages:
pdfFileWriter.add_page(page)
folder = sg.popup_get_folder('请选择文件保存位置:', title='提示', keep_on_top=True)
with open(folder + '/合并文件.pdf', 'wb') as output_pdf:
pdfFileWriter.write(output_pdf)
sg.popup_ok('文件已合并完成~', keep_on_top=True)
except NameError:
pass
window.close()
页:
[1]