基于wxPython、PIL的图片文件夹转换PDF器

本文介绍了一款简单实用的图片转PDF工具,该工具利用Python的wxPython和PIL库实现图形界面及图片到PDF的转换功能。用户只需选择包含图片的文件夹,程序即可自动将所有图片按顺序合并成一个PDF文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

0、编者话

因为刚好有需要,所以写了个简单的小工具。——zxx.20190717.

Github链接

1、初始化图形界面,并绑定按钮对应的函数

# coding=utf-8
import wx
import os
from PIL import Image

class ImgToPdf(wx.Frame):

    
    def __init__(self):
        wx.Frame.__init__(self,None,title='图片生成PDF器',size=(500,400))


        self.url_text = wx.TextCtrl(self, pos=(5, 5), size=(350, 24), value="请选择目录")
        
        self.select_button = wx.Button(self, label="选择目录", pos=(370, 5), size=(80, 24))
        self.select_button.Bind(wx.EVT_BUTTON, self.OnButton)
        
        self.open_button = wx.Button(self, label="生成", pos=(5, 30), size=(50, 24))
        self.open_button.Bind(wx.EVT_BUTTON, self.conpdf)

        self.content_text = wx.TextCtrl(self, pos=(5, 55), size=(475, 200), style=wx.TE_MULTILINE)

2、定义生成PDF函数

    # 生成PDF
    def conpdf(self, event):

        # 获取图片文件夹所在路径
        path = self.url_text.GetValue()
        pdf_name = os.path.basename(path)
        picList = []
        
        # 遍历图片文件夹内的图片,生成图片列表
        file_list = os.listdir(path)
        pic_name = []
        im_list = []
        for x in file_list:
            if "jpg" in x or 'png' in x or 'jpeg' in x:

                pic_name.append(x)

        pic_name.sort()
        new_pic = []
        
        for x in pic_name:
            if "jpg" in x:
                new_pic.append(x)

        for x in pic_name:
            if "png" in x:
                new_pic.append(x)
                
        for x in pic_name:
            if "jpeg" in x:
                new_pic.append(x)            
        
        # 打开第一张图片,并删除图片列表中第一张图片
        im1 = Image.open(os.path.join(path, new_pic[0]))
        if im1.mode == "RGBA":
            im1 = im1.convert('RGB')
        new_pic.pop(0)
        
        # 将图片打开分别添加至im_list
        for i in new_pic:
            img = Image.open(os.path.join(path, i))
            if img.mode == "RGBA":
                img = img.convert('RGB')
                im_list.append(img)
            else:
                im_list.append(img)

        # 将首张图片添加im_list,并保存为pdf至程序所在目录
        im1.save(pdf_name+'.pdf', "PDF", resolution=100.0, save_all=True, append_images=im_list)

3、定义选择目录函数

    # 选择文件夹
    def OnButton(self, event):
            # 打开选择文件夹窗口并赋值给目录文本框
            dlg = wx.DirDialog(self,u"选择文件夹",style=wx.DD_DEFAULT_STYLE)
            if dlg.ShowModal() == wx.ID_OK:
                self.url_text.SetValue(dlg.GetPath())
            dlg.Destroy()

4、运行窗口

if __name__=='__main__':
    app = wx.App()
    ImgToPdfFrame = ImgToPdf()
    ImgToPdfFrame.Show()
    app.MainLoop()

5、通过pyinstaller 生成exe文件

pyinstaller -F test.py --noconsole

6、运行结果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值