Table of Contents
2. 表格拆分" >2. 表格拆分
3. 表格合并" >3. 表格合并
4. GUI设计" >4. GUI设计
5. 打包代码" >5. 打包代码
Home Backend Development Python Tutorial 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

Aug 10, 2023 pm 04:02 PM
python


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
Copy after login

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}张表格...')
Copy after login

导出结果如下:

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='汇总')
Copy after login

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;)]
          ]
Copy after login

由于我们在进行表格拆分时需要先选定文件及拆分字段,而拆分字段是在选定文件后读取到的文件数据的表头,所以需要在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()
Copy after login

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

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

5. 打包代码

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

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

部分参数含义:

-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 of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to efficiently integrate Node.js or Python services under LAMP architecture? How to efficiently integrate Node.js or Python services under LAMP architecture? Apr 01, 2025 pm 02:48 PM

Many website developers face the problem of integrating Node.js or Python services under the LAMP architecture: the existing LAMP (Linux Apache MySQL PHP) architecture website needs...

What is the reason why pipeline persistent storage files cannot be written when using Scapy crawler? What is the reason why pipeline persistent storage files cannot be written when using Scapy crawler? Apr 01, 2025 pm 04:03 PM

When using Scapy crawler, the reason why pipeline persistent storage files cannot be written? Discussion When learning to use Scapy crawler for data crawler, you often encounter a...

What is the reason why the Python process pool handles concurrent TCP requests and causes the client to get stuck? What is the reason why the Python process pool handles concurrent TCP requests and causes the client to get stuck? Apr 01, 2025 pm 04:09 PM

Python process pool handles concurrent TCP requests that cause client to get stuck. When using Python for network programming, it is crucial to efficiently handle concurrent TCP requests. ...

How to view the original functions encapsulated internally by Python functools.partial object? How to view the original functions encapsulated internally by Python functools.partial object? Apr 01, 2025 pm 04:15 PM

Deeply explore the viewing method of Python functools.partial object in functools.partial using Python...

Python Cross-platform Desktop Application Development: Which GUI Library is the best for you? Python Cross-platform Desktop Application Development: Which GUI Library is the best for you? Apr 01, 2025 pm 05:24 PM

Choice of Python Cross-platform desktop application development library Many Python developers want to develop desktop applications that can run on both Windows and Linux systems...

Python hourglass graph drawing: How to avoid variable undefined errors? Python hourglass graph drawing: How to avoid variable undefined errors? Apr 01, 2025 pm 06:27 PM

Getting started with Python: Hourglass Graphic Drawing and Input Verification This article will solve the variable definition problem encountered by a Python novice in the hourglass Graphic Drawing Program. Code...

How to optimize processing of high-resolution images in Python to find precise white circular areas? How to optimize processing of high-resolution images in Python to find precise white circular areas? Apr 01, 2025 pm 06:12 PM

How to handle high resolution images in Python to find white areas? Processing a high-resolution picture of 9000x7000 pixels, how to accurately find two of the picture...

How to efficiently count and sort large product data sets in Python? How to efficiently count and sort large product data sets in Python? Apr 01, 2025 pm 08:03 PM

Data Conversion and Statistics: Efficient Processing of Large Data Sets This article will introduce in detail how to convert a data list containing product information to another containing...

See all articles