吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 7069|回复: 86
收起左侧

[原创工具] 一个合并Excel的小工具,把所有表行列标题内容合并到新表格

  [复制链接]
858983646 发表于 2024-6-5 23:00
本帖最后由 858983646 于 2024-6-10 11:04 编辑

完全用kimi写的,Python基础都还没看几个,编译出来40m有点大不会继续缩小了

链接:https://pan.baidu.com/s/1c7wkjZ0xCd5n3OowY5017A 提取码:c9wm 密码52pojie
32位的exe  https://wwz.lanzn.com/il2bh20yzfli 密码:gz4b

更新了下,加了个运行完打开输出文件夹,链接: https://pan.baidu.com/s/1IqBMvY2tuibtzbplveZv_w?pwd=ycwn 提取码: ycwn
可以把多个Excel表合并到一张表,先是把所有行标题和列标题汇总,做成一个新表,
然后每个单元格内容根据对应行列标题名字填入对应总表单元格
要求每个表都是第一行和第一列是名标题,不同表每一个第一行和列标题同名就表示一个东西
ps1合并后有多个完全相同内容,则只显示一个,若多个内容不完全相同,则全部显示,并标明来源表且标红
   2.打钩表示每个文件里的每张表都合并,不打勾表示只有每个文件的第一张才合并
Screenshot_2024-06-06-00-23-16-105_com.realvnc.viewer.android-edit.jpg
效果如图 IMG_20240606_125251.png

源码见下面
[Python] 纯文本查看 复制代码
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
import os
import pandas as pd
import tkinter as tk
from tkinter import filedialog, messagebox, Listbox, Scrollbar
from openpyxl import load_workbook
from openpyxl.styles import Alignment, PatternFill
 
# 定义处理Excel文件的函数
def process_excel_files(input_files, output_file):
    # 初始化一个空字典,用于存储所有行索引和列索引
    unique_rows = set()
    unique_cols = set()
 
    # 遍历所有Excel文件以收集所有唯一的行索引和列索引
    for excel_file in input_files:
        excel_file_path = os.path.join(os.getcwd(), excel_file)
        try:
            df = pd.read_excel(excel_file_path, index_col=0)
            unique_rows.update(df.index)
            unique_cols.update(df.columns)
        except Exception as e:
            print(f"Error reading {excel_file}: {e}")
            continue
 
    # 将集合转换为列表,以便可以作为DataFrame的索引和列
    unique_rows = list(unique_rows)
    unique_cols = list(unique_cols)
 
    # 创建一个空的DataFrame,行索引和列索引分别设置为所有唯一的行索引和列索引
    summary_df = pd.DataFrame(index=unique_rows, columns=unique_cols)
 
    # 再次遍历所有Excel文件,填充汇总表
    for excel_file in input_files:
        excel_file_path = os.path.join(os.getcwd(), excel_file)
        try:
            df = pd.read_excel(excel_file_path, index_col=0)
        except Exception as e:
            print(f"Error reading {excel_file}: {e}")
            continue
 
        # 获取带扩展名的文件名
        file_name_without_ext, file_extension = os.path.splitext(os.path.basename(excel_file))
        file_name_with_ext = file_name_without_ext + file_extension
        # 填充汇总表,为每个单元格的值添加文件名后缀
        for idx in df.index:
            for col in df.columns:
                current_value = summary_df.at[idx, col]
                new_value = df.at[idx, col]
 
                if pd.isna(new_value):
                    continue
 
                new_value_with_suffix = f"{new_value} * {file_name_with_ext}"
 
                if pd.isna(current_value) or current_value == new_value_with_suffix:
                    summary_df.at[idx, col] = new_value_with_suffix
                elif current_value != new_value_with_suffix:
                    summary_df.at[idx, col] = f"{current_value}‖r4kh5nV5zxSBDY92‖{new_value_with_suffix}"
 
    # 处理DataFrame中的每个单元格
    for idx in summary_df.index:
        for col in summary_df.columns:
            cell_value = str(summary_df.at[idx, col])
            if '‖r4kh5nV5zxSBDY92‖' in cell_value:
                values = cell_value.split('‖r4kh5nV5zxSBDY92‖')
                unique_values = {v.rsplit(' * ', 1)[0] if ' * ' in v else v for v in values}
                if len(unique_values) == 1:
                    summary_df.at[idx, col] = list(unique_values)[0]
 
    # 去除不带‖r4kh5nV5zxSBDY92‖的单元格的文件名后缀
    for idx in summary_df.index:
        for col in summary_df.columns:
            cell_value = summary_df.at[idx, col]
            if pd.isna(cell_value):
                continue
            if isinstance(cell_value, str) and '‖r4kh5nV5zxSBDY92‖' not in cell_value:
                parts = cell_value.rsplit(' * ', 1)
                if len(parts) == 2:
                    summary_df.at[idx, col] = parts[0]
 
    # 写入Excel文件,不包括格式
    summary_df.to_excel(output_file, index=True, engine='openpyxl')
 
    # 加载工作簿和工作表
    workbook = load_workbook(output_file)
    worksheet = workbook.active
 
    # 遍历工作表中的所有单元格
    for row in worksheet.iter_rows():
        for cell in row:
            if '‖r4kh5nV5zxSBDY92‖' in str(cell.value):
                cell_value_list = cell.value.split('‖r4kh5nV5zxSBDY92‖')
                cell.value = '\n'.join(cell_value_list)
                cell.alignment = Alignment(wrap_text=True)
                cell.fill = PatternFill(start_color="FFFF0000", end_color="FFFF0000", fill_type="solid")
 
    # 保存工作簿
    workbook.save(filename=output_file)
     
