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
def
process_excel_files(input_files, output_file):
unique_rows
=
set
()
unique_cols
=
set
()
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
unique_rows
=
list
(unique_rows)
unique_cols
=
list
(unique_cols)
summary_df
=
pd.DataFrame(index
=
unique_rows, columns
=
unique_cols)
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}"
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
]
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
]
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
()
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
unique_rows
=
list
(unique_rows)
unique_cols
=
list
(unique_cols)
summary_df
=
pd.DataFrame(index
=
unique_rows, columns
=
unique_cols)
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
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
]
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
]
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)
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
file_listbox
=
Listbox(root, width
=
40
, height
=
4
)
file_scroll
=
Scrollbar(root, orient
=
"vertical"
, command
=
file_listbox.yview)
file_listbox.config(yscrollcommand
=
file_scroll.
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'
)
file_listbox.delete(
0
, tk.END)
for
file
in
input_files:
file_listbox.insert(tk.END,
file
)
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)
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()
root.mainloop()