当前位置: 首页 > news >正文

使用Python创建多功能文件管理器

简介

在本文中,我们将探索一个使用Python的wxPython库开发的文件管理器应用程序。这个应用程序不仅能够浏览和选择文件,还支持文件预览、压缩、图片转换以及生成PPT演示文稿的功能。
C:\pythoncode\new\filemanager.py

完整代码

import wx
import os
import zipfile
from PIL import ImageGrab, Image
from pptx import Presentation
import win32com.clientclass MyFrame(wx.Frame):def __init__(self):super().__init__(parent=None, title="文件管理器", size=(800, 600))self.panel = wx.Panel(self)# 布局self.main_sizer = wx.BoxSizer(wx.VERTICAL)self.top_sizer = wx.BoxSizer(wx.HORIZONTAL)self.mid_sizer = wx.BoxSizer(wx.HORIZONTAL)self.listbox_sizer = wx.BoxSizer(wx.VERTICAL)self.preview_sizer = wx.BoxSizer(wx.VERTICAL)self.bottom_sizer = wx.BoxSizer(wx.HORIZONTAL)# 文件类型选择self.file_type_choice = wx.Choice(self.panel, choices=["照片", "Word", "Excel", "PPT", "PDF"])self.file_type_choice.Bind(wx.EVT_CHOICE, self.on_file_type_selected)self.top_sizer.Add(self.file_type_choice, 1, wx.EXPAND | wx.ALL, 5)# 文件选择self.dir_picker = wx.DirPickerCtrl(self.panel, message="选择文件夹")self.dir_picker.Bind(wx.EVT_DIRPICKER_CHANGED, self.on_dir_picked)self.top_sizer.Add(self.dir_picker, 1, wx.EXPAND | wx.ALL, 5)# 包含子文件夹复选框self.include_subfolders_checkbox = wx.CheckBox(self.panel, label="包含子文件夹")self.include_subfolders_checkbox.Bind(wx.EVT_CHECKBOX, self.on_include_subfolders_checked)self.top_sizer.Add(self.include_subfolders_checkbox, 0, wx.ALIGN_CENTER_VERTICAL | wx.ALL, 5)# 分割器self.splitter = wx.SplitterWindow(self.panel)self.splitter.Bind(wx.EVT_SPLITTER_DCLICK, self.on_splitter_dclick)self.left_panel = wx.Panel(self.splitter)self.right_panel = wx.Panel(self.splitter)# 文件列表self.file_listbox = wx.ListBox(self.left_panel, style=wx.LB_MULTIPLE, size=(400, -1))self.file_listbox.Bind(wx.EVT_LISTBOX, self.on_file_selected)self.file_listbox.Bind(wx.EVT_LISTBOX_DCLICK, self.on_file_deselected)self.file_listbox.Bind(wx.EVT_LISTBOX, self.on_drag_select)        self.listbox_sizer.Add(self.file_listbox, 1, wx.EXPAND | wx.ALL, 5)# 全选和全不选按钮self.select_all_button = wx.Button(self.left_panel, label="全选")self.select_all_button.Bind(wx.EVT_BUTTON, self.on_select_all)self.deselect_all_button = wx.Button(self.left_panel, label="全不选")self.deselect_all_button.Bind(wx.EVT_BUTTON, self.on_deselect_all)self.listbox_sizer.Add(self.select_all_button, 0, wx.EXPAND | wx.ALL, 5)self.listbox_sizer.Add(self.deselect_all_button, 0, wx.EXPAND | wx.ALL, 5)# 设置左侧面板布局self.left_panel.SetSizer(self.listbox_sizer)# 预览窗口self.preview_panel = wx.Panel(self.right_panel, size=(400, 400))self.preview_sizer = wx.BoxSizer(wx.VERTICAL)self.preview_panel.SetSizer(self.preview_sizer)# self.preview_panel = wx.Panel(self.right_panel, size=(400, 400))# self.preview_panel.SetSizer(self.preview_sizer)# self.right_panel.SetSizer(self.preview_sizer)# 设置分割器布局self.splitter.SplitVertically(self.left_panel, self.right_panel)self.splitter.SetSashGravity(0.5)self.splitter.SetMinimumPaneSize(100)self.mid_sizer.Add(self.splitter, 1, wx.EXPAND | wx.ALL, 5)# 操作按钮self.zip_button = wx.Button(self.panel, label="压缩")self.zip_button.Bind(wx.EVT_BUTTON, self.on_zip)self.convert_button = wx.Button(self.panel, label="转换图片")self.convert_button.Bind(wx.EVT_BUTTON, self.on_convert)self.generate_ppt_button = wx.Button(self.panel, label="生成PPT文档")self.generate_ppt_button.Bind(wx.EVT_BUTTON, self.on_generate_ppt)self.bottom_sizer.Add(self.zip_button, 1, wx.EXPAND | wx.ALL, 5)self.bottom_sizer.Add(self.convert_button, 1, wx.EXPAND | wx.ALL, 5)self.bottom_sizer.Add(self.generate_ppt_button, 1, wx.EXPAND | wx.ALL, 5)# 整体布局self.main_sizer.Add(self.top_sizer, 0, wx.EXPAND)self.main_sizer.Add(self.mid_sizer, 1, wx.EXPAND)self.main_sizer.Add(self.bottom_sizer, 0, wx.EXPAND)self.panel.SetSizer(self.main_sizer)self.Show()def on_dir_picked(self, event):self.dir_path = self.dir_picker.GetPath()self.update_file_list()def on_file_type_selected(self, event):self.file_type = self.file_type_choice.GetStringSelection()self.update_file_list()def on_include_subfolders_checked(self, event):self.update_file_list()def update_file_list(self):if hasattr(self, 'dir_path') and hasattr(self, 'file_type'):self.file_listbox.Clear()include_subfolders = self.include_subfolders_checkbox.GetValue()for root, dirs, files in os.walk(self.dir_path):if not include_subfolders and root != self.dir_path:continuefor file in files:if self.file_type == "照片" and file.lower().endswith(('png', 'jpg', 'jpeg')):self.file_listbox.Append(os.path.join(root, file))elif self.file_type == "Word" and file.lower().endswith('docx'):self.file_listbox.Append(os.path.join(root, file))elif self.file_type == "Excel" and file.lower().endswith('xlsx'):self.file_listbox.Append(os.path.join(root, file))elif self.file_type == "PPT" and file.lower().endswith('pptx'):self.file_listbox.Append(os.path.join(root, file))elif self.file_type == "PDF" and file.lower().endswith('pdf'):self.file_listbox.Append(os.path.join(root, file))def on_file_selected(self, event):selections = self.file_listbox.GetSelections()if selections:file_path = self.file_listbox.GetString(selections[0])self.preview_file(file_path)def on_file_deselected(self, event):# 单击文件名时,取消选择event.Skip()def on_drag_select(self, event):selections = self.file_listbox.GetSelections()if selections:file_path = self.file_listbox.GetString(selections[0])self.preview_file(file_path)def on_select_all(self, event):count = self.file_listbox.GetCount()for i in range(count):self.file_listbox.Select(i)def on_deselect_all(self, event):count = self.file_listbox.GetCount()for i in range(count):self.file_listbox.Deselect(i)def preview_file(self, file_path):# Clear any existing content in the preview panelfor child in self.preview_panel.GetChildren():child.Destroy()if file_path.lower().endswith(('png', 'jpg', 'jpeg')):try:original_image = wx.Image(file_path, wx.BITMAP_TYPE_ANY)# Get the size of the preview panelpanel_size = self.preview_panel.GetSize()# Calculate the scaling factor to fit the image within the panelwidth_ratio = panel_size.width / original_image.GetWidth()height_ratio = panel_size.height / original_image.GetHeight()scale_factor = min(width_ratio, height_ratio)# Scale the imagenew_width = int(original_image.GetWidth() * scale_factor)new_height = int(original_image.GetHeight() * scale_factor)image = original_image.Scale(new_width, new_height, wx.IMAGE_QUALITY_HIGH)bitmap = wx.Bitmap(image)static_bitmap = wx.StaticBitmap(self.preview_panel, -1, bitmap)self.preview_sizer.Add(static_bitmap, 0, wx.CENTER)except Exception as e:print(f"Error loading image: {e}")# Other file types preview implementation can be added hereself.preview_panel.Layout()self.right_panel.Layout()def on_zip(self, event):file_paths = [self.file_listbox.GetString(i) for i in self.file_listbox.GetSelections()]with zipfile.ZipFile(os.path.join(self.dir_path, 'compressed_files.zip'), 'w') as zipf:for file_path in file_paths:zipf.write(file_path, os.path.basename(file_path))def on_convert(self, event):file_paths = [self.file_listbox.GetString(i) for i in self.file_listbox.GetSelections()]self.process_files(file_paths)def process_files(self, file_paths):excel = win32com.client.Dispatch("Excel.Application")excel.Visible = Falsefor file_path in file_paths:try:workbook = excel.Workbooks.Open(file_path)sheet = workbook.Sheets(1)# 获取工作表的使用范围used_range = sheet.UsedRange# 设置截图区域sheet.Range(used_range.Address).CopyPicture(Format=2)  # 2 表示位图# 创建一个临时图片对象并粘贴截图img = ImageGrab.grabclipboard()if img:# 保存截图base_name = os.path.splitext(file_path)[0]img_path = f"{base_name}_screenshot.png"img.save(img_path)print(f"截图已保存: {img_path}")else:print(f"无法为 {file_path} 创建截图")workbook.Close(SaveChanges=False)except Exception as e:print(f"处理 {file_path} 时出错: {str(e)}")excel.Quit()wx.MessageBox("所有文件处理完成", "完成", wx.OK | wx.ICON_INFORMATION)def on_generate_ppt(self, event):file_paths = [self.file_listbox.GetString(i) for i in self.file_listbox.GetSelections()]prs = Presentation()for file_path in file_paths:if file_path.lower().endswith(('png', 'jpg', 'jpeg')):slide = prs.slides.add_slide(prs.slide_layouts[5])img_path = file_pathslide.shapes.add_picture(img_path, 0, 0, prs.slide_width, prs.slide_height)prs.save(os.path.join(self.dir_path, 'output_ppt.pptx'))def on_splitter_dclick(self, event):self.splitter.Unsplit()def on_drag_select(self, event):selections = self.file_listbox.GetSelections()if selections:file_path = self.file_listbox.GetString(selections[0])self.preview_file(file_path)if __name__ == '__main__':app = wx.App(False)frame = MyFrame()app.MainLoop()
环境准备