def process_excel_files_all(input_files, output_file):
    # 初始化一个空字典,用于存储所有行索引和列索引
    unique_rows = set()
    unique_cols = set()
 
    # 遍历所有Excel文件以收集所有唯一的行索引和列索引
    for excel_file in input_files:
        excel_file_path = os.path.join(os.getcwd(), excel_file)
        try:
            # 加载工作簿
            workbook = load_workbook(excel_file_path)
            # 遍历工作簿中的所有工作表
            for sheet in workbook.sheetnames:
                # 获取工作表数据
                df = pd.read_excel(excel_file_path, sheet_name=sheet, index_col=0)
                unique_rows.update(df.index)
                unique_cols.update(df.columns)
        except Exception as e:
            print(f"Error reading {excel_file}: {e}")
            continue
 
    # 将集合转换为列表,以便可以作为DataFrame的索引和列
    unique_rows = list(unique_rows)
    unique_cols = list(unique_cols)
 
    # 创建一个空的DataFrame,行索引和列索引分别设置为所有唯一的行索引和列索引
    summary_df = pd.DataFrame(index=unique_rows, columns=unique_cols)
 
    # 遍历所有Excel文件和工作表,填充汇总表
    for excel_file in input_files:
        excel_file_path = os.path.join(os.getcwd(), excel_file)
        try:
            workbook = load_workbook(excel_file_path)
            for sheet in workbook.sheetnames:
                # 重新读取当前工作表的数据
                df = pd.read_excel(excel_file_path, sheet_name=sheet, index_col=0)
 
                # 获取带扩展名的文件名和工作表名称
                file_name_without_ext, file_extension = os.path.splitext(os.path.basename(excel_file))
                sheet_name = sheet  # 直接使用工作表名称
                file_name_with_ext = f"{file_name_without_ext} - {sheet_name}{file_extension}"
 
                # 填充汇总表,为每个单元格的值添加文件名和工作表名后缀
                for idx in df.index:
                    for col in df.columns:
                        current_value = summary_df.at[idx, col]
                        new_value = df.at[idx, col]
 
                        if pd.isna(new_value):
                            continue
 
                        new_value_with_suffix = f"{new_value} * {file_name_with_ext}"
 
                        if pd.isna(current_value) or current_value == new_value_with_suffix:
                            summary_df.at[idx, col] = new_value_with_suffix
                        elif current_value != new_value_with_suffix:
                            summary_df.at[idx, col] = f"{current_value}‖r4kh5nV5zxSBDY92‖{new_value_with_suffix}"
 
        except Exception as e:
            print(f"Error reading {excel_file}: {e}")
            continue
 
    # 处理DataFrame中的每个单元格
    for idx in summary_df.index:
        for col in summary_df.columns:
            cell_value = str(summary_df.at[idx, col])
            if '‖r4kh5nV5zxSBDY92‖' in cell_value:
                values = cell_value.split('‖r4kh5nV5zxSBDY92‖')
                unique_values = {v.rsplit(' * ', 1)[0] if ' * ' in v else v for v in values}
                if len(unique_values) == 1:
                    summary_df.at[idx, col] = list(unique_values)[0]
 
    # 去除不带‖r4kh5nV5zxSBDY92‖的单元格的文件名后缀
    for idx in summary_df.index:
        for col in summary_df.columns:
            cell_value = summary_df.at[idx, col]
            if pd.isna(cell_value):
                continue
            if isinstance(cell_value, str) and '‖r4kh5nV5zxSBDY92‖' not in cell_value:
                parts = cell_value.rsplit(' * ', 1)
                if len(parts) == 2:
                    summary_df.at[idx, col] = parts[0]
 
    # 写入Excel文件,不包括格式
    summary_df.to_excel(output_file, index=True, engine='openpyxl')
 
    # 加载工作簿和工作表
    workbook = load_workbook(output_file)
    worksheet = workbook.active
 
    # 遍历工作表中的所有单元格
    for row in worksheet.iter_rows():
        for cell in row:
            if '‖r4kh5nV5zxSBDY92‖' in str(cell.value):
                cell_value_list = cell.value.split('‖r4kh5nV5zxSBDY92‖')
                cell.value = '\n'.join(cell_value_list)
                cell.alignment = Alignment(wrap_text=True)
                cell.fill = PatternFill(start_color="FFFF0000", end_color="FFFF0000", fill_type="solid")
 
    # 保存工作簿
    workbook.save(filename=output_file)
 
 
