鱼C论坛

 找回密码
 立即注册
查看: 1626|回复: 2

[作品展示] 用python批量合并pdf文件

[复制链接]
发表于 2023-6-8 19:34:07 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 楪祈53409 于 2023-6-8 20:03 编辑

原帖为
https://fishc.com.cn/forum.php?mod=viewthread&tid=208162&page=1&extra=#pid6255293

但这个代码的许多函数用法都过期了,我对楼主的代码进行了维护,修改了部分被废弃的函数,现在能用了
我在py3.0上运行的,初次运行没安装库的先win+r分别运行
  1. pip install PySimpleGUI
复制代码
  1. pip install PyPDF2
复制代码

安装所需的库.
以下是我维护好的代码:
  1. import glob
  2. import PySimpleGUI as sg
  3. from PyPDF2 import PdfWriter, PdfReader

  4. fra1 = sg.Frame('打开文件夹', [[sg.InputText(key='-IN-'), sg.FolderBrowse('文件位置')], [sg.Button('确认')]])
  5. fra2 = sg.Frame('文件列表', [[sg.Listbox(values=[''], size=(54, 30), key='-LIST-')],
  6.                          [sg.Button('上移'), sg.Button('下移'), sg.Button('移除'), sg.Button('合并')]])

  7. col = sg.Column([[fra1], [fra2]], pad=(0, 0))

  8. layout = [[col]]
  9. window = sg.Window('合并PDF', layout, keep_on_top=True)

  10. filename = []

  11. while True:
  12.     event, value = window.read()
  13.     if event is None:
  14.         break
  15.     elif event == '确认':
  16.         if value['-IN-'] != '':
  17.             path = value['-IN-'] + '/*.pdf'
  18.             file = glob.glob(path)
  19.             if file:
  20.                 filefolder = file[0].split('\\')[0] + '/'
  21.                 filename = [i.split('\\')[-1] for i in file]
  22.                 window['-LIST-'].update(filename)
  23.             else:
  24.                 sg.popup_ok('文件夹内未识别到PDF文件~', title='错误', keep_on_top=True)
  25.         else:
  26.             sg.popup_ok('请选择文件夹~', title='错误', keep_on_top=True)
  27.     elif event == '上移':
  28.         try:
  29.             choice = value['-LIST-']
  30.             index = filename.index(choice[0])
  31.             filename[index], filename[index-1] = filename[index-1], filename[index]
  32.             window['-LIST-'].update(filename)
  33.         except NameError:
  34.             pass
  35.     elif event == '下移':
  36.         try:
  37.             choice = value['-LIST-']
  38.             index = filename.index(choice[0])
  39.             filename[index], filename[index+1] = filename[index+1], filename[index]
  40.             window['-LIST-'].update(filename)
  41.         except NameError:
  42.             pass
  43.         except IndexError:
  44.             pass
  45.     elif event == '移除':
  46.         try:
  47.             choice = value['-LIST-'][0]
  48.             filename.remove(choice)
  49.             window['-LIST-'].update(filename)
  50.         except NameError:
  51.             pass
  52.         except IndexError:
  53.             pass
  54.     elif event == '合并':
  55.         try:
  56.             if filename:
  57.                 file = [filefolder + i for i in filename]
  58.                 pdfFileWriter = PdfWriter()
  59.                 for pdf in file:
  60.                     pdfReader = PdfReader(open(pdf, 'rb'))
  61.                     for page in pdfReader.pages:
  62.                         pdfFileWriter.add_page(page)
  63.                 folder = sg.popup_get_folder('请选择文件保存位置:', title='提示', keep_on_top=True)
  64.                 with open(folder + '/合并文件.pdf', 'wb') as output_pdf:
  65.                     pdfFileWriter.write(output_pdf)
  66.                 sg.popup_ok('文件已合并完成~', keep_on_top=True)
  67.         except NameError:
  68.             pass

  69. window.close()
复制代码

评分

参与人数 1荣誉 +5 贡献 +3 收起 理由
歌者文明清理员 + 5 + 3

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-6-8 19:45:54 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-6-8 20:03:19 | 显示全部楼层
本帖最后由 楪祈53409 于 2023-6-8 20:06 编辑


谢谢,我这个是用来批量合并pdf文件的,例如大量pdf形式的图表不方便传输和打印.
另:如何在论坛上传打包好的exe文件?每个人的运行环境不同容易造成这类"小工具"的代码并不是那么通用,我想上传打包好的exe文件但太大了超出了附件的限制(打包文件大小22MB)
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-24 04:09

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表