在开始之前,请确保你的Python环境中已经安装了以下库:

  • wxPython:用于创建GUI应用程序。
  • Pillow:用于图像处理。
  • python-pptx:用于操作PPT文件。
  • win32com.client:用于处理Excel文件(仅限Windows系统)。

可以通过以下命令安装所需的库:

pip install wxPython Pillow python-pptx pywin32
应用程序结构

我们的文件管理器基于wxPython的框架构建,主要分为以下几个部分:

  1. 主窗口 (MyFrame 类):包含整个应用程序的布局和控件。
  2. 文件类型选择:允许用户根据文件类型筛选文件。
  3. 文件选择:使用目录选择控件让用户选择要浏览的文件夹。
  4. 子文件夹选项:一个复选框,决定是否包括子文件夹中的文件。
  5. 文件列表和预览:展示选中文件夹中的文件,并提供预览功能。
  6. 操作按钮:包括压缩文件、转换图片和生成PPT文档的功能。
代码解析
初始化和布局设置
class MyFrame(wx.Frame):def __init__(self):super().__init__(parent=None, title="文件管理器", size=(800, 600))# 初始化窗口、面板、大小器等# ...
文件类型选择和目录选择
self.file_type_choice = wx.Choice(self.panel, choices=["照片", "Word", "Excel", "PPT", "PDF"])
self.dir_picker = wx.DirPickerCtrl(self.panel, message="选择文件夹")