# 创建GUI窗口
root = tk.Tk()
root.title("Excel 文件合并器1.0")
 
# 获取屏幕的尺寸,并计算窗口位置使其居中
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
width = 600
height = 300
x = (screen_width - width) // 2
y = (screen_height - height) // 2
root.geometry(f"{width}x{height}+{x}+{y}")
 
# 存储输入文件的全局变量
input_files = None
process_all_sheets = False  # 新增变量,用于记录复选框状态
 
# 创建用于显示已选文件的Listbox和滚动条
file_listbox = Listbox(root, width=40, height=4# 根据需要调整宽度和高度
file_scroll = Scrollbar(root, orient="vertical", command=file_listbox.yview)  # 滚动条的command绑定到Listbox的yview
file_listbox.config(yscrollcommand=file_scroll.set# Listbox的yscrollcommand绑定到滚动条的set
file_listbox.pack(side="left", fill="both", expand=True)
file_scroll.pack(side="right", fill="y")
 
# 定义选择输入文件的函数
def select_input_files():
    global input_files
    file_path = filedialog.askopenfilenames(filetypes=[("Excel files", "*.xlsx;*.xls")])
    input_files = file_path if file_path else []
    if input_files:
        start_button.config(state='normal')
        # 更新Listbox显示选中的文件
        file_listbox.delete(0, tk.END)  # 清空Listbox
        for file in input_files:
            file_listbox.insert(tk.END, file# 插入文件路径到Listbox
 
# 创建输入文件选择按钮
input_button = tk.Button(root, text="选择输入文件", command=select_input_files)
input_button.pack()
 
# 创建复选框,让用户选择是否处理所有工作表
def toggle_checkbox():
    global process_all_sheets
    process_all_sheets = not process_all_sheets  # 切换复选框状态
 
include_checkbox = tk.Checkbutton(root, text="处理所有工作表。不选处理每个excel的第一张表", command=toggle_checkbox)
include_checkbox.pack()
 
# 定义开始处理文件的函数
def start_process():
    global input_files
    if input_files:
        output_path = filedialog.asksaveasfilename(defaultextension=".xlsx", filetypes=[("Excel files", "*.xlsx")])
        if output_path:
            # 根据复选框状态选择处理函数
            if process_all_sheets:
                process_excel_files_all(input_files, output_path)
            else:
                process_excel_files(input_files, output_path)
            # 弹出完成消息框后重置状态
            messagebox.showinfo("完成", "所有Excel文件已处理并汇总到一个Excel文件中。")
            # 重置状态
            input_files = None  # 重置输入文件列表
            file_listbox.delete(0, tk.END)  # 清空Listbox
            start_button.config(state='disabled'# 禁用开始处理按钮
            include_checkbox.deselect()  # 直接将复选框设置为未选中状态
    else:
        messagebox.showerror("错误", "请先选择输入文件。")
 
# 创建开始处理按钮(初始状态为禁用)
start_button = tk.Button(root, text="开始处理", command=start_process, state='disabled')
start_button.pack()
 
# 运行GUI主循环
root.mainloop()

免费评分

参与人数 11吾爱币 +16 热心值 +10 收起 理由
horking + 1 + 1 谢谢@Thanks!
Richeng + 1 + 1 我很赞同!
tt0000 + 1 + 1 压缩包密码是什么?
AngIos + 1 谢谢@Thanks!
15235109295 + 1 + 1 谢谢@Thanks!
bfgxp + 1 + 1 热心回复!
jamessteed + 1 + 1 用心讨论,共获提升!
sidesand + 1 谢谢@Thanks!
viconly + 1 + 1 用心讨论,共获提升!
wawjwmdj + 1 + 1 谢谢@Thanks!
风之暇想 + 7 + 1 感谢发布原创作品,吾爱破解论坛因你更精彩!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

 楼主| 858983646 发表于 2024-6-14 20:28
xinke-qd 发表于 2024-6-14 14:22
我录了屏,您看一下什么问题:https://www.alipan.com/s/LBDKkhVLrt3

我也不太懂,都ai写的,你直接运行源码试试,1234依次安装运行就好,exe安装要Add python.exe to PATH也打钩
 链接:https://pan.baidu.com/s/1MptQ4O5W6vg1mnILqzWQMA 提取码:099v--来自百度网盘超级会员的分享
 楼主| 858983646 发表于 2024-6-6 16:52
freedover 发表于 2024-6-6 16:19
这个是什么场景的需求

我工作中中遇到这边发来的表格是某些部门的某些数据,另一边发来的是另外一些部门的另外某些数据,有重复也有不重复的,很多张看的要晕了,就做了这个直接合并
showhand 发表于 2024-6-6 16:17
freedover 发表于 2024-6-6 16:19
这个是什么场景的需求
12394613643 发表于 2024-6-6 16:51
原创的看看好不好去
xuming0315 发表于 2024-6-6 17:21
挺牛的表格合并啊。
PEKO16 发表于 2024-6-6 17:32
PQ就可以实现了 1行M语言完事
 楼主| 858983646 发表于 2024-6-6 17:50
PEKO16 发表于 2024-6-6 17:32
PQ就可以实现了 1行M语言完事

谢谢,pq没了解过,有空去看看
头像被屏蔽
psj736213 发表于 2024-6-7 04:21
提示: 作者被禁止或删除 内容自动屏蔽
zd8028122 发表于 2024-6-7 08:14
楼主,打开提示No error messages generated。F0rmatMessageW:参数错误
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2025-5-23 16:16

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表