Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,31 +1,50 @@
|
|
1 |
-
# # 定义一个函数,该函数将接受上传的XLSX文件并显示其内容
|
2 |
-
# def read_xlsx(file):
|
3 |
-
# try:
|
4 |
-
# # 使用pandas读取上传的XLSX文件
|
5 |
-
# df = pd.read_excel(file.name)
|
6 |
-
# # 返回XLSX文件的内容
|
7 |
-
# return df.to_html()
|
8 |
-
# except Exception as e:
|
9 |
-
# return str(e)
|
10 |
-
|
11 |
-
# # 创建Gradio界面
|
12 |
-
# iface = gr.Interface(
|
13 |
-
# fn=read_xlsx,
|
14 |
-
# inputs=gr.inputs.File(label="上传XLSX文件"),
|
15 |
-
# outputs=gr.outputs.HTML(),
|
16 |
-
# title="XLSX文件内容查看器"
|
17 |
-
# )
|
18 |
-
|
19 |
-
# # 启动Gradio应用程序
|
20 |
-
# iface.launch()
|
21 |
-
|
22 |
import gradio as gr
|
23 |
import pandas as pd
|
24 |
|
|
|
25 |
def process_excel(file):
|
|
|
26 |
df = pd.read_excel(file.name, engine='openpyxl')
|
27 |
-
df = pd.DataFrame(df)
|
28 |
return df
|
29 |
|
30 |
-
|
31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
import pandas as pd
|
3 |
|
4 |
+
|
5 |
def process_excel(file):
|
6 |
+
# 注意这里的file.name,如果自己在线下python环境运行的话要删掉(.name)
|
7 |
df = pd.read_excel(file.name, engine='openpyxl')
|
|
|
8 |
return df
|
9 |
|
10 |
+
def merge_excel_tables(file_path1, file_path2,output_file = '合并后的表.xlsx'):
|
11 |
+
# 读取第一张表
|
12 |
+
df1 = process_excel(file_path1)
|
13 |
+
|
14 |
+
# 读取第二张表
|
15 |
+
df2 = process_excel(file_path2)
|
16 |
+
|
17 |
+
# 执行关联操作,假设关联列名为'关联列'
|
18 |
+
merged_df = df1.merge(df2, left_on=list(df1.columns)[0], right_on=list(df2.columns)[0], how='left')
|
19 |
+
|
20 |
+
# 重命名合并后的表格的列名,去除_x和_y后缀
|
21 |
+
column_mapping = {}
|
22 |
+
for col in df1.columns:
|
23 |
+
column_mapping[col] = col
|
24 |
+
|
25 |
+
for col in merged_df.columns:
|
26 |
+
if col.endswith('_x'):
|
27 |
+
new_col = col[:-2] # 去除_x后缀
|
28 |
+
if new_col in column_mapping:
|
29 |
+
column_mapping[new_col] = new_col # 如果存在相同的列名,保持不变
|
30 |
+
merged_df.rename(columns={col: new_col}, inplace=True)
|
31 |
+
elif col.endswith('_y'):
|
32 |
+
new_col = col[:-2] # 去除_y后缀
|
33 |
+
merged_df.rename(columns={col: new_col}, inplace=True)
|
34 |
+
# 删除没有数据的列
|
35 |
+
merged_df = merged_df.dropna(axis=1, how='all')
|
36 |
+
|
37 |
+
#把修改后表格的标签写好
|
38 |
+
merged_df = merged_df[list(df1.columns)]
|
39 |
+
|
40 |
+
# # 保存结果到新的Excel文件
|
41 |
+
# merged_df.to_excel(output_file, index=False)
|
42 |
+
|
43 |
+
return merged_df
|
44 |
+
|
45 |
+
iface = gr.Interface(fn=merge_excel_tables,
|
46 |
+
inputs=[gr.File(type='file',label='excle表格1'),
|
47 |
+
gr.File(type='file',label='excle表格2')],
|
48 |
+
outputs=[gr.outputs.Dataframe(type='pandas',label='合并后表格')],
|
49 |
+
title="强哥的Excel Processor")
|
50 |
+
iface.launch()
|