介绍:
在Python编程中,有时我们需要根据特定的数据生成Excel文件。本文将介绍如何使用wxPython和pandas模块来实现这个目标。我们将创建一个简单的GUI应用程序,允许用户选择输出文件夹和输入的Excel文件,并根据Excel文件中每个单元格的字段名组合生成多个Excel文件。
C:pythoncodenewgenxlsbyxls.py
1. 准备工作
在开始之前,确保您已经安装了wxPython和pandas模块。如果没有安装,可以使用以下命令进行安装:
1 | pip install wxPythonpip install pandas |
2. 创建GUI窗口
首先,我们需要创建一个GUI窗口,以便用户可以选择输出文件夹和输入的Excel文件。我们将使用wxPython模块来创建窗口和按钮,并将事件与相应的回调函数关联起来。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | import wx import os import pandas as pd class MyFrame(wx.Frame): def __init__( self , parent, title): super (MyFrame, self ).__init__(parent, title = title, size = ( 400 , 200 )) panel = wx.Panel( self ) self .output_dir_btn = wx.Button(panel, label = "选择输出文件夹" ) self .Bind(wx.EVT_BUTTON, self .on_select_output_dir, self .output_dir_btn) self .input_file_btn = wx.Button(panel, label = "选择Excel文件" ) self .Bind(wx.EVT_BUTTON, self .on_select_input_file, self .input_file_btn) self .start_btn = wx.Button(panel, label = "开始生成" ) self .Bind(wx.EVT_BUTTON, self .on_start_generation, self .start_btn) sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add( self .output_dir_btn, 0 , wx. ALL |wx.EXPAND, 5 ) sizer.Add( self .input_file_btn, 0 , wx. ALL |wx.EXPAND, 5 ) sizer.Add( self .start_btn, 0 , wx. ALL |wx.EXPAND, 5 ) panel.SetSizer(sizer) def on_select_output_dir( self , event): dlg = wx.DirDialog( self , "选择输出文件夹" , style = wx.DD_DEFAULT_STYLE) if dlg.ShowModal() = = wx.ID_OK: self .output_dir = dlg.GetPath() print ( "输出文件夹:" , self .output_dir) dlg.Destroy() def on_select_input_file( self , event): dlg = wx.FileDialog( self , "选择Excel文件" , wildcard = "Excel files (*.xlsx)|*.xlsx" , style = wx.FD_OPEN | wx.FD_FILE_MUST_EXIST) if dlg.ShowModal() = = wx.ID_OK: self .input_file = dlg.GetPath() print ( "输入文件:" , self .input_file) dlg.Destroy() def on_start_generation( self , event): if not hasattr ( self , 'output_dir' ) or not hasattr ( self , 'input_file' ): wx.MessageBox( "请先选择输出文件夹和Excel文件" , "错误" , wx.OK | wx.ICON_ERROR) return df = pd.read_excel( self .input_file) for i, col in enumerate (df.iloc[:, 0 ]): column_names = col.split( ',' ) file_name = f "{i+1}.xlsx" file_path = os.path.join( self .output_dir, file_name) df_new = pd.DataFrame(columns = column_names) df_new.to_excel(file_path, index = False ) wx.MessageBox( "生成完成" , "提示" , wx.OK | wx.ICON_INFORMATION) self .Close() app = wx.App() frame = MyFrame( None , "Excel文件生成器" ) frame.Show() app.MainLoop()``` 在上述代码中,我们创建了一个`MyFrame`类,继承自wxPython的`Frame`类。该类表示我们的应用程序窗口,并包含了选择输出文件夹和选择Excel文件的按钮。 * * 3. 实现回调函数 * * 接下来,我们需要实现与按钮关联的回调函数。这些函数将在用户点击相应的按钮时被调用。 ```python def on_select_output_dir( self , event): dlg = wx.DirDialog( self , "选择输出文件夹" , style = wx.DD_DEFAULT_STYLE) if dlg.ShowModal() = = wx.ID_OK: self .output_dir = dlg.GetPath() print ( "输出文件夹:" , self .output_dir) dlg.Destroy() def on_select_input_file( self , event): dlg = wx.FileDialog( self , "选择Excel文件" , wildcard = "Excel files (*.xlsx)|*.xlsx" , style = wx.FD_OPEN | wx.FD_FILE_MUST_EXIST) if dlg.ShowModal() = = wx.ID_OK: self .input_file = dlg.GetPath() print ( "输入文件:" , self .input_file) dlg.Destroy() def on_start_generation( self , event): if not hasattr ( self , 'output_dir' ) or not hasattr ( self , 'input_file' ): wx.MessageBox( "请先选择输出文件夹和Excel文件" , "错误" , wx.OK | wx.ICON_ERROR) return df = pd.read_excel( self .input_file) for i, col in enumerate (df.iloc[:, 0 ]): column_names = col.split( ',' ) file_name = f "{i+1}.xlsx" file_path = os.path.join( self .output_dir, file_name) df_new = pd.DataFrame(columns = column_names) df_new.to_excel(file_path, index = False ) wx.MessageBox( "生成完成" , "提示" , wx.OK | wx.ICON_INFORMATION) self .Close() |
在上述代码中,on_select_output_dir
函数用于选择输出文件夹,并将选择的路径存储在self.output_dir
变量中。on_select_input_file
函数类似地用于选择输入的Excel文件,并将选择的文件路径存储在self.input_file
变量中。
on_start_generation
函数是最重要的回调函数。它首先读取选择的Excel文件,并逐个单元格解析字段名组合。然后,根据字段名组合创建新的DataFrame,并将其保存为一个新的Excel文件,文件名为序号加上.xlsx
后缀。生成的Excel文件将保存在选择的输出文件夹中。
4. 运行应用程序
最后,我们需要创建一个wx.App
对象并运行应用程序的主循环。
1 2 3 4 | app = wx.App() frame = MyFrame( None , "Excel文件生成器" ) frame.Show() app.MainLoop() |
这段代码创建了一个wx.App
对象和一个MyFrame
对象,并将应用程序的主循环交给app.MainLoop()
处理。
5. 效果演示
现在,我们已经完成了整个程序的编写。运行程序后,将会出现一个GUI窗口,您可以通过点击按钮选择输出文件夹和输入的Excel文件。当您点击”开始生成”按钮后,程序将根据Excel文件中的字段名组合生成多个Excel文件,并保存在指定的输出文件夹中。
这个程序提供了一个简单而实用的方法来根据Excel文件中的字段名组合生成多个Excel文件。您可以根据自己的需求进行修改和扩展,以满足更复杂的场景。
总结:
本文介绍了如何使用wxPython和pandas模块创建一个简单的GUI应用程序,用于根据Excel文件中的字段名组合生成多个Excel文件。通过选择输出文件夹和输入的Excel文件,程序能够自动解析字段名组合,并生成对应的Excel文件。这种方法可以方便地处理大量数据,并将其保存为易于管理和使用的Excel文件。
以上就是使用wxPython和pandas模块生成Excel文件的代码实现的详细内容,更多关于wxPython pandas生成Excel的资料请关注IT俱乐部其它相关文章!