Home  >  Article  >  Backend Development  >  Tips | Write a table splitting and merging gadget yourself with 80 lines of code

Tips | Write a table splitting and merging gadget yourself with 80 lines of code

Python当打之年
Python当打之年forward
2023-08-10 16:02:481246browse


Recently some new friends have seen the previous article "Comparing Python with VBA to implement excel table merging and splitting" Split ", I would like to ask if there is any free gadget that can split and merge tables. In fact, wps has these two functions, and the effect is very good. However, considering that everyone may not be used to wps, here we simply write a small tool to satisfy everyone.

Let’s take a look at the gadget operation process first!

The effects of merging and splitting are as follows:

Tips | Write a table splitting and merging gadget yourself with 80 lines of code
Merge - Split

Next, let’s try Try writing this tool yourself!

1. Preliminary preparation

Since we use python for tool writing, and ultimately need Packaged into an exe file for our use. In order to reduce the package size, we need to first create a virtual environment for backup.

In addition, the third-party library pandas is used for table splitting and merging operations. At the same time, we use pysimplegui for the gui, which is packaged into exe. The one is pyinstaller. After creating the virtual environment, let's install the third-party libraries we need to use one by one.

# 创建虚拟环境
conda create -n env_1 python=3.8.8

# 激活虚拟环境
conda  activate env_1

# 安装三个库 (pandas一些操作需要依赖openpyxl和xlrd,建议加上)
pip install pandas
pip install openpyxl
pip install xlrd
pip install pysimplegui
pip install pyinstaller

Regarding these three libraries, you can check the official documentation to learn more:

pandas : https://pandas.pydata. org/

##pysimplegui:https://pysimplegui.readthedocs.io/en/latest/

pyinstaller: http://www.pyinstaller.org/

The early preparation tools are ready, and we begin to enter the tool writing stage.

2. 表格拆分

Python实现表格拆分的逻辑比较简单,就是利用pandas分组然后将每组的数据单独导出存表即可

原表数据长这样:

Tips | Write a table splitting and merging gadget yourself with 80 lines of code
汇总表
# 拆分表格
def splitTable(df,_key):
    print('----------正在进行表格拆分----------')
    df = df.astype('str')
    # 按照字段_key进行分组
    grouped = df.groupby(by = _key)
    # 输出分组数据导出成单表
    for num , (i, data) in enumerate(grouped):
        data.to_excel(f'.\\{i}.xlsx',index = False,sheet_name = i)
        print(f'已经拆成{num+1}张表格...')

导出结果如下:

Tips | Write a table splitting and merging gadget yourself with 80 lines of code
拆分

3. 表格合并

Python实现表格合并的本质是 遍历全部表格数据,然后采用concat方法进行数据合并Pandas学习笔记02-数据合并

因此,在这里我们主要用到两个库:ospandas,其中os用于获取文件夹下全部满足要求的文件信息,pandas用于读取表格数据并进行concat

# 合并表格
def concatTable(folder):
    print('----------正在进行表格合并----------')
    # 新建一个空列表,用于存储表格数据
    fileList = []
    # 把文件夹下表格数据放在一个列表里
    for fileName in os.walk(folder):
        for table in fileName[2]:
            path = fileName[0] + '\\' + table
            if os.path.splitext(path)[1] in ('.xlsx','.xls'):
                li = pd.read_excel(path)
                fileList.append(li)
                print(f'已读取{len(fileList)}个表格...')
            else:
                continue
    # 用concat方法合并表单数据
    result = pd.concat(fileList)
    # 导出数据
    result.to_excel(r'.\合并后文件.xlsx',index=False,sheet_name='汇总')

4. GUI设计

因为要支持表格拆分和合并,我们已经在2和3部分将这两个功能封装为函数了。

Regarding the functional part of the GUI, the following functions need to be supported.

For the table splitting part, function points:

  • Select file: <span style="font-size: 14px;">Text</span><span style="font-size: 14px;">InputText</span><span style="font-size: 14px;">FileBrowse</span>
  • Split field selection after reading the file: <span style="font-size: 14px;">Text</span><span style="font-size: 14px;">Combo</span>
  • Split:<span style="font-size: 14px;">Button</span>
##For table merge part ,function points:

  • 选取文件夹:<span style="font-size: 14px;">Text</span><span style="font-size: 14px;">InputText</span><span style="font-size: 14px;">FolderBrowse</span>
  • 合并:<span style="font-size: 14px;">Button</span>

此外,我们还需要有用于展示 程序操作记录的输出框、工具操作说明文本以及关闭程序按钮。

