一个有界面的pdf合成工具(python)超级简单实用可做成exe

这是一个带有图形用户界面的pdf合成工具,无需了解内部运行结构,非常友好,还可以做成exe放在电脑中,实用简单!话不多说,直接上

​

import os
from PyPDF2 import PdfFileReader,PdfFileWriter
import PySimpleGUI as sg


def FileNameGet(filedir):
    files_list=[os.path.join(root, filespath) \
                for root,dirs,files in os.walk(filedir) \
                for filespath in files \
                if str(filespath).endswith('pdf')
        
    ]
    return files_list if files_list else []

def main():
    
    layout=[
        [sg.T('选择文件夹:',size=(18,1)),sg.In(key='-file_dir-'),sg.FolderBrowse()],
        [sg.T('输入合并后的文件名:',size=(18,1)),sg.In(key='-output_file-'),sg.B('开始合并')],
        [sg.ML(size=(80,15),key='-ML-')],
        [sg.B('关于'),sg.In(key='-cd-')]
    ]

    window=sg.Window('PDF合并工具',layout)
    
    def PDFMerge(filepath,output_file):
        output=PdfFileWriter()
        output_pages=0
        pdf_file_name=FileNameGet(filepath)
        if pdf_file_name:
            for pdf_file in pdf_file_name:
                print('路径:%s'% pdf_file)
                input=PdfFileReader(open(pdf_file,'rb'))
                page_count=input.getNumPages()
                output_pages+=page_count
                window['-ML-'].update('正在进行或最后一个文件为:'+'\n'+'路径:%s'% pdf_file+'\n'+'页数:%d'% page_count)
                print('页数:%d'% page_count)

                for iPage in range(page_count):
                    output.addPage(input.getPage(iPage))
            window['-ML-'].update('正在进行或最后一个文件为:'+'\n'+'路径:%s'% pdf_file+'\n'+'页数:%d'% page_count+'\n'+'合并后总页数:%d'% output_pages)
            print('合并后总页数:%d'% output_pages)

            output_stream=open(os.path.join(filepath,output_file),'wb')
            try:
                output.write(output_stream)
                output_stream.close()
            except Exception as e:
                window['-ML-'].update('正在进行或最后一个文件为:'+'\n'+'路径:%s'% pdf_file+'\n'+'页数:%d'% page_count+'\n'+'合并后总页数:%d'% output_pages+'\n'+'合并失败,请重试!')
                print('合并失败,请重试!')
            window['-ML-'].update('正在进行或最后一个文件为:'+'\n'+'路径:%s'% pdf_file+'\n'+'页数:%d'% page_count+'\n'+'合并后总页数:%d'% output_pages+'\n'+f'合并后的文件路径:{filepath}/{output_file}'+'\n'+'PDF文件合并完成!')
            print('PDF文件合并完成!')
            
            print(f'合并后的文件路径:{filepath}/{output_file}')
        else:
            window['-ML-'].update('没有可以合并的PDF文件!')
            print('没有可以合并的PDF文件!')
        

    try:
        while True:
            event,values=window.read()
            print(event,values)
            if event==None:
                break
            if event=='开始合并':
                file_dir=values['-file_dir-']
                protion = os.path.splitext(values['-output_file-'])
                if protion[-1]=='.pdf':
                     output_file=values['-output_file-']
                else:
                    output_file=values['-output_file-']+'.pdf'
                PDFMerge(file_dir,output_file)
            if event=='关于':
                #window['-ML-'].update('123')
                sg.Popup('使用方法:请将需要合并的pdf放在一个文件夹中,点击Browse选择此文件夹,合并顺序按照文件夹中存放顺序合并,并将合并后的文件同时存入此文件夹中',title='关于')

    finally:
        window.close()
    
    

[点击并拖拽以移动]

​

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值