这里创建了一个下拉菜单供用户选择文件类型,以及一个目录选择控件来选择文件所在的文件夹。

文件列表更新
def update_file_list(self):# 根据选择的目录和文件类型更新文件列表# ...
文件预览
def preview_file(self, file_path):# 根据文件类型显示预览# ...
压缩文件
def on_zip(self, event):# 将选中的文件压缩成一个ZIP文件# ...
转换图片
def on_convert(self, event):# 将Excel文件转换为图片# ...
生成PPT文档
def on_generate_ppt(self, event):# 使用选中的图片生成PPT文档# ...
总结

这个文件管理器应用程序是一个功能丰富的工具,它展示了wxPython在创建桌面应用程序方面的强大能力。通过结合其他库,我们能够扩展其功能,满足不同的需求。

结果如下

在这里插入图片描述

结尾

希望本文能够帮助你了解如何使用Python和wxPython创建具有多种文件操作功能的桌面应用程序。如果你有任何问题或建议,请在下方留言。

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • AcWing食物链
  • Lua 脚本编程基础
  • 搭建nexus上传jar包,并结合jenkins运行项目
  • OpenCV||超细节的基本操作
  • Redis学习笔记——第19章 事务
  • 【算法】递归实现二分查找(优化)以及非递归实现二分查找
  • RMAN-06618不同版本之间RMAN无法连接
  • 『C++实战项目 负载均衡式在线OJ』二、编译模块编写(持续更新)
  • 【前端 22】使用Nginx部署前端项目
  • 手持式气象站:科技赋能精准气象观测
  • 长短期记忆网络LSTM
  • 自制安卓车机软件(含APP)
  • 1、爬⾍概述
  • 【2024】Datawhale AI夏令营 Task4笔记——vllm加速方式修改及llm推理参数调整上分
  • 神经网络基础--激活函数
  • 345-反转字符串中的元音字母
  • Docker容器管理
  • echarts的各种常用效果展示
  • web标准化(下)
  • 阿里云Kubernetes容器服务上体验Knative
  • 服务器之间,相同帐号,实现免密钥登录
  • 给第三方使用接口的 URL 签名实现
  • 工作中总结前端开发流程--vue项目
  • 力扣(LeetCode)22
  • 免费小说阅读小程序
  • 排序算法学习笔记
  • 前端面试总结(at, md)
  • 浅谈JavaScript的面向对象和它的封装、继承、多态
  • 事件委托的小应用
  • 它承受着该等级不该有的简单, leetcode 564 寻找最近的回文数
  • 移动互联网+智能运营体系搭建=你家有金矿啊!
  • 再谈express与koa的对比
  • ​RecSys 2022 | 面向人岗匹配的双向选择偏好建模
  • ‌JavaScript 数据类型转换
  • $L^p$ 调和函数恒为零
  • (12)Linux 常见的三种进程状态
  • (145)光线追踪距离场柔和阴影
  • (1综述)从零开始的嵌入式图像图像处理(PI+QT+OpenCV)实战演练
  • (4)事件处理——(2)在页面加载的时候执行任务(Performing tasks on page load)...
  • (C++哈希表01)
  • (CPU/GPU)粒子继承贴图颜色发射
  • (HAL)STM32F103C6T8——软件模拟I2C驱动0.96寸OLED屏幕
  • (大众金融)SQL server面试题(1)-总销售量最少的3个型号的车及其总销售量
  • (第一天)包装对象、作用域、创建对象
  • (附源码)ssm教师工作量核算统计系统 毕业设计 162307
  • (经验分享)作为一名普通本科计算机专业学生,我大学四年到底走了多少弯路
  • (三)SvelteKit教程:layout 文件
  • (转载)CentOS查看系统信息|CentOS查看命令
  • (转载)hibernate缓存
  • .mp4格式的视频为何不能通过video标签在chrome浏览器中播放?
  • .Net MVC + EF搭建学生管理系统
  • .NET 使用 ILRepack 合并多个程序集(替代 ILMerge),避免引入额外的依赖
  • .NET/C#⾯试题汇总系列:集合、异常、泛型、LINQ、委托、EF!(完整版)
  • .NET3.5下用Lambda简化跨线程访问窗体控件,避免繁复的delegate,Invoke(转)
  • .NET编程C#线程之旅:十种开启线程的方式以及各自使用场景和优缺点