基于以上需求,我们可以构建GUI布局如下:

# 布局设置
layout = [[sg.Text(&#39;选择待拆分的文件:&#39;,font=("微软雅黑", 12)),sg.InputText(key=&#39;file&#39;,size=(60,1),font=("微软雅黑", 10),enable_events=True) ,sg.FileBrowse(&#39;打开&#39;,file_types=(("Text Files", "*.xls*"),),font=("微软雅黑", 12))],
          [sg.Text(&#39;选择待拆分的字段:&#39;,font=("微软雅黑", 12)),sg.Combo(&#39;&#39;,tooltip=&#39;选择用于拆分的字段&#39;,font=("微软雅黑", 10), default_value=&#39;&#39;,auto_size_text=True,size=(15, 5),key=&#39;-keys-&#39;),sg.Button(&#39;开始拆分&#39;,font=("微软雅黑", 12))],
          [sg.Text(&#39;选择待合并文件夹:&#39;,font=("微软雅黑", 12)),sg.InputText(key=&#39;Folder&#39;,size=(60,1),font=("微软雅黑", 10),enable_events=True) ,sg.FolderBrowse(&#39;打开文件夹&#39;,font=("微软雅黑", 12)),sg.Button(&#39;开始合并&#39;,font=("微软雅黑", 12))],
          [sg.Text(&#39;程序操作记录:&#39;,justification=&#39;center&#39;)],
          [sg.Output(size=(100, 10),font=("微软雅黑", 10))],  
          [sg.Text(&#39;操作说明:&#39;,font=("微软雅黑", 12))],
          [sg.Text(&#39;表格拆分指引:选择文件—>选择用于拆分的字段—>开始拆分\n表格合并指引:选择需要合并的表格所在文件夹—>开始合并&#39;,font=("微软雅黑", 10)),sg.Text(&#39;&#39;,font=("微软雅黑", 12),size=(35, 1)),sg.Button(&#39;关闭程序&#39;,font=("微软雅黑", 12),button_color =&#39;red&#39;)]
          ]

由于我们在进行表格拆分时需要先选定文件及拆分字段,而拆分字段是在选定文件后读取到的文件数据的表头,所以需要在sg.InputText()中将参数enable_events设置为True,这样选定文件操作就是一个事件,可以触发某些操作。

接下来,我们编写循环事件功能如下:

# 事件循环
while True:
    event, values = window.read()
    if event in (None, &#39;关闭程序&#39;):
        break
    if event == &#39;file&#39;:
        fileName = values[&#39;file&#39;]
        if os.path.exists(fileName):
            df = pd.read_excel(fileName)
            keys = df.columns.to_list()
            window["-keys-"].Update(values = keys,font=("微软雅黑", 10),size=(15, 8))
        else:
            print(&#39;文件不存在\n请先选择正确文件&#39;)
    if event == &#39;开始拆分&#39;:
        if values[&#39;-keys-&#39;]:
            _key = values[&#39;-keys-&#39;]
            splitTable(df,_key)
            print(&#39;----------拆分工作已经完成----------\n&#39;)
        else:
            print(&#39;字段未选择-请先选择字段\n或文件未选取-请先选择文件&#39;)
    if event == &#39;开始合并&#39;:
        if values[&#39;Folder&#39;]:
            folder = values[&#39;Folder&#39;]
            concatTable(folder)
            print(&#39;----------合并工作已经完成----------\n&#39;)
        else:
            print(&#39;待合并文件所在文件夹未选择&#39;)

window.close()

根据需求,我们将事件类型分为三种:

  • 当我们选定待拆分文件后,先读取文件内容,然后获取文件数据的表头,从而刷新( window["-keys-"].Update)拆分字段的下拉框为表头内容;
  • 当我们点击开始拆分按钮时,需要判断拆分字段是否选取(默认为空),若有选定字段则进行拆分操作,否则会进行错误操作提示;
  • 当我们点击开始合并按钮时,需要判断是否选定了文件夹,若是则调用合并数据函数进行合并操作,否则会进行错误操作提示。

5. 打包代码

这里采用的是pyinstaller进行程序代码打包,操作指令如下:

pyinstaller -F -w 表格拆分合并工具.py

部分参数含义:

-F   表示生成单个可执行文件

-w  表示去掉控制台窗口,这在GUI界面时非常有用

-p   表示你自己自定义需要加载的类路径,一般情况下用不到

-i   表示可执行文件的图标

The above is the detailed content of Tips | Write a table splitting and merging gadget yourself with 80 lines of code. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:Python当打之年. If there is any infringement, please contact admin@php.cn delete